MySQL Replication (M-M)
Last updated
Last updated
Two MySQL Servers β You need two MySQL servers, each with a unique hostname (e.g., server1.example.com
and server2.example.com
).
Network Access β Ensure that both MySQL servers can communicate with each other over the network (typically port 3306
).
Admin Access β You need to have root access to both servers to configure replication and make the necessary changes.
Configure MySQL on both servers to enable binary logging and unique server IDs.
Edit the my.cnf
file on both servers (/etc/my.cnf
or /etc/mysql/my.cnf
depending on your system):
On Server 1 (e.g., server1.example.com
):
On Server 2 (e.g., server2.example.com
):
After making these changes, restart MySQL on both servers:
Create a replication user on both servers. This user will have the necessary privileges to replicate data between the servers.
On Server 1 (run from mysql
client):
On Server 2 (run from mysql
client):
Find the current binary log position on each server.
On Server 1, execute:
This will show something like this:
Note the File
and Position
values. In this example, itβs mysql-bin.000001
and 107
.
On Server 2, execute the same command to get the binary log status:
Again, note the File
and Position
.
Configure Server 1 to replicate from Server 2.
On Server 1, connect to the MySQL client and run:
Start the replication on Server 1:
Check the status of replication on Server 1 to ensure itβs running:
Look for Slave_IO_Running: Yes
and Slave_SQL_Running: Yes
in the output. If both are Yes
, replication is working.
Configure Server 2 to replicate from Server 1.
On Server 2, connect to the MySQL client and run:
Start the replication on Server 2:
Check the status of replication on Server 2:
Look for Slave_IO_Running: Yes
and Slave_SQL_Running: Yes
in the output. If both are Yes
, replication is working.
Test replication by inserting data into Server 1:
Check if the data appears on Server 2:
On Server 2, run:
You should see the data inserted on Server 1 appearing on Server 2.
Repeat the same on Server 2, inserting data into Server 2, and check if it appears on Server 1.
Regularly monitor the replication status using:
Ensure that both Slave_IO_Running
and Slave_SQL_Running
are Yes
.
Check for any replication errors. If you encounter errors, resolve them before proceeding to prevent data inconsistencies.