作者:趙黎明
愛可生 MySQL DBA 團(tuán)隊(duì)成員,Oracle 10g OCM,MySQL 5.7 OCP,擅長(zhǎng)數(shù)據(jù)庫性能問題診斷、事務(wù)與鎖問題的分析等,負(fù)責(zé)處理客戶 MySQL 及我司自研 DMP 平臺(tái)日常運(yùn)維中的問題,對(duì)開源數(shù)據(jù)庫相關(guān)技術(shù)非常感興趣。
本文來源:原創(chuàng)投稿
*愛可生開源社區(qū)出品,原創(chuàng)內(nèi)容未經(jīng)授權(quán)不得隨意使用,轉(zhuǎn)載請(qǐng)聯(lián)系小編并注明來源。
本文關(guān)鍵字:升級(jí)、Windows、賬號(hào)管理
最近有去某客戶那里支持 MySQL 實(shí)例升級(jí),他們的實(shí)例版本比較多,有 5.5、5.6、5.7,外加各種小版本,平臺(tái)也很多,有 Linux、Windows。對(duì)于 Unix/Linux 平臺(tái)的 MySQL 版本升級(jí),大家可能再熟悉不過了,但對(duì)于 windows 平臺(tái)的,可能接觸地較少。由于項(xiàng)目上有需求,自己也在 Windows 環(huán)境上測(cè)試了一把,有了些心得,想跟大家分享一些經(jīng)驗(yàn),于是有了本文。這里主要是針對(duì)比較常見的 5.6 到 5.7 的升級(jí)。
雖然是在講升級(jí),這里也稍微提一下 Windows 上安裝實(shí)例的方法。首先,準(zhǔn)備一臺(tái) Windows 主機(jī)(我這里用的是 Windows 10),去官網(wǎng)下載好 5.6.x,5.7.x for Windows 的 zip 包,我選擇了社區(qū)版的 5.6.30 和 5.7.30(最新 GA)的包。
解壓完的 zip包 是這樣的,有點(diǎn)像 Linux 上的二進(jìn)制包:
安裝很簡(jiǎn)單,最小化配置只需要把 my-default 參數(shù)中示例的幾個(gè)參數(shù)前的注釋符號(hào)去掉,并配置上相應(yīng)的值,就可以啟動(dòng)了,當(dāng)然,如果是生產(chǎn)環(huán)境,肯定會(huì)自定義一堆參數(shù),此處略過。
改完后如下,然后就可以用這個(gè)參數(shù)文件去初始化實(shí)例了:
初始化實(shí)例后,再啟動(dòng)實(shí)例,接著創(chuàng)建并配置 Windows 服務(wù),以后就可以通過啟停服務(wù)對(duì) MySQL 實(shí)例進(jìn)行管理。
這里不討論詳細(xì)的升級(jí)步驟,與安裝實(shí)例其實(shí)差不多,主要說一下升級(jí)時(shí)要注意的點(diǎn)(本次測(cè)試采用 IN-PLACE 方式)。
參考鏈接:
https://www.groovypost.com/howto/fix-visual-c-plus-plus-redistributable-windows-10/
https://answers.microsoft.com/en-us/windows/forum/windows_7-performance/missing-msvcp120dll-file/f0a14d55-73f0-4a21-879e-1cbacf05e906
各個(gè) dll 文件對(duì)應(yīng)的 Visual C++ 版本:
安裝完以后,可以查看 dll 動(dòng)態(tài)庫鏈接文件與對(duì)應(yīng) Visual C++ 版本之間的關(guān)系:
我的環(huán)境中要正常運(yùn)行 5.7.30 實(shí)例,需要安裝 Visual C++ 2012、Visual C++ 2013,之后就可以正常創(chuàng)建服務(wù)。
安裝完 5.7.30 實(shí)例后,創(chuàng)建并啟動(dòng) windows 服務(wù):
通過 SC 啟動(dòng)服務(wù)后卻立即停止了:
用 NET 命令也無法啟動(dòng),error 日志也沒有輸出:
嘗試刪除服務(wù)再重新添加:
此時(shí)服務(wù)還沒有被創(chuàng)建:
通過 SC 再次啟動(dòng)后,服務(wù)有了,但仍然是已停止?fàn)顟B(tài):
再次刪除服務(wù)后直接啟動(dòng) mysqld,在登錄客戶端后發(fā)現(xiàn)了端倪,原來是用 5.6.30 的 mysqld 命令啟動(dòng)了:
立即檢查 my.ini 參數(shù),發(fā)現(xiàn)參數(shù)中果然是用了 5.6.30 的路徑:
停止進(jìn)程,修改參數(shù)并重新啟動(dòng) 5.7.30 進(jìn)程和服務(wù):
msyql5730 服務(wù)恢復(fù)正常,不會(huì)再自動(dòng)停止了:
服務(wù)端(mysqld)和客戶端(mysql)進(jìn)程也運(yùn)行正常:
檢查數(shù)據(jù)庫(注意,此時(shí)仍然是沒有 sys 庫的,因?yàn)槭前?5.6 的 data 目錄直接拿來啟動(dòng)實(shí)例的):
執(zhí)行 mysql_upgrade,重建系統(tǒng)庫:
至此,完成 5.6.30 to 5.7.30 的升級(jí)(是不是 so easy?)。
要注意,執(zhí)行完 mysql_upgrade 后需要重啟實(shí)例,否則會(huì)報(bào)以下錯(cuò)誤:
重啟實(shí)例后,可以正常使用了:
mysql> select user,host from mysql.user;
+---------------+-------------+
| user | host |
+---------------+-------------+
| root | 10.186.64.% |
| root | 127.0.0.1 |
| root | ::1 |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-------------+
6 rows in set (0.00 sec)
mysql> exit
Bye
C:\Users\Administrator>mysql
ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)
C:\Users\Administrator>mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
C:\Users\Administrator>mysql -uroot -p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.30-log 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> select current_user(),user();
+----------------+----------------+
| current_user() | user() |
+----------------+----------------+
| root@::1 | root@localhost |
+----------------+----------------+
1 row in set (0.00 sec)
mysql> drop user root@'::1';
Query OK, 0 rows affected (0.06 sec)
mysql> exit
Bye
C:\Users\Administrator>mysql -uroot -p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.30-log 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> select current_user(),user();
+----------------+----------------+
| current_user() | user() |
+----------------+----------------+
| root@localhost | root@localhost |
+----------------+----------------+
1 row in set (0.02 sec)
mysql> drop user root@localhost;
Query OK, 0 rows affected (0.03 sec)
mysql> exit
Bye
C:\Users\Administrator>mysql -uroot -p
Enter password: ********
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
C:\Users\Administrator>mysql -uroot -p -h127.0.0.1
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.30-log 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> select current_user(),user();
+----------------+----------------+
| current_user() | user() |
+----------------+----------------+
| root@127.0.0.1 | root@localhost |
+----------------+----------------+
1 row in set (0.00 sec)
mysql> \s
--------------
mysql Ver 14.14 Distrib 5.7.30, for Win64 (x86_64)
Connection id: 10
Current database:
Current user: root@localhost
SSL: Cipher in use is ECDHE-RSA-AES128-GCM-SHA256
Using delimiter: ;
Server version: 5.7.30-log MySQL Community Server (GPL)
Protocol version: 10
Connection: 127.0.0.1 via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
Uptime: 6 hours 41 min 40 sec
Threads: 1 Questions: 26 Slow queries: 0 Opens: 123 Flush tables: 1 Open tables: 116 Queries per second avg: 0.001
--------------
mysql> show variables like 'socket';
+-----------------------------------------+-------+
| Variable_name | Value |
+-----------------------------------------+-------+
| socket | MySQL |
+-----------------------------------------+-------+
3 rows in set, 1 warning (0.01 sec)
mysql> exit
Bye
C:\Users\Administrator>mysql -uroot -p -S MySQL
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.30-log 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> \s
--------------
mysql Ver 14.14 Distrib 5.7.30, for Win64 (x86_64)
Connection id: 16
Current database:
Current user: root@localhost
SSL: Cipher in use is ECDHE-RSA-AES128-GCM-SHA256
Using delimiter: ;
Server version: 5.7.30-log MySQL Community Server (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
Uptime: 7 hours 6 min 23 sec
Threads: 1 Questions: 63 Slow queries: 0 Opens: 137 Flush tables: 1 Open tables: 130 Queries per second avg: 0.002
--------------
最后,再把升級(jí)的各個(gè)步驟羅列一遍:
1. 下載 5.7.30 的 zip 包并解壓到新的目錄。
2. 安裝 Visual C++ 2012/2013(或更多版本)。
3. 停應(yīng)用,停止 5.6 實(shí)例(可以通過停服務(wù)來操作,停止服務(wù)前建議記錄一下 GTID 或 binlog file 和 position 位置),刪除服務(wù)。
4. 備份一份 5.6 實(shí)例的 datadir,包括 binlog(整個(gè)目錄 copy 到別的目錄存放)。
5. 拷貝 5.6 實(shí)例的 datadir 和 my.ini 到 5.7 實(shí)例 basedir 目錄,調(diào)整并優(yōu)化參數(shù)值(注意要確保路徑一致,確認(rèn)已開啟 5.7 新特性相關(guān)參數(shù),如增強(qiáng)半同步、MTS 等)。
6. 修改系統(tǒng)環(huán)境變量,把可執(zhí)行路徑指向 5.7 實(shí)例的 basedir/bin。
7. 啟動(dòng) 5.7 實(shí)例,創(chuàng)建服務(wù)并啟動(dòng)。
8. 驗(yàn)證服務(wù)端、客戶端版本是否正確。
9. 確認(rèn)無誤后,執(zhí)行 mysql_upgrade 升級(jí)數(shù)據(jù)字典(會(huì)升級(jí)系統(tǒng)庫:mysql,ps,sys,沒有會(huì)重建)。
10. 重啟實(shí)例。
11. 再次校驗(yàn) 5.7 的參數(shù),盡量保持與 5.6 的兼容,尤其要注意 sql_mode 的默認(rèn)值對(duì)業(yè)務(wù)的影響。
12. 清理 5.6 實(shí)例的 basedir 和 datadir 目錄(可選)。
13. 如果是主從環(huán)境,還要考慮 slave_net_timeout 參數(shù)默認(rèn)值改變帶來的影響(主庫 error 日志中出現(xiàn) “ER_RPL_ZOMBIE_ENCOUNTERED” 的報(bào)錯(cuò))。
在 5.7.7 以前,該參數(shù)默認(rèn)是 3600s,之后改為了 60s,需要重新執(zhí)行 change master to 語句,并且顯式指定 master_heartbeat_period=xx,因?yàn)閺?5.7.4 開始,只有執(zhí)行 reset slave 才能將其重置為默認(rèn)值(slave_net_timeout 值的一半)。另外提一句,也是從 5.7.4 開始,執(zhí)行 change master to 語句時(shí),可以不用先停止復(fù)制線程了。
參考鏈接:
https://dev.mysql.com/doc/refman/5.7/en/change-master-to.html
https://dev.mysql.com/doc/refman/5.7/en/upgrading-from-previous-series.html
https://dev.mysql.com/doc/refman/5.7/en/replication-options-slave.html#sysvar_slave_net_timeout
本文主要闡述了如何優(yōu)雅地通過 ZIP&IN-PLACE 方式在 Windows 服務(wù)器上將 MySQL 5.6 實(shí)例升級(jí)到 5.7。
對(duì) MySQL 官方提供的一些升級(jí)方式和相關(guān)限制做了說明。
通過一個(gè)實(shí)際的升級(jí)案例來展示如何在升級(jí)過程中排查錯(cuò)誤。
演示了一個(gè)關(guān)于賬號(hào)登陸的小測(cè)試,要注意區(qū)分賬號(hào)的來源部分、current_user(),user() 之間的差別,前者是 mysql.user 表中創(chuàng)建的賬號(hào),后者是當(dāng)前操作系統(tǒng)的登錄用戶。
最后對(duì)整個(gè)升級(jí)步驟做了總結(jié),羅列了一些升級(jí)時(shí)的注意點(diǎn)。
參考鏈接:
https://dev.mysql.com/doc/refman/5.7/en/windows-installation.html
https://dev.mysql.com/doc/refman/5.7/en/windows-upgrading.html
官網(wǎng)https://dev.mysql.com/downloads/installer/下載MySql,下面的步驟僅供參考
1.
2.
3.
4.
5.
6.
7.
8.
9.配置環(huán)境變量
10.使用cmd看看有沒有安裝成功,輸入mysqld -install
11.使用Navicate連接數(shù)據(jù)庫試試