升级mysql8小记

January 7, 2020 by stee

然看到mysql8的特性介绍“比 MySQL 5.7快2倍,还带来了大量的改进和更快的性能!”砰然心动,果断想升级成mysql 8来体验一番。结果一升级悲剧发生,后续折腾了好几个小时,现将过程简要记录,以作备忘。
由于原yum源最新只到mysql 5.7,加载webtatic源更新并升级,升级成功后开启服务,
结果MySQL Daemon failed to start。查看日志:
[ERROR] [InnoDB] Operating system error number 2 in a file operation.
[ERROR] [InnoDB] The error means the system cannot find the path specified.
[ERROR] [InnoDB] If you are installing InnoDB, remember that you must create directories yourself, InnoDB does not create them.
[ERROR] [InnoDB] File ./ibdata1: 'open' returned OS error 71. Cannot continue operation
[ERROR] [InnoDB] Cannot continue operation.
提示找不到ibdata1文件,原来旧版数据文件未被覆盖,新版数据没有生成,删除原data目录数据
rm -rf /var/lib/mysql,重启服务成功。
心想还是蛮顺利的,随手打开网站报错:Error establishing a database connection。
连接数据库失败,于是本地登录,发现数据库有默认密码,还不为空。爬文一番发现mysql8默认有随机密码,网文介绍使用命令:mysqld –initialize 数据库会初始化,重新登录会在mysqld.log日志中记录生成的随机密码。结果试了好几次,日志中并没有传说中的随机密码。
于是准备重置密码,在配置文件my.cnf中添加参数skip-grant-tables跳过权限验证,重启免密码登录数据库。直接修改密码:

alter user 'user'@'localhost' identified by 'passwd';

又报错:ERROR 1819 (HY000): Your password does not satisfy the current policy requirements。原来mysql8预加载强密码策略,准备妥协设置巨复杂的密码算了,但是弱口令用习惯了,数据库基本用来本地访问,要改的配置文件很多,想了想还是改默认密码策略方便。
查看默认策略

show variables like 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password.check_user_name    | ON     |
| validate_password.dictionary_file    |        |
| validate_password.length             | 8      |
| validate_password.mixed_case_count   | 1      |
| validate_password.number_count       | 1      |
| validate_password.policy             | MEDIUM |
| validate_password.special_char_count | 1      |
+--------------------------------------+--------+

密码长度需要8位也就算了,还要有大写字母、小写字母、数字和特殊字符。果断改掉:

set global validate_password.policy=0;
set global validate_password.length=6; 

然后弱口令回来了。
重启数据库,打开网站还是是熟悉的错误:Error establishing a database connection。
查看日志发现:Authentication plugin 'caching_sha2_password' cannot be loaded。
查看密码认证方式;

select user,host,plugin from mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+

原来mysql8 默认密码认证方式改成了caching_sha2_password,php7的mysql扩展不支持这种加密规则。
继续登录数据库,修改加密方式:

alter user 'user'@'localhost' identified with mysql_native_password by 'passwd';

并刷新权限:

flush privileges;

再次打开网站报错:Database Query Error。心中一沉,准备放弃回档,转念一想,原数据库好像没有导入,于是导入备份的原有数据库,打开网站好像正常了,于是重启数据库,再次MySQL Daemon failed to start。
将mysql8 默认数据库再次导入,重启数据库恢复。
但是数据库占用内存巨大,86%MEM,修改以下参数调优:

table_definition_cache=300
performance_schema = OFF
innodb_buffer_pool_chunk_size = 10M
innodb_buffer_pool_size = 10M

恢复正常。

© 2025 | ♥