MySQL數據庫升級那些事
作者:智明雜談
說起MySQL升級,相信很多人都在工作中遇到過.尤其是運維和DBA們.那么大家對MySQL的升級又有哪些了解呢?我們為什么要升級?我們怎么升級?
說起MySQL升級,相信很多人都在工作中遇到過.尤其是運維和DBA們.那么大家對MySQL的升級又有哪些了解呢?我們為什么要升級?我們怎么升級?
1 升級準備工作
- 官網文檔介紹:https://dev.mysql.com/doc/refman/5.7/en/upgrade-before-you-begin.html
2 升級注意事項
- a. 支持GA版本之間升級
- b. 5.6--> 5.7 ,先將5.6升級至最新版,再升級到5.7
- c. 5.5 ---> 5.7 ,先將5.5 升級至最新,再5.5---> 5.6最新,再5.6--
- ->5.7 最新
- d. 回退方案要提前考慮好,最好升級前要備份(特別是往8.0版本升級)。
- e. 降低停機時間(停業務的時間),在業務不繁忙期間升級,做好足夠的預演。
3 升級方式了解
- 官方文檔介紹:https://dev.mysql.com/doc/refman/5.7/en/upgrade-binary-package.html
- 解釋:升級方式兩種.一是In-Place Upgrade 二是Logical Upgrade
- In-Place升級原理:
- a. 安裝新版本軟件
- b. 關閉原數據庫業務(掛維護頁) innodb_fast_shutdown=0
- 備份原數據庫數據(冷備)
- c. 使用新版本軟件 “掛” 舊版本數據啟動(--skip-grant-tables ,--
- skip-networking)
- d. 升級 : 只是升級系統表。升級時間和數據量無關的。
- e. 正常重啟數據庫。
- f. 驗證各項功能是否正常。
- g. 業務恢復。
- 建議: inpalce升級最好是主從環境,先從庫再主庫。
- Logical Upgrade升級原理:
- 1. 使用mysqldump備份全庫數據
- 2. 停原庫
- 3. 下載新版MySQL軟件
- 4. 初始化新版MySQL
- 5. 啟動新庫
- 6. 把之前備份的數據導入新庫
- 目前企業中一般使用In-Place 方式升級的比較多,Logical 方式,數據量大的話就不合適了,幾個T的數據mysqldump要dump多久,更別提導入庫里了.
- 所以接下來,我將介紹In-Place方式的升級過程.Logical方式大家可以根據官方文檔介紹進行自己學習.
4 In-Place方式升級過程
4.1 由MySQL5616升級到MySQL5651
- 首先我得環境是5616版本.要從5616版本升級到5733,我們需要先把5616升級到5.6的最新版本5651
- 1 停原庫
- [root@db01 opt]# /usr/local/mysql5616/bin/mysql -S /tmp/mysql5616.sock
- mysql> set global innodb_fast_shutdown=0;
- [root@db01 opt]# /usr/local/mysql5616/bin/mysqladmin -S /tmp/mysql5616.sock shutdown
- [root@db01 opt]# 210704 06:46:15 mysqld_safe mysqld from pid file /data/5616/data/db01.pid ended
- 2 下載5733數據庫軟件(略)
- 3 使用高版本軟件掛載低版本數據啟動
- [root@db01 opt]# /usr/local/mysql5651/bin/mysqld_safe --defaults-file=/data/5616/my.cnf --skip-grant-tables --skip-networking &
- [4] 11802
- [root@db01 opt]# 210704 07:15:27 mysqld_safe Logging to '/data/5616/data/db01.err'.
- 210704 07:15:27 mysqld_safe Starting mysqld daemon with databases from /data/5616/data
- 4 升級
- [root@db01 opt]# /usr/local/mysql5651/bin/mysql_upgrade -S /tmp/mysql5616.sock --force
- Looking for 'mysql' as: /usr/local/mysql5651/bin/mysql
- Looking for 'mysqlcheck' as: /usr/local/mysql5651/bin/mysqlcheck
- Running 'mysqlcheck' with connection arguments: '--socket=/tmp/mysql5616.sock'
- Running 'mysqlcheck' with connection arguments: '--socket=/tmp/mysql5616.sock'
- mysql.columns_priv OK
- mysql.db OK
- mysql.event OK
- mysql.func OK
- mysql.general_log 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.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
- Running 'mysql_fix_privilege_tables'...
- Running 'mysqlcheck' with connection arguments: '--socket=/tmp/mysql5616.sock'
- Running 'mysqlcheck' with connection arguments: '--socket=/tmp/mysql5616.sock'
- OK
- 現在數據庫已經由5616升級到了5651
- [root@db01 opt]# /usr/local/mysql5651/bin/mysql -S /tmp/mysql5616.sock
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 9
- Server version: 5.6.51 MySQL Community Server (GPL)
- Copyright (c) 2000, 2021, 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>
- 5 重啟數據庫到正常狀態
- [root@db01 opt]# /usr/local/mysql5651/bin/mysqladmin -S /tmp/mysql5616.sock shutdown
- 210704 07:22:22 mysqld_safe mysqld from pid file /data/5616/data/db01.pid ended
- [4]+ Done /usr/local/mysql5651/bin/mysqld_safe --defaults-file=/data/5616/my.cnf
- [root@db01 opt]# /usr/local/mysql5651/bin/mysqld_safe --defaults-file=/data/5616/my.cnf &
- [4] 12006
- [root@db01 opt]# 210704 07:22:37 mysqld_safe Logging to '/data/5616/data/db01.err'.
- 210704 07:22:37 mysqld_safe Starting mysqld daemon with databases from /data/5616/data
- [root@db01 opt]# /usr/local/mysql5651/bin/mysql -S /tmp/mysql5616.sock
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 1
- Server version: 5.6.51 MySQL Community Server (GPL)
- Copyright (c) 2000, 2021, 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>
- 現在數據庫版本已經由MySQL5616升級到了MySQL5651
4.2 由MySQL5651升級到MySQL5733
- 1. 關閉原庫
- 2. 修改配置文件,指定當前basedir為5733的目錄
- 3. 使用高版本軟件帶起低版本數據
- [root@db01 opt]# /usr/local/mysql5733/bin/mysqld_safe --defaults-file=/data/5616/my.cnf --skip-grant-tables --skip-networking &
- [4] 12193
- [root@db01 opt]# 2021-07-04T11:28:57.280601Z mysqld_safe Logging to '/data/5616/data/db01.err'.
- 2021-07-04T11:28:57.337826Z mysqld_safe Starting mysqld daemon with databases from /data/5616/data
- 4. 升級
- [root@db01 opt]# /usr/local/mysql5733/bin/mysql_upgrade -S /tmp/mysql5616.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
- Upgrade process completed successfully.
- Checking if update is needed.
- 5 重啟數據庫到正常狀態
- [root@db01 opt]# /usr/local/mysql5733/bin/mysqladmin -S /tmp/mysql
- mysql5616.sock mysql5616.sock.lock mysql5733.sock mysql5733.sock.lock mysql8021.sock mysql8021.sock.lock mysqlx.sock mysqlx.sock.lock
- [root@db01 opt]# /usr/local/mysql5733/bin/mysqladmin -S /tmp/mysql
- mysql5616.sock mysql5616.sock.lock mysql5733.sock mysql5733.sock.lock mysql8021.sock mysql8021.sock.lock mysqlx.sock mysqlx.sock.lock
- [root@db01 opt]# /usr/local/mysql5733/bin/mysqladmin -S /tmp/mysql5616.sock shutdown
- 2021-07-04T11:31:39.620201Z mysqld_safe mysqld from pid file /data/5616/data/db01.pid ended
- [4]+ Done /usr/local/mysql5733/bin/mysqld_safe --defaults-file=/data/5616/my.cnf --skip-grant-tables --skip-networking
- [root@db01 opt]# /usr/local/mysql5733/bin/mysqld_safe --defaults-file=/data/5616/my.cnf &
- [4] 12431
- [root@db01 opt]# 2021-07-04T11:31:52.666976Z mysqld_safe Logging to '/data/5616/data/db01.err'.
- 2021-07-04T11:31:52.727277Z mysqld_safe Starting mysqld daemon with databases from /data/5616/data
- [root@db01 opt]# /usr/local/mysql5733/bin/mysql -S /tmp/mysql5616.sock
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 2
- Server version: 5.7.33 MySQL Community Server (GPL)
- Copyright (c) 2000, 2021, Oracle and/or its affiliates.
- 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已經由5616升級到了5733 接下來我們將把MySQL從5733升級到8021
5 將數據庫從5733升級到8021
MySQL8.0的升級方式發生了變化.不再使用mysql_upgrade 而是使用mysql-shell對升級前數據庫進行校驗.
MySQL升級8.0和8.0之間的小版本升級需要注意:升級前必須備份.因為8.0不支持回退.
- 1 下載對應要升級到的8.0版本的mysql-shell https://downloads.mysql.com/archives/shell/
- 2 解壓做軟連接
- tar xf mysql-shell-8.0.21-linux-glibc2.12-x86-64bit.tar.gz
- ln -s /opt/mysql-shell-8.0.21-linux-glibc2.12-x86-64bit /usr/local/mysqlsh
- 2 添加環境變量
- vim /etc/profile
- export PATH=/usr/local/mysqlsh/bin:$PATH
- [root@db01 opt]# source /etc/profile
- [root@db01 opt]# mysqlsh -V
- mysqlsh Ver 8.0.21 for Linux on x86_64 - for MySQL 8.0.21 (MySQL Community Server (GPL))
- 3 連接到5733創建mysql-shell的連接用戶
- [root@db01 opt]# /usr/local/mysql5733/bin/mysql -S /tmp/mysql5616.sock
- mysql> grant all on *.* to root@'10.0.0.%' identified by '123';
- 4 使用mysql-shell進行升級前的預檢查
- [root@db01 opt]# mysqlsh root:123@10.0.0.110:3307 -e "util.checkForServerUpgrade()" >/tmp/up.log
- WARNING: Using a password on the command line interface can be insecure.
- [root@db01 opt]# cat /tmp/up.log
- The MySQL server at 10.0.0.110:3307, version 5.7.33 - MySQL Community Server
- (GPL), will now be checked for compatibility issues for upgrade to MySQL
- 8.0.21...
- 1) Usage of old temporal type
- No issues found
- 2) Usage of db objects with names conflicting with new reserved keywords
- No issues found
- 3) Usage of utf8mb3 charset
- No issues found
- 4) Table names in the mysql schema conflicting with new tables in 8.0
- No issues found
- 5) Partitioned tables using engines with non native partitioning
- No issues found
- 6) Foreign key constraint names longer than 64 characters
- No issues found
- 7) Usage of obsolete MAXDB sql_mode flag
- No issues found
- 8) Usage of obsolete sql_mode flags
- No issues found
- 9) ENUM/SET column definitions containing elements longer than 255 characters
- No issues found
- 10) Usage of partitioned tables in shared tablespaces
- No issues found
- 11) Circular directory references in tablespace data file paths
- No issues found
- 12) Usage of removed functions
- No issues found
- 13) Usage of removed GROUP BY ASC/DESC syntax
- No issues found
- 14) Removed system variables for error logging to the system log configuration
- To run this check requires full path to MySQL server configuration file to be specified at 'configPath' key of options dictionary
- More information:
- https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-13.html#mysqld-8-0-13-logging
- 15) Removed system variables
- To run this check requires full path to MySQL server configuration file to be specified at 'configPath' key of options dictionary
- More information:
- https://dev.mysql.com/doc/refman/8.0/en/added-deprecated-removed.html#optvars-removed
- 16) System variables with new default values
- To run this check requires full path to MySQL server configuration file to be specified at 'configPath' key of options dictionary
- More information:
- https://mysqlserverteam.com/new-defaults-in-mysql-8-0/
- 17) Zero Date, Datetime, and Timestamp values
- No issues found
- 18) Schema inconsistencies resulting from file removal or corruption
- No issues found
- 19) Tables recognized by InnoDB that belong to a different engine
- No issues found
- 20) Issues reported by 'check table x for upgrade' command
- No issues found
- 21) New default authentication plugin considerations
- Warning: The new default authentication plugin 'caching_sha2_password' offers
- more secure password hashing than previously used 'mysql_native_password'
- (and consequent improved client connection authentication). However, it also
- has compatibility implications that may affect existing MySQL installations.
- If your MySQL installation must serve pre-8.0 clients and you encounter
- compatibility issues after upgrading, the simplest way to address those
- issues is to reconfigure the server to revert to the previous default
- authentication plugin (mysql_native_password). For example, use these lines
- in the server option file:
- [mysqld]
- default_authentication_plugin=mysql_native_password
- However, the setting should be viewed as temporary, not as a long term or
- permanent solution, because it causes new accounts created with the setting
- in effect to forego the improved authentication security.
- If you are using replication please take time to understand how the
- authentication plugin changes may impact you.
- More information:
- https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password-compatibility-issues
- https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password-replication
- Errors: 0
- Warnings: 1
- Notices: 0
- 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.
- 5 校驗沒問題之后停原庫
- [root@db01 opt]# /usr/local/mysql5733/bin/mysql -S /tmp/mysql5616.sock
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 6
- Server version: 5.7.33 MySQL Community Server (GPL)
- mysql> set global innodb_fast_shutdown=0;
- mysql> shutdown;
- 6 修改配置文件中程序目錄路徑為8.0的程序路徑
- [root@db01 opt]# cat /data/5616/my.cnf
- [mysqld]
- user=mysql
- basedir=/usr/local/mysql8021
- datadir=/data/5616/data
- socket=/tmp/mysql5616.sock
- server_id=56
- port=3307
- 7 使用8.0的軟件掛載5.7的數據啟動
- [root@db01 opt]# /usr/local/mysql8021/bin/mysqld_safe --defaults-file=/data/5616/my.cnf &
- [4] 12714
- [root@db01 opt]# 2021-07-04T11:53:53.629634Z mysqld_safe Logging to '/data/5616/data/db01.err'.
- 2021-07-04T11:53:53.686412Z mysqld_safe Starting mysqld daemon with databases from /data/5616/data
- [root@db01 opt]# /usr/local/mysql8021/bin/mysql -S /tmp/mysql5616.sock
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 11
- Server version: 8.0.21 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>
6 后話
當然生產環境中的升級沒有這么簡單容易.會遇到很多問題.當然作為一名優秀的DBA.我相信大家都會迎刃而解.
當然在升級過程中尤其要注意sql_mode的變化.也要和業務及開發一起協同好!
責任編輯:武曉燕
來源:
今日頭條