MySQL5.7升级到8.0

1.1 升级说明

MySQL升级:MySQL中不存在打补丁的说法,MySQL的所谓的补丁就是升级到一个新版本,然后把原始数据拉起来进行升级。

1.2 升级方式

1.2.1 inplace就地升级

在一台服务器上,进行原版本升级到新版本,风险较大。如果是主从环境可以先就地升级从库,然后再升级主库,滚动方式升级。

1.2.2 逻辑迁移升级

准备新的服务器,然后将数据从源库中逻辑导出,然后再导入到新的数据库中,数据量大的时候,会很慢。例如:
如果是一主一从(主->从1),在有条件的情况下,可以新准备一台服务器,采用物理备份的方式将数据备份恢复到新的服务中,然后构建从库的从库(主->从1->从2),最后将从2进行inplace方式升级,然后进行业务验证,验证完成后构建主->从2。升级从1,将(主->从1)的从1断开,从1升级完成后,构建(主->从1,主->从2),此时可以升级主库,主库停止写业务,主从关系变更为(从1->从2)原从1承担写库,待主库完成升级后重新加入主从即可。

1.2.3 前提建议

1.不管哪种方式升级,都先冷备全量数据,包括日志,便于回退。
2.升级之前一定要充分的测试验证,包含业务验证。

1.2.4 升级注意事项

1.支持GA版本之间的升级,不支持跨版本升级。
2.5.6升级到5.7时,先将5.6升级到最新版本,然后再升级到5.7
3.5.6升级5.7时,先将5.5升级到最新版本,然后从5.5的最新版本升级到5.6最新版本,最后从5.6最新版本升级到5.7最新版本
4.回退方案提前准备好,做好充足的演练;墙裂建议做好备份,尤其是升级到8.0最新版本时
5.降低停机时间,在业务不繁忙的月黑风高的后半夜升级。
6.升级过程中需要重启节点,

1.3 inplace升级过程(生产思路)

1.部署新版本的数据库软件
2. 设置参数:innodb_fast_shutdown=1,然后关闭数据库。 #表示不干净的关闭数据库,建议设置0,表示干净的关闭,该落盘的数据都落盘
3.冷备份数据库数据
4.使用新版本数据库,拉起旧版本的数据进行启动,参数(--skip-grant-tables --skip-networking
5.启动完成后,验证业务功能
6.恢复业务,升级完成。

1.4 5.7inplace升级到8.0

5.7.32升级到8.0.24。
注意:注意备份,冷备!

1.4.1 准备工作

1.预检查,8.0新特性:mysql8.0有检查工具:mysql-shell,升级前通过该工具检查当前版本是否具备条件升级到8.0,下载:https://downloads.mysql.com/archives/shell/
也可以yum安装:
yum install -y mysql-shell-8.0.24-1.el7.x86_64.rpm
注意:要升级到8.0的哪个版本,建议下载哪个版本的mysql-shell
2.部署mysql-shell

[root@localhost local]# tar -xf mysql-shell-8.0.24-linux-glibc2.12-x86-64bit.tar.gz
[root@localhost local]# ln -s /usr/local/mysql-shell-8.0.24-linux-glibc2.12-x86-64bit /usr/local/mysqlsh

配置环境变量:

[root@localhost local]# vim /etc/profile
export PATH=/usr/local/mysqlsh/bin:$PATH

3.检查
命令:

mysqlsh root:123@10.0.0.51:3307 -e "util.checkForServerUpgrade()"

mysqlsh -uroot -p123 -S /tmp/mysql.sock -e "util.checkForServerUpgrade()"

例如:

[root@localhost ~]# mysqlsh -uroot -p123 -S /tmp/mysql.sock -e "util.checkForServerUpgrade()"
输出信息省略
Errors:   0  # 检查是否有错误,如果没有,则可以进行升级
Warnings: 1
Notices:  1

No fatal errors were found that would prevent an upgrade, but some potential issues were detected. Please ensure that the reported issues are not significant before upgrading.
[root@localhost ~]# 
1.4.2 部署新版本的MySQL(8.0)
[root@localhost local]# tar -xf mysql-8.0.24-linux-glibc2.12-x86_64.tar.xz 
[root@localhost local]# ln -s /usr/local/mysql-8.0.24-linux-glibc2.12-x86_64/ mysql8
1.4.3 停服务

设置:

mysql> select @@innodb_fast_shutdown;
+------------------------+
| @@innodb_fast_shutdown |
+------------------------+
|                      1 |
+------------------------+
1 row in set (0.00 sec)

mysql> set global innodb_fast_shutdown=0;
Query OK, 0 rows affected (0.05 sec)

停服务:

[root@localhost ~]# /usr/local/mysql57/bin/mysqladmin -uroot -p123 -S /tmp/mysql.sock shutdown
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      14827/sshd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      6526/master         
tcp6       0      0 :::22                   :::*                    LISTEN      14827/sshd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      6526/master         
[root@localhost ~]# 
[root@localhost ~]# ps -ef|grep mysql
root      19027  14789  0 18:15 pts/2    00:00:00 grep --color=auto mysql
[root@localhost ~]# 
1.4.4 备份数据

1.冷备份
如果数据和日志都需要备份

[root@localhost data]# cp -r 3306 3306_bak # 生产环境中建议把数据单独其他盘
1.4.5 升级

1.使用8.0版本软件挂5.7版本数据启动
命令:

/usr/local/mysql8/bin/mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables --skip-networking &

例如:

[root@localhost ~]# /usr/local/mysql8/bin/mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables --skip-networking &
[1] 19270
[root@localhost ~]# 2022-01-03T10:19:13.979712Z mysqld_safe Logging to '/data/3306/data/localhost.localdomain.err'.
2022-01-03T10:19:14.032070Z mysqld_safe Starting mysqld daemon with databases from /data/3306/data

[root@localhost ~]# mysql  # 连接测试
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.24 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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> 

1.4.6 重启服务
1.升级完成后,还处理安全模式,因为在拉起数据的库的时候加了参数“--skip-grant-tables --skip-networking”,此时需要重启数据库,且配置文件中的basedir可以调整成新版本的
修改配置:

[root@localhost ~]# cat /etc/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql8 # 由之前的57版本改成8
datadir=/data/3306/data
server_id=56
port=3306
socket=/tmp/mysql.sock
default_authentication_plugin=mysql_native_password
[root@localhost ~]# 

2.重启服务

[root@localhost ~]# /usr/local/mysql8/bin/mysqladmin -uroot -p123 -S /tmp/mysql.sock shutdown  # 关闭服务
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# 2022-01-03T10:28:45.204366Z mysqld_safe mysqld from pid file /data/3306/data/localhost.localdomain.pid ended

[1]+  Done                    /usr/local/mysql8/bin/mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables --skip-networking
[root@localhost ~]# 
[root@localhost ~]# /etc/init.d/mysqld start  # 启动
Starting MySQL. SUCCESS! 
[root@localhost ~]# netstat -lntup  # 8.0中MySQL的服务端口已监听
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      14827/sshd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      6526/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      19772/mysqld        
tcp6       0      0 :::22                   :::*                    LISTEN      14827/sshd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      6526/master         
tcp6       0      0 :::33060                :::*                    LISTEN      19772/mysqld        
[root@localhost ~]# ps -ef|grep mysql
root      19592      1  0 18:29 pts/0    00:00:00 /bin/sh /usr/local/mysql8/bin/mysqld_safe --datadir=/data/3306/data --pid-file=/data/3306/data/localhost.localdomain.pid
mysql     19772  19592  5 18:29 pts/0    00:00:00 /usr/local/mysql8/bin/mysqld --basedir=/usr/local/mysql8 --datadir=/data/3306/data --plugin-dir=/usr/local/mysql8/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/data/3306/data/localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306
root      19818  14763  0 18:29 pts/0    00:00:00 grep --color=auto mysql
[root@localhost ~]# 

1.4.7 验证

[root@localhost ~]# mysql -uroot -p123 # 登录连接
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.24 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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> use sysbenchdb57
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from sbtest1 limit 3;  # 查询验证
+----+------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
| id | k    | c                                                                                                                       | pad                                                         |
+----+------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
|  1 |  125 | 50739423477-59896895752-91121550334-25071371310-03454727381-25307272676-12883025003-48844794346-97662793974-67443907837 | 10824941535-62754685647-36430831520-45812593797-70371571680 |
|  2 | 4909 | 49539573485-04290970034-07007898786-76235712409-63549516919-38567537726-61464371934-73041702018-68090018268-15038237444 | 46626061119-67478129051-74112140298-15060467792-43446447884 |
|  3 | 4641 | 89102349959-12177005240-27388430679-87816169622-26709772977-24906799335-28199650567-01171976601-99969718852-51664004083 | 14337533566-48142312521-67057187519-35890983782-75782102604 |
+----+------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
3 rows in set (0.00 sec)
1.4.8 小结

1.升级到8.0不需要手动执行mysql_upgrade命令
2.升级前一定得备份,尤其是升级到8.0,因为无法回退
3.8.0中的数据文件有undo_001/2这两个文件及mysql.ibd文件。如:

[root@localhost data]# ls -ltr undo_00*
-rw-r----- 1 mysql mysql 16777216 Jan  3 18:31 undo_002
-rw-r----- 1 mysql mysql 16777216 Jan  3 18:31 undo_001
[root@localhost data]# ls -ltr mysql.i*
-rw-r----- 1 mysql mysql 29360128 Jan  3 18:29 mysql.ibd
[root@localhost data]#
  1. 如果有如下报错:
[root@localhost ~]# mysql
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/local/mysql/lib/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
[root@localhost ~]# 

在配置文件/etc/my.cnf中添加:

[mysqld]
default_authentication_plugin=mysql_native_password	
  1. 升级到8.0时,注意默认密码加密方式由原来的mysql_native_password变成caching_sha2_passord,在升级前一定要进行充分的测
  2. 8.0有很多新特性,慢慢体验吧
posted @ 2022-01-04 22:52  红桃Z  阅读(3150)  评论(0编辑  收藏  举报