作者:Vinicius Grippa 管長龍 譯
本文中,我們將討論如何設置從 MySQL 8.0 到 MySQL 5.7 的複製。在某些情況下,使用此配置可能會有所幫助。例如,在 MySQL 升級的情況下,將使用較新版本的 MySQL 主服務器作為回滾計劃用於較舊版本的從服務器會很有用。另一個,則是在升級主主(master master)複製的情況。
MySQL 複製僅在連續主版本之間支持,並且僅從主低到從高之間支持。
master 5.7 - > slave 8.0 支持
master 8.0 - > slave 5.7 不支持
本文中,我將介紹如何克服這個問題並初始化,以便在此方案中設置複製功能。如果使用 MySQL 8 的新功能,我還會顯示一些可以暫停複製的錯誤。
slave > select @@version;
+---------------+
| @@version |
+---------------+
| 5.7.17-log |
+---------------+
1 row in set (0.00 sec)
master > select @@version;
+-----------+
| @@version |
+-----------+
| 8.0.12 |
+-----------+
1 row in set (0.00 sec)
首先,在執行 CHANGE MASTER 命令之前,需要修改主服務器上的排序規則。否則複製將遇到此錯誤:
slave > show slave statusG
Last_Errno: 22
Last_Error: Error 'Character set '#255' is not a compiled character set and is not specified in the '/opt/percona_server/5.7.17/share/charsets/Index.xml' file' on query. Default database: 'mysql8_1'. Query: 'create database mysql8_1'
這是因為 MySQL 8 上的默認 character_set 和排序規則發生了變化。根據文檔:
character_set_server 和 character_set_database 系統變量的默認值 已從 latin1更改為 utf8mb4。
collation_server 和 collation_database 系統變量的默認值已經從 latin1_swedish_ci 更改為 utf8mb4_0900_ai_ci。
讓我們在 MySQL 8 上將排序規則和字符集更改為 utf8(可以使用兩個版本中存在的任何選項):
# master my.cnf
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
collation-server=utf8_unicode_ci
重啟 MySQL 8 才能生效。接下來,在重啟之後,您必須使用 mysql_native_password 創建複製用戶 。這是因為 MySQL 8 將默認的 Authentication Plugin 更改為 maching_sha2_password,MySQL 5.7 不支持。如果您嘗試使用 caching_sha2_password 插件執行 CHANGE MASTER 命令 ,您將收到以下錯誤消息:
Last_IO_Errno: 2059
Last_IO_Error: error connecting to master '[email protected]:19025' - retry-time: 60 retries: 1
使用 mysql_native_password 創建用戶:
master> CREATE USER 'replica_user'@'%' IDENTIFIED WITH mysql_native_password BY 'repli$cat';
master> GRANT REPLICATION SLAVE ON *.* TO 'replica_user'@'%';
最後,我們可以像往常一樣繼續構建複製:
master > show master statusG
*************************** 1. row ***************************
File: mysql-bin.000007
Position: 155
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
slave > CHANGE MASTER TO MASTER_HOST='127.0.0.1',
MASTER_USER='replica_user',
MASTER_PASSWORD='repli$cat',
MASTER_PORT=19025,
MASTER_LOG_FILE='mysql-bin.000007',
MASTER_LOG_POS=155; start slave;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
# This procedure works with GTIDs too
slave > CHANGE MASTER TO MASTER_HOST='127.0.0.1',
MASTER_USER='replica_user',
MASTER_PASSWORD='repli$cat',
MASTER_PORT=19025,
MASTER_AUTO_POSITION = 1 ; start slave;
檢查複製狀態:
master > show slave statusG
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: replica_user
Master_Port: 19025
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 155
Relay_Log_File: mysql-relay.000002
Relay_Log_Pos: 321
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 155
Relay_Log_Space: 524
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 100
Master_UUID: 00019025-1111-1111-1111-111111111111
Master_Info_File: /home/vinicius.grippa/sandboxes/rsandbox_5_7_17/master/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.01 sec)
執行快速測試以檢查複製是否正常工作:
master > create database vinnie;
Query OK, 1 row affected (0.06 sec)
slave > show databases like 'vinnie';
+-------------------+
| Database (vinnie) |
+-------------------+
| vinnie |
+-------------------+
1 row in set (0.00 sec)
注意事項
任何嘗試使用 MySQL 8 中的新功能(如 rule,不可見索引或 caching_sha2_password)的嘗試都將使複製停止並顯示錯誤:
master > alter user replica_user identified with caching_sha2_password by 'sekret';
Query OK, 0 rows affected (0.01 sec)
slave > show slave statusG
Last_SQL_Errno: 1396
Last_SQL_Error: Error 'Operation ALTER USER failed for 'replica_user'@'%'' on query. Default database: ''. Query: 'ALTER USER 'replica_user'@'%' IDENTIFIED WITH 'caching_sha2_password' AS '$A$005$H MEDi"gQ
wR{/I/VjlgBIUB08h1jIk4fBzV8kU1J2RTqeqMq8Q2aox0''
總結
可以從 MySQL 8 複製到 MySQL 5.7。在某些情況下(特別是升級),這可能會有所幫助,但不建議在異構拓撲結構中使用,因為在某些情況下它會容易出錯和不兼容。
閱讀更多 愛可生雲數據庫 的文章