MySQL數(shù)據(jù)庫升級(jí)那些事
說起MySQL升級(jí),相信很多人都在工作中遇到過.尤其是運(yùn)維和DBA們.那么大家對(duì)MySQL的升級(jí)又有哪些了解呢?我們?yōu)槭裁匆?jí)?我們?cè)趺瓷?jí)?
1 升級(jí)準(zhǔn)備工作
- 官網(wǎng)文檔介紹:https://dev.mysql.com/doc/refman/5.7/en/upgrade-before-you-begin.html
2 升級(jí)注意事項(xiàng)
- a. 支持GA版本之間升級(jí)
- b. 5.6--> 5.7 ,先將5.6升級(jí)至最新版,再升級(jí)到5.7
- c. 5.5 ---> 5.7 ,先將5.5 升級(jí)至最新,再5.5---> 5.6最新,再5.6--
- ->5.7 最新
- d. 回退方案要提前考慮好,最好升級(jí)前要備份(特別是往8.0版本升級(jí))。
- e. 降低停機(jī)時(shí)間(停業(yè)務(wù)的時(shí)間),在業(yè)務(wù)不繁忙期間升級(jí),做好足夠的預(yù)演。
3 升級(jí)方式了解
- 官方文檔介紹:https://dev.mysql.com/doc/refman/5.7/en/upgrade-binary-package.html
- 解釋:升級(jí)方式兩種.一是In-Place Upgrade 二是Logical Upgrade
- In-Place升級(jí)原理:
- a. 安裝新版本軟件
- b. 關(guān)閉原數(shù)據(jù)庫業(yè)務(wù)(掛維護(hù)頁) innodb_fast_shutdown=0
- 備份原數(shù)據(jù)庫數(shù)據(jù)(冷備)
- c. 使用新版本軟件 “掛” 舊版本數(shù)據(jù)啟動(dòng)(--skip-grant-tables ,--
- skip-networking)
- d. 升級(jí) : 只是升級(jí)系統(tǒng)表。升級(jí)時(shí)間和數(shù)據(jù)量無關(guān)的。
- e. 正常重啟數(shù)據(jù)庫。
- f. 驗(yàn)證各項(xiàng)功能是否正常。
- g. 業(yè)務(wù)恢復(fù)。
- 建議: inpalce升級(jí)最好是主從環(huán)境,先從庫再主庫。
- Logical Upgrade升級(jí)原理:
- 1. 使用mysqldump備份全庫數(shù)據(jù)
- 2. 停原庫
- 3. 下載新版MySQL軟件
- 4. 初始化新版MySQL
- 5. 啟動(dòng)新庫
- 6. 把之前備份的數(shù)據(jù)導(dǎo)入新庫
- 目前企業(yè)中一般使用In-Place 方式升級(jí)的比較多,Logical 方式,數(shù)據(jù)量大的話就不合適了,幾個(gè)T的數(shù)據(jù)mysqldump要dump多久,更別提導(dǎo)入庫里了.
- 所以接下來,我將介紹In-Place方式的升級(jí)過程.Logical方式大家可以根據(jù)官方文檔介紹進(jìn)行自己學(xué)習(xí).
4 In-Place方式升級(jí)過程
4.1 由MySQL5616升級(jí)到MySQL5651
- 首先我得環(huán)境是5616版本.要從5616版本升級(jí)到5733,我們需要先把5616升級(jí)到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數(shù)據(jù)庫軟件(略)
- 3 使用高版本軟件掛載低版本數(shù)據(jù)啟動(dòng)
- [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 升級(jí)
- [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
- 現(xiàn)在數(shù)據(jù)庫已經(jīng)由5616升級(jí)到了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 重啟數(shù)據(jù)庫到正常狀態(tài)
- [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>
- 現(xiàn)在數(shù)據(jù)庫版本已經(jīng)由MySQL5616升級(jí)到了MySQL5651
4.2 由MySQL5651升級(jí)到MySQL5733
- 1. 關(guān)閉原庫
- 2. 修改配置文件,指定當(dāng)前basedir為5733的目錄
- 3. 使用高版本軟件帶起低版本數(shù)據(jù)
- [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. 升級(jí)
- [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 重啟數(shù)據(jù)庫到正常狀態(tài)
- [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已經(jīng)由5616升級(jí)到了5733 接下來我們將把MySQL從5733升級(jí)到8021
5 將數(shù)據(jù)庫從5733升級(jí)到8021
MySQL8.0的升級(jí)方式發(fā)生了變化.不再使用mysql_upgrade 而是使用mysql-shell對(duì)升級(jí)前數(shù)據(jù)庫進(jìn)行校驗(yàn).
MySQL升級(jí)8.0和8.0之間的小版本升級(jí)需要注意:升級(jí)前必須備份.因?yàn)?.0不支持回退.
- 1 下載對(duì)應(yīng)要升級(jí)到的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 添加環(huán)境變量
- 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創(chuàng)建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進(jìn)行升級(jí)前的預(yù)檢查
- [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 校驗(yàn)沒問題之后停原庫
- [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的數(shù)據(jù)啟動(dòng)
- [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 后話
當(dāng)然生產(chǎn)環(huán)境中的升級(jí)沒有這么簡(jiǎn)單容易.會(huì)遇到很多問題.當(dāng)然作為一名優(yōu)秀的DBA.我相信大家都會(huì)迎刃而解.
當(dāng)然在升級(jí)過程中尤其要注意sql_mode的變化.也要和業(yè)務(wù)及開發(fā)一起協(xié)同好!