29 December 2022

How to MySQL replication master to master

MySQL replication allows you to replicate data from one MySQL database server (the master) to one or more MySQL database servers (the slaves). You can set up master-master replication to allow two servers to act as both masters and slaves, so that each server can receive updates from and propagate updates to the other server.

Here's how to set up MySQL master-master replication:
  • Make sure that both servers have the necessary software and are configured to communicate with each other.
  • On both servers, create a new user account that will be used for replication. This user should have the REPLICATION SLAVE privilege.
  • On the first server (the "master"), create a unique identifier for the server using the following command:
CHANGE MASTER TO MASTER_HOST='server_name', MASTER_USER='replication_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='log_file_name', MASTER_LOG_POS=log_position;

  • Replace "server_name" with the name or IP address of the first server, "replication_user" with the replication user you created in step 2, and "password" with the password for that user. "log_file_name" is the name of the binary log file on the first server, and "log_position" is the position within that file where replication should start. 
  • You can find this information by running the "SHOW MASTER STATUS" command on the first server.
  • On the second server (the "slave"), run the following command to set up replication from the first server:
CHANGE MASTER TO MASTER_HOST='server_name', MASTER_USER='replication_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='log_file_name', MASTER_LOG_POS=log_position;
  • Replace "server_name" with the name or IP address of the first server, "replication_user" with the replication user you created in step 2, and "password" with the password for that user. "log_file_name" and "log_position" should be the same as the values you used on the first server.
  • Start the replication process on both servers by running the following command on each server:
START SLAVE;
  • To verify that replication is working, check the status of the slave on each server by running the following command:
SHOW SLAVE STATUS\G
  • Look for the "Slave_IO_Running" and "Slave_SQL_Running" fields. If both are "Yes", replication is working correctly.
That's it! You should now have a functioning MySQL master-master replication setup. Keep in mind that this is just a basic overview of the process, and there are many other configuration options and considerations to take into account when setting up replication.

No comments:

Post a Comment