8

Windows 下更新 MySQL 至 5.7.22

 3 years ago
source link: http://misaka.im/index.php/archives/20/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Windows 下更新 MySQL 至 5.7.22

2018.05.10默认分类 0 评

在搭建 Laravel 项目时执行 php artisan migrate,报出如下错误:

    In Connection.php line 664:
    
      SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was t
      oo long; max key length is 1000 bytes (SQL: alter table `users` add unique
      `users_email_unique`(`email`))
    
    
    In PDOStatement.php line 107:
    
      SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was t
      oo long; max key length is 1000 bytes
    
    
    In PDOStatement.php line 105:
    
      SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was t
      oo long; max key length is 1000 bytes

因为有数据库最大 key 长度限制,造成访问冲突。

Google 了一下原因,在 Laravel 5.4 尝试合并数据表时也出现类似报错。

[[5.4] SQL error when migrating tables #17508](https://github.com/laravel/framework/issues/17508)

[Laravel migration: unique key is too long, even if specified
](https://stackoverflow.com/questions/23786359/laravel-migration-unique-key-is-too-long-even-if-specified/39750202#39750202
)

8.4. 数据库迁移 - 创建索引 - 索引长度 & MySQL / MariaDB

Laravel 默认使用 utf8mb4 字符,它支持在数据库中存储 "emojis" 。

如果你是在版本低于 5.7.7 的 MySQL release 或者版本低于 10.2.2 的 MariaDB release 上创建索引,那就需要你手动配置迁移生成的默认字符串长度。

即在 AppServiceProvider 中调用 Schema::defaultStringLength 方法来配置它 :

use Illuminate\Support\Facades\Schema;

/**
 * 引导任何应用程序服务。
 *
 * @return void
 */
public function boot()
{
    Schema::defaultStringLength(191);
}

或者,你可以开启数据库的 innodb_large_prefix 选项。 至于如何正确开启,请自行查阅数据库文档。

我坚信不支持 utf8mb4 字符集的原因,是数据库版本太低,并开始着手升级由 phpStudy 面板集成的 MySQL 5.5.53 。这是继承开发环境的版本:

  • Laravel Version: ^5.5.0
  • PHP Version: 7.1.15
    mysql> select
        -> version();
    +-----------+
    | version() |
    +-----------+
    | 5.5.53    |
    +-----------+
    1 row in set (0.00 sec)

防止万一,备份是必要的

  1. 关闭 Nginx 及数据库程序
  2. 备份原 MySQL 中所有数据库
  3. 备份 MySQL 主程序
  4. 清空 MySQL 主程序文件夹
  1. 点击 mysql-5.7.22-winx64.zip 下载新版数据库。
  2. 解压至原MySQL 主程序文件夹

E:phpStudyMySQL

  1. 发现压缩包中并没有配置文件 my.ini 。没关系,这里有一份:

保存为 ANSI 编码,否则会出现 [ERROR] Found option without preceding group in config file

    [client]
    port=3306
    default-character-set=utf8
    [mysqld] 
    # 设置为自己MYSQL的安装目录 
    basedir="E:/phpStudy/MySQL/"
    # 设置为MYSQL的数据目录 
    datadir="E:/phpStudy/MySQL/data/"
    port=3306
    character_set_server=utf8
    sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER
    #开启查询缓存
    explicit_defaults_for_timestamp=true
    skip-grant-tables

将 MySQL 主程序目录 ;E:\phpStudy\MySQL\bin 添加到 Windows 环境变量中

别忘了重启系统

    mysqld --initialize
    mysqld --install
    net start MySQL 

启动 MySQL 主程序

    C:\Windows\system32>net start MySQL
    MySQL 服务正在启动 .
    MySQL 服务已经启动成功。

使用 root 帐户登陆数据库

    C:\Windows\system32>mysql -uroot -p
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.22 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql>

查看 MySQL 版本号

    mysql> select version();
    +-----------+
    | version() |
    +-----------+
    | 5.7.22    |
    +-----------+
    1 row in set (0.00 sec)
    
    mysql>

从备份的 *.sql 文件中恢复所有数据库

执行 php artisan migrate 便生成正常的数据表

    E:\phpStudy\WWW\passport>php artisan migrate
    Migration table created successfully.
    Migrating: 2014_10_12_000000_create_users_table
    Migrated:  2014_10_12_000000_create_users_table
    Migrating: 2014_10_12_100000_create_password_resets_table
    Migrated:  2014_10_12_100000_create_password_resets_table
    ...

https://serverfault.com/questions/8855/how-do-you-add-a-windows-environment-variable-without-rebooting


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK