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]#
- 如果有如下報錯:
[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
- 升級到8.0時,注意默認密碼加密方式由原來的mysql_native_password變成caching_sha2_passord,在升級前一定要進行充分的測
- 8.0有很多新特性,慢慢體驗吧