MySQL数据库升级那些事

数据库 MySQL
说起MySQL升级,相信很多人都在工作中遇到过.尤其是运维和DBA们.那么大家对MySQL的升级又有哪些了解呢?我们为什么要升级?我们怎么升级?

 [[410420]]

说起MySQL升级,相信很多人都在工作中遇到过.尤其是运维和DBA们.那么大家对MySQL的升级又有哪些了解呢?我们为什么要升级?我们怎么升级?

1 升级准备工作

  1. 官网文档介绍:https://dev.mysql.com/doc/refman/5.7/en/upgrade-before-you-begin.html 

2 升级注意事项

  1. a. 支持GA版本之间升级 
  2. b. 5.6--> 5.7 ,先将5.6升级至最新版,再升级到5.7 
  3. c. 5.5 ---> 5.7 ,先将5.5 升级至最新,再5.5---> 5.6最新,再5.6-- 
  4. ->5.7 最新 
  5. d. 回退方案要提前考虑好,最好升级前要备份(特别是往8.0版本升级)。 
  6. e. 降低停机时间(停业务的时间),在业务不繁忙期间升级,做好足够的预演。 

3 升级方式了解

  1. 官方文档介绍:https://dev.mysql.com/doc/refman/5.7/en/upgrade-binary-package.html 
  2. 解释:升级方式两种.一是In-Place Upgrade 二是Logical Upgrade 
  3. In-Place升级原理: 
  4. a.  安装新版本软件 
  5. b.  关闭原数据库业务(挂维护页) innodb_fast_shutdown=0 
  6. 备份原数据库数据(冷备) 
  7. c.  使用新版本软件 “挂” 旧版本数据启动(--skip-grant-tables ,-- 
  8. skip-networking) 
  9. d.  升级 : 只是升级系统表。升级时间和数据量无关的。 
  10. e.  正常重启数据库。 
  11. f.  验证各项功能是否正常。 
  12. g.  业务恢复。 
  13.  
  14. 建议: inpalce升级最好是主从环境,先从库再主库。 
  15.  
  16. Logical Upgrade升级原理: 
  17. 1. 使用mysqldump备份全库数据 
  18. 2. 停原库 
  19. 3. 下载新版MySQL软件 
  20. 4. 初始化新版MySQL 
  21. 5. 启动新库 
  22. 6. 把之前备份的数据导入新库 
  23.  
  24. 目前企业中一般使用In-Place 方式升级的比较多,Logical 方式,数据量大的话就不合适了,几个T的数据mysqldump要dump多久,更别提导入库里了. 
  25. 所以接下来,我将介绍In-Place方式的升级过程.Logical方式大家可以根据官方文档介绍进行自己学习. 

4 In-Place方式升级过程

4.1 由MySQL5616升级到MySQL5651

  1. 首先我得环境是5616版本.要从5616版本升级到5733,我们需要先把5616升级到5.6的最新版本5651 
  2. 1 停原库 
  3. [root@db01 opt]# /usr/local/mysql5616/bin/mysql -S /tmp/mysql5616.sock  
  4. mysql> set global innodb_fast_shutdown=0; 
  5. [root@db01 opt]# /usr/local/mysql5616/bin/mysqladmin -S /tmp/mysql5616.sock shutdown 
  6. [root@db01 opt]# 210704 06:46:15 mysqld_safe mysqld from pid file /data/5616/data/db01.pid ended 
  7. 2 下载5733数据库软件(略) 
  8. 3  使用高版本软件挂载低版本数据启动 
  9. [root@db01 opt]# /usr/local/mysql5651/bin/mysqld_safe --defaults-file=/data/5616/my.cnf --skip-grant-tables --skip-networking & 
  10. [4] 11802 
  11. [root@db01 opt]# 210704 07:15:27 mysqld_safe Logging to '/data/5616/data/db01.err'
  12. 210704 07:15:27 mysqld_safe Starting mysqld daemon with databases from /data/5616/data 
  13. 4 升级 
  14. [root@db01 opt]# /usr/local/mysql5651/bin/mysql_upgrade -S /tmp/mysql5616.sock --force 
  15. Looking for 'mysql' as: /usr/local/mysql5651/bin/mysql 
  16. Looking for 'mysqlcheck' as: /usr/local/mysql5651/bin/mysqlcheck 
  17. Running 'mysqlcheck' with connection arguments: '--socket=/tmp/mysql5616.sock'  
  18. Running 'mysqlcheck' with connection arguments: '--socket=/tmp/mysql5616.sock'  
  19. mysql.columns_priv                                 OK 
  20. mysql.db                                           OK 
  21. mysql.event                                        OK 
  22. mysql.func                                         OK 
  23. mysql.general_log                                  OK 
  24. mysql.help_category                                OK 
  25. mysql.help_keyword                                 OK 
  26. mysql.help_relation                                OK 
  27. mysql.help_topic                                   OK 
  28. mysql.innodb_index_stats                           OK 
  29. mysql.innodb_table_stats                           OK 
  30. mysql.ndb_binlog_index                             OK 
  31. mysql.plugin                                       OK 
  32. mysql.proc                                         OK 
  33. mysql.procs_priv                                   OK 
  34. mysql.proxies_priv                                 OK 
  35. mysql.servers                                      OK 
  36. mysql.slave_master_info                            OK 
  37. mysql.slave_relay_log_info                         OK 
  38. mysql.slave_worker_info                            OK 
  39. mysql.slow_log                                     OK 
  40. mysql.tables_priv                                  OK 
  41. mysql.time_zone                                    OK 
  42. mysql.time_zone_leap_second                        OK 
  43. mysql.time_zone_name                               OK 
  44. mysql.time_zone_transition                         OK 
  45. mysql.time_zone_transition_type                    OK 
  46. mysql.user                                         OK 
  47. Running 'mysql_fix_privilege_tables'... 
  48. Running 'mysqlcheck' with connection arguments: '--socket=/tmp/mysql5616.sock'  
  49. Running 'mysqlcheck' with connection arguments: '--socket=/tmp/mysql5616.sock'  
  50. OK 
  51. 现在数据库已经由5616升级到了5651 
  52. [root@db01 opt]# /usr/local/mysql5651/bin/mysql -S /tmp/mysql5616.sock  
  53. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  54. Your MySQL connection id is 9 
  55. Server version: 5.6.51 MySQL Community Server (GPL) 
  56.  
  57. Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved. 
  58.  
  59. Oracle is a registered trademark of Oracle Corporation and/or its 
  60. affiliates. Other names may be trademarks of their respective 
  61. owners. 
  62.  
  63. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
  64.  
  65. mysql>  
  66.  
  67. 5 重启数据库到正常状态 
  68. [root@db01 opt]# /usr/local/mysql5651/bin/mysqladmin -S /tmp/mysql5616.sock shutdown 
  69. 210704 07:22:22 mysqld_safe mysqld from pid file /data/5616/data/db01.pid ended 
  70. [4]+  Done                    /usr/local/mysql5651/bin/mysqld_safe --defaults-file=/data/5616/my.cnf 
  71. [root@db01 opt]# /usr/local/mysql5651/bin/mysqld_safe --defaults-file=/data/5616/my.cnf & 
  72. [4] 12006 
  73. [root@db01 opt]# 210704 07:22:37 mysqld_safe Logging to '/data/5616/data/db01.err'
  74. 210704 07:22:37 mysqld_safe Starting mysqld daemon with databases from /data/5616/data 
  75.  
  76. [root@db01 opt]# /usr/local/mysql5651/bin/mysql -S /tmp/mysql5616.sock 
  77. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  78. Your MySQL connection id is 1 
  79. Server version: 5.6.51 MySQL Community Server (GPL) 
  80.  
  81. Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved. 
  82.  
  83. Oracle is a registered trademark of Oracle Corporation and/or its 
  84. affiliates. Other names may be trademarks of their respective 
  85. owners. 
  86.  
  87. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
  88.  
  89. mysql>  
  90.  
  91. 现在数据库版本已经由MySQL5616升级到了MySQL5651 

4.2 由MySQL5651升级到MySQL5733

  1. 1. 关闭原库 
  2. 2. 修改配置文件,指定当前basedir为5733的目录 
  3. 3. 使用高版本软件带起低版本数据 
  4. [root@db01 opt]# /usr/local/mysql5733/bin/mysqld_safe --defaults-file=/data/5616/my.cnf --skip-grant-tables --skip-networking & 
  5. [4] 12193 
  6. [root@db01 opt]# 2021-07-04T11:28:57.280601Z mysqld_safe Logging to '/data/5616/data/db01.err'
  7. 2021-07-04T11:28:57.337826Z mysqld_safe Starting mysqld daemon with databases from /data/5616/data 
  8.  
  9. 4. 升级 
  10. [root@db01 opt]# /usr/local/mysql5733/bin/mysql_upgrade -S /tmp/mysql5616.sock --force 
  11. Checking server version. 
  12. Running queries to upgrade MySQL server. 
  13. Checking system database
  14. mysql.columns_priv                                 OK 
  15. mysql.db                                           OK 
  16. mysql.engine_cost                                  OK 
  17. mysql.event                                        OK 
  18. mysql.func                                         OK 
  19. mysql.general_log                                  OK 
  20. mysql.gtid_executed                                OK 
  21. mysql.help_category                                OK 
  22. mysql.help_keyword                                 OK 
  23. mysql.help_relation                                OK 
  24. mysql.help_topic                                   OK 
  25. mysql.innodb_index_stats                           OK 
  26. mysql.innodb_table_stats                           OK 
  27. mysql.ndb_binlog_index                             OK 
  28. mysql.plugin                                       OK 
  29. mysql.proc                                         OK 
  30. mysql.procs_priv                                   OK 
  31. mysql.proxies_priv                                 OK 
  32. mysql.server_cost                                  OK 
  33. mysql.servers                                      OK 
  34. mysql.slave_master_info                            OK 
  35. mysql.slave_relay_log_info                         OK 
  36. mysql.slave_worker_info                            OK 
  37. mysql.slow_log                                     OK 
  38. mysql.tables_priv                                  OK 
  39. mysql.time_zone                                    OK 
  40. mysql.time_zone_leap_second                        OK 
  41. mysql.time_zone_name                               OK 
  42. mysql.time_zone_transition                         OK 
  43. mysql.time_zone_transition_type                    OK 
  44. mysql.user                                         OK 
  45. Upgrading the sys schema
  46. Checking databases. 
  47. sys.sys_config                                     OK 
  48. Upgrade process completed successfully. 
  49. Checking if update is needed. 
  50. 5 重启数据库到正常状态 
  51. [root@db01 opt]# /usr/local/mysql5733/bin/mysqladmin -S /tmp/mysql 
  52. mysql5616.sock       mysql5616.sock.lock  mysql5733.sock       mysql5733.sock.lock  mysql8021.sock       mysql8021.sock.lock  mysqlx.sock          mysqlx.sock.lock      
  53. [root@db01 opt]# /usr/local/mysql5733/bin/mysqladmin -S /tmp/mysql 
  54. mysql5616.sock       mysql5616.sock.lock  mysql5733.sock       mysql5733.sock.lock  mysql8021.sock       mysql8021.sock.lock  mysqlx.sock          mysqlx.sock.lock      
  55. [root@db01 opt]# /usr/local/mysql5733/bin/mysqladmin -S /tmp/mysql5616.sock shutdown 
  56. 2021-07-04T11:31:39.620201Z mysqld_safe mysqld from pid file /data/5616/data/db01.pid ended 
  57. [4]+  Done                    /usr/local/mysql5733/bin/mysqld_safe --defaults-file=/data/5616/my.cnf --skip-grant-tables --skip-networking 
  58. [root@db01 opt]# /usr/local/mysql5733/bin/mysqld_safe --defaults-file=/data/5616/my.cnf  & 
  59. [4] 12431 
  60. [root@db01 opt]# 2021-07-04T11:31:52.666976Z mysqld_safe Logging to '/data/5616/data/db01.err'
  61. 2021-07-04T11:31:52.727277Z mysqld_safe Starting mysqld daemon with databases from /data/5616/data 
  62.  
  63. [root@db01 opt]# /usr/local/mysql5733/bin/mysql -S /tmp/mysql5616.sock 
  64. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  65. Your MySQL connection id is 2 
  66. Server version: 5.7.33 MySQL Community Server (GPL) 
  67.  
  68. Copyright (c) 2000, 2021, Oracle and/or its affiliates. 
  69.  
  70. Oracle is a registered trademark of Oracle Corporation and/or its 
  71. affiliates. Other names may be trademarks of their respective 
  72. owners. 
  73.  
  74. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
  75.  
  76. mysql>  
  77. 至此,MySQL已经由5616升级到了5733 接下来我们将把MySQL从5733升级到8021 

5 将数据库从5733升级到8021

MySQL8.0的升级方式发生了变化.不再使用mysql_upgrade 而是使用mysql-shell对升级前数据库进行校验.

MySQL升级8.0和8.0之间的小版本升级需要注意:升级前必须备份.因为8.0不支持回退.

  1. 1 下载对应要升级到的8.0版本的mysql-shell  https://downloads.mysql.com/archives/shell/ 
  2. 2  解压做软连接 
  3. tar xf mysql-shell-8.0.21-linux-glibc2.12-x86-64bit.tar.gz 
  4. ln -s /opt/mysql-shell-8.0.21-linux-glibc2.12-x86-64bit /usr/local/mysqlsh 
  5. 2  添加环境变量 
  6. vim /etc/profile 
  7. export PATH=/usr/local/mysqlsh/bin:$PATH 
  8. [root@db01 opt]# source /etc/profile 
  9. [root@db01 opt]# mysqlsh -V 
  10. mysqlsh   Ver 8.0.21 for Linux on x86_64 - for MySQL 8.0.21 (MySQL Community Server (GPL)) 
  11. 3 连接到5733创建mysql-shell的连接用户 
  12. [root@db01 opt]# /usr/local/mysql5733/bin/mysql -S /tmp/mysql5616.sock 
  13. mysql> grant all on *.* to root@'10.0.0.%' identified by '123'
  14. 4 使用mysql-shell进行升级前的预检查 
  15. [root@db01 opt]# mysqlsh root:123@10.0.0.110:3307 -e "util.checkForServerUpgrade()" >/tmp/up.log 
  16. WARNING: Using a password on the command line interface can be insecure. 
  17. [root@db01 opt]# cat /tmp/up.log  
  18. The MySQL server at 10.0.0.110:3307, version 5.7.33 - MySQL Community Server 
  19. (GPL), will now be checked for compatibility issues for upgrade to MySQL 
  20. 8.0.21... 
  21.  
  22. 1) Usage of old temporal type 
  23.   No issues found 
  24.  
  25. 2) Usage of db objects with names conflicting with new reserved keywords 
  26.   No issues found 
  27.  
  28. 3) Usage of utf8mb3 charset 
  29.   No issues found 
  30.  
  31. 4) Table names in the mysql schema conflicting with new tables in 8.0 
  32.   No issues found 
  33.  
  34. 5) Partitioned tables using engines with non native partitioning 
  35.   No issues found 
  36.  
  37. 6) Foreign key constraint names longer than 64 characters 
  38.   No issues found 
  39.  
  40. 7) Usage of obsolete MAXDB sql_mode flag 
  41.   No issues found 
  42.  
  43. 8) Usage of obsolete sql_mode flags 
  44.   No issues found 
  45.  
  46. 9) ENUM/SET column definitions containing elements longer than 255 characters 
  47.   No issues found 
  48.  
  49. 10) Usage of partitioned tables in shared tablespaces 
  50.   No issues found 
  51.  
  52. 11) Circular directory references in tablespace data file paths 
  53.   No issues found 
  54.  
  55. 12) Usage of removed functions 
  56.   No issues found 
  57.  
  58. 13) Usage of removed GROUP BY ASC/DESC syntax 
  59.   No issues found 
  60.  
  61. 14) Removed system variables for error logging to the system log configuration 
  62.   To run this check requires full path to MySQL server configuration file to be specified at 'configPath' key of options dictionary 
  63.   More information: 
  64.     https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-13.html#mysqld-8-0-13-logging 
  65.  
  66. 15) Removed system variables 
  67.   To run this check requires full path to MySQL server configuration file to be specified at 'configPath' key of options dictionary 
  68.   More information: 
  69.     https://dev.mysql.com/doc/refman/8.0/en/added-deprecated-removed.html#optvars-removed 
  70.  
  71. 16) System variables with new default values 
  72.   To run this check requires full path to MySQL server configuration file to be specified at 'configPath' key of options dictionary 
  73.   More information: 
  74.     https://mysqlserverteam.com/new-defaults-in-mysql-8-0/ 
  75.  
  76. 17) Zero Date, Datetime, and Timestamp values 
  77.   No issues found 
  78.  
  79. 18) Schema inconsistencies resulting from file removal or corruption 
  80.   No issues found 
  81.  
  82. 19) Tables recognized by InnoDB that belong to a different engine 
  83.   No issues found 
  84.  
  85. 20) Issues reported by 'check table x for upgrade' command 
  86.   No issues found 
  87.  
  88. 21) New default authentication plugin considerations 
  89.   Warning: The new default authentication plugin 'caching_sha2_password' offers 
  90.     more secure password hashing than previously used 'mysql_native_password' 
  91.     (and consequent improved client connection authentication). However, it also 
  92.     has compatibility implications that may affect existing MySQL installations.  
  93.     If your MySQL installation must serve pre-8.0 clients and you encounter 
  94.     compatibility issues after upgrading, the simplest way to address those 
  95.     issues is to reconfigure the server to revert to the previous default 
  96.     authentication plugin (mysql_native_password). For example, use these lines 
  97.     in the server option file: 
  98.      
  99.     [mysqld] 
  100.     default_authentication_plugin=mysql_native_password 
  101.      
  102.     However, the setting should be viewed as temporarynot as a long term or 
  103.     permanent solution, because it causes new accounts created with the setting 
  104.     in effect to forego the improved authentication security. 
  105.     If you are using replication please take time to understand how the 
  106.     authentication plugin changes may impact you. 
  107.   More information: 
  108.     https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password-compatibility-issues 
  109.     https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password-replication 
  110.  
  111. Errors:   0 
  112. Warnings: 1 
  113. Notices:  0 
  114.  
  115. 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. 
  116. 5 校验没问题之后停原库 
  117. [root@db01 opt]# /usr/local/mysql5733/bin/mysql -S /tmp/mysql5616.sock 
  118. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  119. Your MySQL connection id is 6 
  120. Server version: 5.7.33 MySQL Community Server (GPL) 
  121.  
  122. mysql> set global innodb_fast_shutdown=0; 
  123. mysql> shutdown; 
  124. 6 修改配置文件中程序目录路径为8.0的程序路径 
  125. [root@db01 opt]# cat /data/5616/my.cnf 
  126. [mysqld]   
  127. user=mysql 
  128. basedir=/usr/local/mysql8021 
  129. datadir=/data/5616/data 
  130. socket=/tmp/mysql5616.sock 
  131. server_id=56 
  132. port=3307 
  133.  
  134. 7 使用8.0的软件挂载5.7的数据启动 
  135. [root@db01 opt]# /usr/local/mysql8021/bin/mysqld_safe --defaults-file=/data/5616/my.cnf & 
  136. [4] 12714 
  137. [root@db01 opt]# 2021-07-04T11:53:53.629634Z mysqld_safe Logging to '/data/5616/data/db01.err'
  138. 2021-07-04T11:53:53.686412Z mysqld_safe Starting mysqld daemon with databases from /data/5616/data  
  139. [root@db01 opt]# /usr/local/mysql8021/bin/mysql -S /tmp/mysql5616.sock 
  140. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  141. Your MySQL connection id is 11 
  142. Server version: 8.0.21 MySQL Community Server - GPL 
  143.  
  144. Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 
  145.  
  146. Oracle is a registered trademark of Oracle Corporation and/or its 
  147. affiliates. Other names may be trademarks of their respective 
  148. owners. 
  149.  
  150. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
  151.  
  152. mysql>  

6 后话

当然生产环境中的升级没有这么简单容易.会遇到很多问题.当然作为一名优秀的DBA.我相信大家都会迎刃而解.

当然在升级过程中尤其要注意sql_mode的变化.也要和业务及开发一起协同好!

 

责任编辑:武晓燕 来源: 今日头条
相关推荐

2018-04-24 10:00:20

2022-06-20 05:40:25

数据库MySQL查询

2010-11-23 09:05:31

MySQL升级

2015-03-04 09:30:34

数据库

2010-05-27 09:11:50

列数据库NoSQL

2023-02-28 00:01:53

MySQL数据库工具

2015-07-16 17:00:15

MySQLMariaDBPercona

2021-08-11 21:46:47

MySQL索引join

2023-11-14 09:08:12

MySQL多表关联

2020-02-02 13:59:59

MySQL数据库线程

2011-03-30 08:56:43

Zabbix数据库

2020-08-07 08:04:03

数据库MySQL技术

2010-05-24 17:33:43

MySQL数据库

2013-04-12 09:41:52

MySQL 5.6

2011-08-30 14:59:34

Qt数据库

2020-11-30 13:10:39

MySQL安全服务器

2010-04-09 15:08:17

Oracle 数据库性

2024-04-10 07:16:17

JDBC驱动MySQL数据库

2023-07-31 08:21:22

语法校对器Pick

2022-01-19 08:33:17

Oracle数据库AutoUpgrad
点赞
收藏

51CTO技术栈公众号