Setup Master-Master Replication in MySQL Server

MySQL Master-Slave replication is to set up slave server to update immediately as soon as changes done in Master server. But it will not update Master if there are any changes done on slave server.

This article will help you to set up Master-Master replication between MySQL servers. In this setup if any changes made on either server will update on an other one.

Setup Details:

MySQL Master1 system : RHEL 5
Master1 IP Address : 192.168.1.250/24
MySQL Master2 system : RHEL 5
Master2 IP Address: 192.168.1.150/24

If you are using different – different versions of MySQL on either servers use this link to check compatibility.

Step 1. Set Up MySQL Master-1 Server

Edit MySQL configuration file and add the following lines under [mysqld] section.

# vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
binlog-do-db=unixmen
server-id=1

Restart MySQL server to changes take effect.

# service mysqld restart

Create an mysql account on Master-1 server with REPLICATION SLAVE privileges to which replication client will connect to master.

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'%' IDENTIFIED BY 'secretpassword';
mysql> FLUSH PRIVILEGES;

Block write statement on all the tables, so no changes can be made after taking backup.

mysql> use unixmen;
mysql> FLUSH TABLES WITH READ LOCK;

Check the current binary log file name (File) and current offset (Position) value using following command.

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      332 | unixmen      |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

The above output is showing that the current binary file is using mysql-bin.000003 and offset value is 332. Note down these values to use on Master-2 server in next step.

Take a backup of database and copy it to another mysql server.

# mysqldump -u root -p unixmen > unixmen.sql
# scp unixmen.sql 192.168.1.150:/opt/

After completing backup remove the READ LOCK from tables, So that changes can be made.

mysql> UNLOCK TABLES;

Step 2. Setup MySQL Master-2 Server

Edit mysql Master-2 configuration file and add following values under [mysqld] section.

# vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
binlog-do-db=unixmen
server-id=1

server-id always be an non zero numeric value. These value will never be similar with other master or slave servers.

Restart MySQL server, If you had already configured replication use –skip-slave-start in start to not to immediate connect to master server.

# service mysqld restart

Restore database backup taken from master server.

# mysql -u root -p unixmen < /opt/unixmen.sql

Create an mysql account on Master-1 server with REPLICATION SLAVE privileges to which replication client will connect to master.

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'%' IDENTIFIED BY 'secretpassword';
mysql> FLUSH PRIVILEGES;

Check the current binary log file name (File) and current offset (Position) value using following command.

mysql > SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      847 | unixmen      |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

The above output is showing that the current binary file is using mysql-bin.000001 and offset value is 847. Note down these values to use in Step 3.

Setup option values on slave server using following command.

mysql>  CHANGE MASTER TO MASTER_HOST='192.168.1.250',
    -> MASTER_USER='repl_user',
    -> MASTER_PASSWORD='secretpassword',
    -> MASTER_LOG_FILE='mysql-bin.000003',
    -> MASTER_LOG_POS=332;

Step 3: Complete Setup on MySQL Master-1

Login to MySQL Master-1 server and execute following command.

mysql>  CHANGE MASTER TO MASTER_HOST='192.168.1.150',
     MASTER_USER='repl_user',
     MASTER_PASSWORD='secretpassword',
     MASTER_LOG_FILE='mysql-bin.000001',
     MASTER_LOG_POS=847;

Step 4: Start SLAVE on Both Servers

Execute following command on both servers to start replication slave process.

mysql> SLAVE START;

MySQL Master-Master Replication has been configured successfully on your system and in working mode. To test if replication is working make changes on either server and check if changes are reflecting on other server.

This entry was posted in MySQL. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *