MySQL 5.7 升級到 8.0

运维工作栈發表於2024-05-24

1.升級前檢查

要注意作業系統 glibc 的版本, 下載可支援的版本的 mysql 相關的程式包

# 檢視作業系統支援的 glibc 版本
strings /lib/x86_64-linux-gnu/libc.so.6 | grep GLIBC_
wget https://downloads.mysql.com/archives/get/p/43/file/mysql-shell-8.0.36-linux-glibc2.12-x86-64bit.tar.gz
tar xf mysql-shell-8.0.36-linux-glibc2.12-x86-64bit.tar.gz -C /tmp/
cd /tmp/mysql-shell-8.0.34-linux-glibc2.12-x86-64bit/bin
./mysqlsh -uroot -p -S /tmp/mysql.sock -e "util.checkForServerUpgrade()" > util.checkForServerUpgrade.log

2.下載安裝包

wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.36-linux-glibc2.12-x86_64.tar.xz
tar xf mysql-8.0.36-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
cd /usr/local && mv mysql-8.0.36-linux-glibc2.12-x86_64 mysql-8.0.36

3.邏輯備份

mysqldump -uroot -p --routines --set-gtid-purged=OFF --databases XXX1  XXX2 > /data/all-database-`date +%F`.sql
# --routines 備份儲存過程和函式

4.關閉資料庫

mysql -h127.0.0.1 -uroot -p'xxx'
select version();
show variables like 'innodb_fast_shutdown';
set global innodb_fast_shutdown=0;
shutdown;
exit;

5.備份資料目錄和配置檔案

cp -r /data/local/mysql/data /data/local/mysql/data_`date +%F`
cp /etc/my.cnf /etc/my.cnf_`date +%F`

6.修改配置檔案

# /etc/my.cnf

[mysqld]
user=mysql
basedir=/usr/local/mysql-8.0.36
datadir=/data/local/mysql/data
server_id=3
port=3306
socket=/tmp/mysql.sock
log_bin=/data/local/mysql/binlog/bin-log
binlog_format = ROW
max_binlog_size = 100M
log_slave_updates
expire_logs_days=7

long_query_time=5
slow_query_log=ON
slow_query_log_file=/data/local/mysql/slow_query.log

log-error=/data/local/mysql/mysql-error.log
log_timestamps=SYSTEM

max_connections=1000
max_allowed_packet=500M
skip-name-resolve=1

character_set_server=utf8
collation-server=utf8_general_ci

# For 8.0
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
default_authentication_plugin=mysql_native_password
default-storage-engine=INNODB

[mysql]
socket=/tmp/mysql.sock
default-character-set=utf8

7.執行升級

/usr/local/mysql-8.0.36/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --upgrade=FORCE

# 會一直卡住
# error 日誌看到以下輸出, 說明升級完成
# Server upgrade from '80036' to '80036' completed.

# 新開視窗, 連上 mysql 執行命令檢視
select version();

8.修改環境變數及啟動指令碼

export PATH=$PATH:/usr/local/mysql-8.0.36/bin

# /etc/init.d/mysqld
basedir=/usr/local/mysql-8.0.36

9.重啟 mysql

systemctl daemon-reload
/etc/init.d/mysqld restart

相關文章