做者:Vinicius Grippa 翻譯:管長龍mysql
本文中,咱們將討論如何設置從 MySQL 8.0 到 MySQL 5.7 的複製。在某些狀況下,使用此配置可能會有所幫助。例如,在 MySQL 升級的狀況下,將使用較新版本的 MySQL 主服務器做爲回滾計劃用於較舊版本的從服務器會頗有用。另外一個,則是在升級主主(master master)複製的狀況。sql
MySQL 複製僅在連續主版本之間支持,而且僅從主低到從高之間支持。服務器
master 5.7 - > slave 8.0 支持 master 8.0 - > slave 5.7 不支持 ide
本文中,我將介紹如何克服這個問題並初始化,以便在此方案中設置複製功能。若是使用 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 status\G 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(可使用兩個版本中存在的任何選項):code
# 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
命令 ,您將收到如下錯誤消息:server
Last_IO_Errno: 2059 Last_IO_Error: error connecting to master 'root@127.0.0.1:19025' - retry-time: 60 retries: 1
使用 mysql_native_password
建立用戶:xml
master> CREATE USER 'replica_user'@'%' IDENTIFIED WITH mysql_native_password BY 'repli$cat'; master> GRANT REPLICATION SLAVE ON *.* TO 'replica_user'@'%';
最後,咱們能夠像往常同樣繼續構建複製:
master > show master status\G *************************** 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 status\G *************************** 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 status\G 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。在某些狀況下(特別是升級),這可能會有所幫助,但不建議在異構拓撲結構中使用,由於在某些狀況下它會容易出錯和不兼容。
原文連接:
https://www.percona.com/blog/2018/04/10/migrating-database-charsets-to-utf8mb4/