一、概述
MySQL升级5.6到5.7的方式有两种,分别为:
In-Place Upgrade
Logical Upgrade
具体参考官网链接:https://dev.mysql.com/doc/refman/5.7/en/upgrading-strategies.html
二、升级操作
以In-Place方式即物理升级为例,MySQL版本分别为 5.6.27 和 5.7.13。
具体过程如下:
使用xtrabackup对5.6的数据进行一次全备
安装MySQL5.7,将5.6备份的数据文件拷贝到5.7的data目录下
在5.7中对5.6的数据文件进行升级
升级完成后将5.7作为从库同步线上的5.6
找合适的时间切换到5.7的从库上
1. 环境准备
搭建5.7实例,并使用5.6实例的备份替换5.7的data目录
mv data data_20180405 #移走5.7库data
mv 56_back_20180405 data #移入5.6库备份文件
chown -R mysql.mysql data
2. 升级数据文件
开启5.7实例
./bin/mysqld_safe --defaults-file=./etc/mysql.cnf &
启动后会发现此时5.7实例已经可以登录,但是在error日志中会有很多error:
[ERROR] Incorrect definition of table performance_schema.events_waits_current: expected column 'NESTING_EVENT_TYPE' at position 15 to have type enum('TRANSACTION','STATEMENT','STAGE','WAIT', found type enum('STATEMENT','STAGE','WAIT'). [ERROR] Incorrect definition of table performance_schema.events_waits_summary_by_user_by_event_name: expected column 'USER' at position 0 to have type char(32), found type char(16). [ERROR] Column count of performance_schema.table_lock_waits_summary_by_table is wrong. Expected 68, found 73. Created with MySQL 50627, now running 50713. Please use mysql_upgrade to fix this error. [ERROR] mysql.user has no `Event_priv` column at position 28
[ERROR] Native table 'performance_schema'.'session_variables' has the wrong structure ...
下面使用5.7实例对5.6数据文件进行升级:
./bin/mysql_upgrade --defaults-extra-file=./etc/user.root.cnf
该操作会check系统表并进行升级,并且check所有库表兼容性。
3. 重启5.7实例
确保变更生效。
./bin/mysqladmin --defaults-extra-file=./etc/user.root.cnf shutdown ./bin/mysqld_safe --defaults-file=./etc/mysql.cnf &
此时查看error日志,会发现这次的启动过程正常了,没有出现报错。
至此5.6实例的数据文件已经被成功升级。
4. 建立主从同步进行切换
由于5.7使用的是5.6的全备,所以可以通过指定 xtrabackup_binlog_info 中的同步点来建立同步,5.7可以作为5.6的从库。
同步检查没问题后,便可以找合适的时间来进行切换了。切换相关的具体步骤就不再详细表述,选择在业务低峰期使用MHA或手动切换都可以。
三、相关问题
1. Cannot setup server variables
在使用mysql_upgrade进行升级时遇到:
./bin/mysql_upgrade --defaults-extra-file=./etc/user.root.cnf Error occurred: Cannot setup server variables.
这是因为我此时使用的账号是一个普通账号,没有super权限,在更换为具有super权限的账号后问题解决。
四、后记
MySQL官网上提供的方法是shutdown 5.6 ,替换二进制包和命令等为5.7的版本,然后进行升级操作,但是这种方法需要shutdown DB,可能无法适用于线上,因此反过来要更好一些。