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.6inplace升級到5.7
5.6.50升級到5.7.32。
[root@localhost ~]# mysql --version
mysql Ver 14.14 Distrib 5.6.50, for linux-glibc2.12 (x86_64) using EditLine wrapper
1.4.1 部署要新版本的MySQL(5.7)
1.解壓文件
tar xf mysql-5.6.50-linux-glibc2.12-x86_64.tar.gz
ln -s /usr/local/mysql-5.7.32-linux-glibc2.12-x86_64 mysql57
1.4.2 停服務
1停止業務發起新連接到數據庫
停止前先斷開業務,show processlist;
查看當前連接情況,如果連接比較多,需要手工殺掉,可以拼接語句批量殺掉
拼接語句:
select concat("kill ",id,";") from information_schema.processlist;
2.調整快速關閉參數
set global innodb_fast_shutdown=0;
例如:
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.00 sec)
mysql> select @@innodb_fast_shutdown;
+------------------------+
| @@innodb_fast_shutdown |
+------------------------+
| 0 |
+------------------------+
1 row in set (0.00 sec)
3.關閉服務
如果是多實例,可以使用該關閉方法:mysqladmin -S /tmp/mysql56.sock shutdown
單實例關閉:/etc/init.d/mysqld stop
關閉服務:
[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 :::3306 :::* LISTEN 16824/mysqld
tcp6 0 0 :::22 :::* LISTEN 14827/sshd
tcp6 0 0 ::1:25 :::* LISTEN 6526/master
[root@localhost ~]# ps -ef|grep mysql
root 16646 1 0 15:43 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/3306/data --pid-file=/data/3306/data/localhost.localdomain.pid
mysql 16824 16646 0 15:43 pts/0 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/data/3306/data/localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306
root 16855 14763 0 15:52 pts/0 00:00:00 grep --color=auto mysql
[root@localhost ~]# /etc/init.d/mysqld stop # 停止服務
Shutting down MySQL.. SUCCESS!
[root@localhost ~]# ps -ef|grep mysql
root 16878 14763 0 15:53 pts/0 00:00:00 grep --color=auto mysql
[root@localhost ~]#
[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 ~]#
1.4.3 備份數據
1.冷備份
如果數據和日志都需要備份
[root@localhost data]# cp -r 3306 3306_bak # 生產環境中建議把數據單獨其他盤
1.4.4 升級
1.4.4.1 升級前
1.使用5.7服務,拉起5.6的數據,然后升級
/usr/local/mysql57/bin/mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables --skip-networking &
--defaults-file=/etc/my.cnf f # 為了讀取5.6的數據
[root@localhost ~]# mysql # mysql 登錄驗證 此時啟動成功,可以登錄,但並未完成升級
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.32 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> show databases; # 升級前5.6沒有sys庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sysbenchdb |
| test |
+--------------------+
5 rows in set (0.01 sec)
mysql>
1.4.4.2 升級
升級:
/usr/local/mysql57/bin/mysql_upgrade -S /tmp/mysql.sock --force # 該操作是升級系統表
一堆ok
例如:
[root@localhost ~]# /usr/local/mysql57/bin/mysql_upgrade -S /tmp/mysql.sock --force
Checking server version.
Running queries to upgrade MySQL server.
Checking system database.
mysql.columns_priv OK
mysql.db OK
mysql.engine_cost OK
mysql.event OK
mysql.func OK
mysql.general_log OK
mysql.gtid_executed OK
mysql.help_category OK
mysql.help_keyword OK
mysql.help_relation OK
mysql.help_topic OK
mysql.innodb_index_stats OK
mysql.innodb_table_stats OK
mysql.ndb_binlog_index OK
mysql.plugin OK
mysql.proc OK
mysql.procs_priv OK
mysql.proxies_priv OK
mysql.server_cost OK
mysql.servers OK
mysql.slave_master_info OK
mysql.slave_relay_log_info OK
mysql.slave_worker_info OK
mysql.slow_log OK
mysql.tables_priv OK
mysql.time_zone OK
mysql.time_zone_leap_second OK
mysql.time_zone_name OK
mysql.time_zone_transition OK
mysql.time_zone_transition_type OK
mysql.user OK
Upgrading the sys schema.
Checking databases.
sys.sys_config OK
sysbenchdb.sbtest1 OK
sysbenchdb.sbtest10 OK
sysbenchdb.sbtest11 OK
sysbenchdb.sbtest12 OK
sysbenchdb.sbtest13 OK
sysbenchdb.sbtest14 OK
sysbenchdb.sbtest15 OK
sysbenchdb.sbtest16 OK
sysbenchdb.sbtest17 OK
sysbenchdb.sbtest18 OK
sysbenchdb.sbtest19 OK
sysbenchdb.sbtest2 OK
sysbenchdb.sbtest20 OK
sysbenchdb.sbtest3 OK
sysbenchdb.sbtest4 OK
sysbenchdb.sbtest5 OK
sysbenchdb.sbtest6 OK
sysbenchdb.sbtest7 OK
sysbenchdb.sbtest8 OK
sysbenchdb.sbtest9 OK # 應用表
Upgrade process completed successfully.
Checking if update is needed.
1.4.4.3 升級后
show databases;
查看是否有sys庫。查看user表是否有authentication_string字段
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.32 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>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys | # 此時有sys庫
| sysbenchdb |
| test |
+--------------------+
6 rows in set (0.00 sec)
1.4.5 重啟服務
重啟數據庫:
升級完成后,還處理安全模式,因為在拉起數據的庫的時候加了參數“--skip-grant-tables --skip-networking”,此時需要重啟數據庫
此時配置文件中的basedir可以調整成新版本的
修改配置:
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql57 # 由之前的56版本改成57
datadir=/data/3306/data
server_id=56
port=3306
socket=/tmp/mysql.sock
lower_case_table_names=1
重啟服務
[root@localhost ~]# /etc/init.d/mysqld restart # 重啟服務
Shutting down MySQL..2022-01-03T08:28:21.739297Z mysqld_safe mysqld from pid file /data/3306/data/localhost.localdomain.pid ended
SUCCESS!
Starting MySQL. SUCCESS!
[1]+ Done /usr/local/mysql57/bin/mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables --skip-networking
[root@localhost ~]#
[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 :::3306 :::* LISTEN 16824/mysqld
tcp6 0 0 :::22 :::* LISTEN 14827/sshd
tcp6 0 0 ::1:25
以下方式啟動也可以:
/usr/local/mysql57/bin/mysqld_safe --defaults-file=/etc/my.cnf &
1.4.6 驗證
啟動完成后,驗證業務
[root@localhost ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[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 3
Server version: 5.7.32 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> show databses;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databses' at line 1
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| sysbenchdb |
| test |
+--------------------+
6 rows in set (0.01 sec)
mysql> use sysbenchdb;
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 | 3231 | 68487932199-96439406143-93774651418-41631865787-96406072701-20604855487-25459966574-28203206787-41238978918-19503783441 | 22195207048-70116052123-74140395089-76317954521-98694025897 |
| 2 | 557 | 13241531885-45658403807-79170748828-69419634012-13605813761-77983377181-01582588137-21344716829-87370944992-02457486289 | 28733802923-10548894641-11867531929-71265603657-36546888392 |
| 3 | 2758 | 16516882386-05118562259-88939752278-62800788230-55205008755-06868633780-74894238647-69655573455-70526404237-73358617781 | 73198647949-50059256035-48039302709-77824424754-93913530645 |
+----+------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
3 rows in set (0.00 sec)
注意:升級只是升級了系統表,和數據量沒有關系。
1.5小結
1.升級前也可以單獨把所有的表結構進行備份
2.生產環境中建議把數據單獨其他盤
生產環境中操作步驟建議復制粘貼執行,手敲容易出錯
5.重啟時,不建議直接殺進程!!
4.5.6中沒有sys庫,升級后檢查庫中是否由sys庫
2.1 回退
1.調整配置文件
[root@localhost ~]# cat /etc/my56.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql # 56的應用
datadir=/data/3306_bak/data/ # 備份的數據
server_id=3356
port=3356 # 新起一個端口,生產用原來端口即可
socket=/tmp/mysql56.sock
[root@localhost ~]#
2.修改備份數據屬主
[root@localhost data]# chown -R mysql.mysql 3306_bak
[root@localhost data]# ls -ld 3306_bak
drwxr-xr-x 3 mysql mysql 18 Jan 3 16:23 3306_bak
3.啟動服務
[root@localhost ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my56.cnf &
[1] 20111
[root@localhost ~]# 220103 23:26:07 mysqld_safe Logging to '/data/3306_bak/data/localhost.localdomain.err'.
220103 23:26:07 mysqld_safe Starting mysqld daemon with databases from /data/3306_bak/data
4.檢查:
[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
tcp6 0 0 :::3356 :::* LISTEN 20250/mysqld
[root@localhost ~]# # 3356 端口已監聽
驗證
[root@localhost ~]# mysql -uroot -S /tmp/mysql56.sock
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@localhost ~]# mysql -uroot -p -S /tmp/mysql56.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.50 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sysbenchdb |
| test |
+--------------------+
5 rows in set (0.02 sec)
mysql> use sysbenchdb
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 1;
+----+------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
| id | k | c | pad |
+----+------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
| 1 | 3231 | 68487932199-96439406143-93774651418-41631865787-96406072701-20604855487-25459966574-28203206787-41238978918-19503783441 | 22195207048-70116052123-74140395089-76317954521-98694025897 |
+----+------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
2.2 小結
1.生產環境中做好數據、日志備份,及my.cnf的備份,回退的時候也會比較方便快速。