最近系統漏洞掃描,掃出來很多 MySQL 的漏洞,沒有什麼好的辦法,先升級到最新版本。在梳理 MySQL 資料庫資訊時發現有一套 zabbix 用的 MySQL 版本是 5.1.71 的,現在的最新版本是 5.7.17,版本跨越的太大了,不知道直接升級是否可行。庫的資料量大概 15G,於是就想先備份,備份完後用 mysql_upgrade 做升級。如果實在不行就再新建立一套庫把資料導進去。
下面是在虛擬機器上測試的升級步驟,中間也遇到了一些問題,不過好在最後還是升級成功了,説明這條路還是可行了。
操作系統版本 RHEL 6.7 舊的 MySQL 版本 5.1.71,新的 MySQL 版本 5.7.17 。
1 、檢視舊的 MySQL 版本,並建立測試資料庫和測試表
mysql> select version();
+——————+
| version() |
+——————+
| 5.1.71-community |
+——————+
mysql> create database zx;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| test |
| zx |
+——————–+
4 rows in set (0.00 sec)
mysql> use zx;
Database changed
mysql> create table test_upgrade (id int);
Query OK, 0 rows affected (0.07 sec)
……
mysql> select count(*) from test_upgrade;
+———-+
| count(*) |
+———-+
| 33554432 |
+———-+
1 row in set (0.00 sec)
現在 test_upgrade 表裏有 33554432 條資料。
2 、停止舊 MySQL,並備份資料
[root@rhel6 mysql]# service mysql stop
Shutting down MySQL.. SUCCESS!
#這裏直接用 cp 備份資料
[root@rhel6 lib]# cp -R mysql mysql_bak
[root@rhel6 lib]# du -sk mysql*
250880 mysql
250880 mysql_bak
3 、安裝新 Msql 站羣軟件,這裏下載的是編譯好的解壓就可以使用
[root@rhel6 ~]# tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
#解壓完後移動到/opt 目錄下
[root@rhel6 ~]# mv mysql-5.7.17-linux-glibc2.5-x86_64 mysql-5.7.17
[root@rhel6 ~]# mv mysql-5.7.17 /opt
[root@rhel6 ~]# cd /opt
[root@rhel6 opt]# chown -R mysql:mysql mysql-5.7.17/
[root@rhel6 opt]# ll
total 4
drwxr-xr-x. 9 mysql mysql 4096 Mar 22 21:07 mysql-5.7.17
4 、修改 my.cnf
主要修改 basedir 引數:
[root@rhel6 opt]# vi /etc/my.cnf
basedir=/opt/mysql-5.7.17
5 、啓動新 MySQL
[root@rhel6 mysql-5.7.17]# ./bin/mysqld_safe &
1) 啓動報錯 1:
2017-03-22T13:12:39.131998Z 0 [ERROR] InnoDB: The Auto-extending innodb_system data file ‘./ibdata1’ is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero) pages!2017-03-22T13:12:39.132028Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2017-03-22T13:12:39.733953Z 0 [ERROR] Plugin ‘InnoDB’ init function returned error.
2017-03-22T13:12:39.733986Z 0 [ERROR] Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed.
2017-03-22T13:12:39.733994Z 0 [ERROR] Failed to initialize plugins.
2017-03-22T13:12:39.734000Z 0 [ERROR] Aborting
解決方法在 my.cnf 中的 [mysqld] 部分新增如下引數
innodb_data_file_path = ibdata1:10M:autoextend
再次啓動
2) 啓動報錯 2:
2017-03-22T13:16:03.374717Z 0 [ERROR] unknown option ‘–skip-locking’
2017-03-22T13:16:03.374735Z 0 [ERROR] Aborting
解決方法從 my.cnf 中註釋掉相關引數 skip-locking
再次啓動
3) 啓動報錯 3:
2017-03-22T13:18:20.278752Z 0 [ERROR] Fatal error: mysql.user table is damaged. Please run mysql_upgrade.
2017-03-22T13:18:20.278954Z 0 [ERROR] Aborting
5.7 無法讀取 5.1 的 mysql.user 表,解決方法使用–skip-grant-tables 引數跳過授權驗證
再次啓動
[root@rhel6 mysql-5.7.17]# ./bin/mysqld_safe –skip-grant-tables&
啓動成功
2017-03-22T13:20:23.919677Z 0 [Note] ./bin/mysqld: ready for connections.
Version: ‘5.7.17’ socket: ‘/var/lib/mysql/mysql.sock’ port: 3306 MySQL Community Server (GPL)
6 、執行升級
[root@rhel6 mysql-5.7.17]# ./bin/mysql_upgrade -uroot
Checking if update is needed.
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.host 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
zx.test_upgrade OK
Upgrade process completed successfully.
Checking if update is needed.
7 、重啓 MySQL,並驗證
[root@rhel6 mysql-5.7.17]# ./bin/mysqladmin -uroot -p shutdown
[root@rhel6 mysql-5.7.17]# ./bin/mysqld_safe &
mysql> select version();
+———–+
| version() |
+———–+
| 5.7.17 |
+———–+
1 row in set (0.00 sec)
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
| zx |
+——————–+
6 rows in set (0.00 sec)
mysql> select count(*) from zx.test_upgrade;
+———-+
| count(*) |
+———-+
| 33554432 |
+———-+
1 row in set (0.01 sec)
驗證通過後,刪除舊版本的 MySQL 站羣軟件。