In this blog post, we’ll discuss how to set a replication from MySQL 8.0 to MySQL 5.7. There are some situations that having this configuration might help. For example, in the case of a MySQL upgrade, it can be useful to have a master that is using a newer version of MySQL to an older version slave as a rollback plan. Another example is in the case of upgrading a master x master replication topology.
Officially, replication is only supported between consecutive major MySQL versions, and only from a lower version master to a higher version slave. Here is an example of a supported scenario:
5.7 master –> 8.0 slave
while the opposite is not supported:
8.0 master –> 5.7 slave
In this blog post, I’ll walk through how to overcome the initial problems to set a replication working in this scenario. I’ll also show some errors that can halt the replication if a new feature from MySQL 8 is used.
Here is the initial set up that will be used to build the topology:
|
1 |
slave > select @@version;<br>+---------------+<br>| @@version |<br>+---------------+<br>| 5.7.17-log |<br>+---------------+<br>1 row in set (0.00 sec)<br><br>master > select @@version;<br>+-----------+<br>| @@version |<br>+-----------+<br>| 8.0.12 |<br>+-----------+<br>1 row in set (0.00 sec) |
First, before executing the CHANGE MASTER command, you need to modify the collation on the master server. Otherwise the replication will run into this error:
|
1 |
slave > show slave statusG<br> Last_Errno: 22<br> 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'<br> |
This is because the default character_set and the collation has changed on MySQL 8. According to the documentation:
The default value of the character_set_server and character_set_database system variables has changed from latin1 to utf8mb4.
The default value of the collation_server and collation_database system variables has changed from latin1_swedish_ci to utf8mb4_0900_ai_ci.
Let’s change the collation and the character set to utf8 on MySQL 8 (it is possible to use any option that exists in both versions):
|
1 |
# master my.cnf<br>[client]<br>default-character-set=utf8<br><br>[mysqld]<br>character-set-server=utf8<br>collation-server=utf8_unicode_ci |
You need to restart MySQL 8 to apply the changes. Next, after the restart, you have to create a replication user using mysql_native_password.This is because MySQL 8 changed the default Authentication Plugin to caching_sha2_password which is not supported by MySQL 5.7. If you try to execute the CHANGE MASTER command with a user using caching_sha2_password plugin, you will receive the error message below:
|
1 |
Last_IO_Errno: 2059<br>Last_IO_Error: error connecting to master '[email protected]:19025' - retry-time: 60 retries: 1 |
To create a user using mysql_native_password :
|
1 |
master> CREATE USER 'replica_user'@'%' IDENTIFIED WITH mysql_native_password BY 'repli$cat';<br>master> GRANT REPLICATION SLAVE ON *.* TO 'replica_user'@'%'; |
Finally, we can proceed as usual to build the replication:
|
1 |
master > show master statusG<br>*************************** 1. row ***************************<br>File: mysql-bin.000007<br>Position: 155<br>Binlog_Do_DB:<br>Binlog_Ignore_DB:<br>Executed_Gtid_Set:<br>1 row in set (0.00 sec)<br><br>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;<br>Query OK, 0 rows affected, 2 warnings (0.01 sec)<br>Query OK, 0 rows affected (0.00 sec)<br><br># This procedure works with GTIDs too<br>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; <br> |
Checking the replication status:
|
1 |
master > show slave statusG<br>*************************** 1. row ***************************<br>Slave_IO_State: Waiting for master to send event<br>Master_Host: 127.0.0.1<br>Master_User: replica_user<br>Master_Port: 19025<br>Connect_Retry: 60<br>Master_Log_File: mysql-bin.000007<br>Read_Master_Log_Pos: 155<br>Relay_Log_File: mysql-relay.000002<br>Relay_Log_Pos: 321<br>Relay_Master_Log_File: mysql-bin.000007<br>Slave_IO_Running: Yes<br>Slave_SQL_Running: Yes<br>Replicate_Do_DB:<br>Replicate_Ignore_DB:<br>Replicate_Do_Table:<br>Replicate_Ignore_Table:<br>Replicate_Wild_Do_Table:<br>Replicate_Wild_Ignore_Table:<br>Last_Errno: 0<br>Last_Error:<br>Skip_Counter: 0<br>Exec_Master_Log_Pos: 155<br>Relay_Log_Space: 524<br>Until_Condition: None<br>Until_Log_File:<br>Until_Log_Pos: 0<br>Master_SSL_Allowed: No<br>Master_SSL_CA_File:<br>Master_SSL_CA_Path:<br>Master_SSL_Cert:<br>Master_SSL_Cipher:<br>Master_SSL_Key:<br>Seconds_Behind_Master: 0<br>Master_SSL_Verify_Server_Cert: No<br>Last_IO_Errno: 0<br>Last_IO_Error:<br>Last_SQL_Errno: 0<br>Last_SQL_Error:<br>Replicate_Ignore_Server_Ids:<br>Master_Server_Id: 100<br>Master_UUID: 00019025-1111-1111-1111-111111111111<br>Master_Info_File: /home/vinicius.grippa/sandboxes/rsandbox_5_7_17/master/data/master.info<br>SQL_Delay: 0<br>SQL_Remaining_Delay: NULL<br>Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates<br>Master_Retry_Count: 86400<br>Master_Bind:<br>Last_IO_Error_Timestamp:<br>Last_SQL_Error_Timestamp:<br>Master_SSL_Crl:<br>Master_SSL_Crlpath:<br>Retrieved_Gtid_Set:<br>Executed_Gtid_Set:<br>Auto_Position: 0<br>Replicate_Rewrite_DB:<br>Channel_Name:<br>Master_TLS_Version:<br>1 row in set (0.01 sec) |
Executing a quick test to check if the replication is working:
|
1 |
master > create database vinnie;<br>Query OK, 1 row affected (0.06 sec) |
|
1 |
slave > show databases like 'vinnie';<br>+-------------------+<br>| Database (vinnie) |<br>+-------------------+<br>| vinnie |<br>+-------------------+<br>1 row in set (0.00 sec) |
Any tentative attempts to use a new feature from MySQL 8 like roles, invisible indexes or caching_sha2_password will make the replication stop with an error:
|
1 |
master > alter user replica_user identified with caching_sha2_password by 'sekret';<br>Query OK, 0 rows affected (0.01 sec) |
|
1 |
slave > show slave statusG<br> Last_SQL_Errno: 1396<br> 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<br> wR{/I/VjlgBIUB08h1jIk4fBzV8kU1J2RTqeqMq8Q2aox0'' |
Replicating from MySQL 8 to MySQL 5.7 is possible. In some scenarios (especially upgrades), this might be helpful, but it is not advisable to have a heterogeneous topology because it will be prone to errors and incompatibilities under some cases.
Read our solution brief to learn how to setup MySQL Amazon RDS instances. The ease of initial setup makes Amazon RDS capable of accommodating rapid deployment, as well as application development. Our brief also includes failover and basic business continuity components.
For a proven MySQL database architecture that is similarly easy to set up, check out our solution brief: Get Up and Running with Percona Server for MySQL. This solution is ideal for startup organizations and small businesses.
Resources
RELATED POSTS