十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)如何將MySQL5.7升級(jí)到8.0,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
首先,我們要大概了解下MySQL5.7和8.0有哪些不同,參考官方文檔和其他網(wǎng)友文章,概括總結(jié)出MySQL8.0以下幾點(diǎn)新特性:
默認(rèn)字符集由latin1變?yōu)閡tf8mb4。
MyISAM系統(tǒng)表全部換成InnoDB表。
JSON特性增強(qiáng)。
支持不可見索引,支持直方圖。
sql_mode參數(shù)默認(rèn)值變化。
默認(rèn)密碼策略變更。
新增角色管理。
支持窗口函數(shù),支持Hash join。
根據(jù)版本變化及官方升級(jí)教程,列舉出以下幾點(diǎn)注意事項(xiàng):
注意字符集設(shè)置。為了避免新舊對(duì)象字符集不一致的情況,可以在配置文件將字符集和校驗(yàn)規(guī)則設(shè)置為舊版本的字符集和比較規(guī)則。
密碼認(rèn)證插件變更。為了避免連接問題,可以仍采用5.7的mysql_native_password認(rèn)證插件。
sql_mode支持問題。8.0版本sql_mode不支持NO_AUTO_CREATE_USER,要避免配置的sql_mode中帶有NO_AUTO_CREATE_USER。
是否需要手動(dòng)升級(jí)系統(tǒng)表。在MySQL 8.0.16版本之前,需要手動(dòng)的執(zhí)行mysql_upgrade來完成該步驟的升級(jí),在MySQL 8.0.16版本及之后是由mysqld來完成該步驟的升級(jí)。
下面以Linux系統(tǒng)為例,展示下具體升級(jí)過程。我的系統(tǒng)是CentOS7.7,原版本是MySQL5.7.23,以In-Place方式直接升級(jí)到MySQL8.0.19。
2.1 下載解壓安裝包
官網(wǎng)下載對(duì)應(yīng)版本的tar包,可通過wget下載或者本地下載后上傳。
下載地址:
https://downloads.mysql.com/archives/community/
選擇mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
執(zhí)行以下步驟解壓tar包:
# 安裝包上傳至原安裝包目錄下 我的是/usr/local/ cd /usr/local/ # 解壓安裝包 xz -d mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz tar -xvf mysql-8.0.19-linux-glibc2.12-x86_64.tar # 文件夾重命名為mysql8 mv mysql-8.0.19-linux-glibc2.12-x86_64 mysql8 # 更改文件夾所屬 chown -R mysql.mysql /usr/local/mysql8/
2.2 更改配置文件my.cnf
因5.7版本與8.0版本參數(shù)有所不同,為了能順利升級(jí),我們需要更改部分配置參數(shù)。主要注意sql_mode、basedir、密碼認(rèn)證插件及字符集設(shè)置,其他參數(shù)最好還是按照原5.7的來,不需要做調(diào)整。下面展示下更改后的配置文件:
# 最后幾個(gè)for8.0的參數(shù)要格外注意 [mysqld] user = mysql datadir = /data/mysql/data port = 3306 socket = /data/mysql/tmp/mysql.sock pid-file = /data/mysql/tmp/mysqld.pid tmpdir = /data/mysql/tmp skip_name_resolve = 1 max_connections = 2000 group_concat_max_len = 1024000 lower_case_table_names = 1 log_timestamps=SYSTEM max_allowed_packet = 32M binlog_cache_size = 4M sort_buffer_size = 2M read_buffer_size = 4M join_buffer_size = 4M tmp_table_size = 96M max_heap_table_size = 96M max_length_for_sort_data = 8096 default_time_zone = '+8:00' #logs server-id = 1003306 log-error = /data/mysql/logs/error.log slow_query_log = 1 slow_query_log_file = /data/mysql/logs/slow.log long_query_time = 3 log-bin = /data/mysql/logs/binlog binlog_format = row log_bin_trust_function_creators = 1 gtid_mode = ON enforce_gtid_consistency = ON #for8.0 sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION character-set-server = utf8 collation_server = utf8_general_ci basedir = /usr/local/mysql8 skip_ssl default_authentication_plugin=mysql_native_password
2.3 執(zhí)行升級(jí)程序
所有前置工作準(zhǔn)備好后就可以開始正式升級(jí)了,不過升級(jí)前還是建議先全庫(kù)備份下。萬事俱備后,按照如下指示進(jìn)行正式升級(jí)。
# 進(jìn)入原5.7 mysql命令行 正確關(guān)閉數(shù)據(jù)庫(kù) mysql> select version(); +------------+ | version() | +------------+ | 5.7.23-log | +------------+ 1 row in set (0.00 sec) mysql> show variables like 'innodb_fast_shutdown'; +----------------------+-------+ | Variable_name | Value | +----------------------+-------+ | innodb_fast_shutdown | 1 | +----------------------+-------+ 1 row in set (0.00 sec) # 確保數(shù)據(jù)都刷到硬盤上,更改成0 mysql> set global innodb_fast_shutdown=0; Query OK, 0 rows affected (0.00 sec) mysql> shutdown; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye # 退出至終端 用mysql8.0.19客戶端直接啟動(dòng) [root@centos ~]# /usr/local/mysql8/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql & [1] 23333 [root@centos ~]# 2020-05-20T07:07:02.337626Z mysqld_safe Logging to '/data/mysql/logs/error.log'. 2020-05-20T07:07:02.366244Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/data # 可觀察下錯(cuò)誤日志看是否報(bào)錯(cuò) 然后重新登錄測(cè)試 [root@centos ~]# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 17 Server version: 8.0.19 MySQL Community Server - GPL Copyright (c) 2000, 2018, 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 version(); +-----------+ | version() | +-----------+ | 8.0.19 | +-----------+ 1 row in set (0.00 sec)
2.4 環(huán)境變量修改
因basedir由/usr/local/mysql變成了/usr/local/mysql8,故相關(guān)環(huán)境變量推薦修改下??砂凑找韵虏襟E來操作驗(yàn)證:
# 修改mysql服務(wù)啟動(dòng)項(xiàng)配置 vi /etc/init.d/mysql # 修改basedir目錄 basedir=/usr/local/mysql8 # 修改PATH變量 vi /etc/profile # 將PATH中的/usr/local/mysql/bin改為/usr/local/mysql8/bin # 生效驗(yàn)證 [root@centos ~]# source /etc/profile [root@centos ~]# which mysql /usr/local/mysql8/bin/mysql [root@centos ~]# mysql -V mysql Ver 8.0.19 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
至此,我們的數(shù)據(jù)庫(kù)由5.7成功升級(jí)至8.0!對(duì)比MySQL安裝過程及升級(jí)過程,發(fā)現(xiàn)二者很相似,其實(shí)升級(jí)過程并不復(fù)雜,復(fù)雜的是升級(jí)后的驗(yàn)證及兼容測(cè)試,特別是對(duì)于復(fù)雜的業(yè)務(wù)庫(kù),MySQL版本升級(jí)還是要小心的。真實(shí)環(huán)境建議先升級(jí)從庫(kù),驗(yàn)證無誤后再逐步對(duì)主庫(kù)進(jìn)行升級(jí)。
上述就是小編為大家分享的如何將MySQL5.7升級(jí)到8.0了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道。