Configure Master-Master MySQL Database Replication

MySQL Master-Master replication adds speed and redundancy for active websites. With replication, two separate MySQL servers act as a cluster. Database clustering is particularly useful for high availability website configurations. mysql-master-master-replication-title

Step 1. Installing MySQL Server

The first thing you need to do is to install MySQL on Server A and Server B. If you already have a MySQL instance up and running on one of them, just upgrade it to the latest version you feel confident to use and then install that same identical version to the other one.

As soon as you’re finished, edit the main config file – /ProgramData/MySQL/MySQL Server 5.x/my.ini on Windows, /etc/mysql/my.cnf on Linux – and type-in the following settings, replacing the existing ones if present:

Server A

server-id=1
log-bin="mysql-bin"
binlog-ignore-db=test
binlog-ignore-db=information_schema
replicate-ignore-db=test
replicate-ignore-db=information_schema
relay-log="mysql-relay-log"
auto-increment-increment = 2
auto-increment-offset = 1
expire_logs_days    = 10
max_binlog_size     = 100M
log_slave_updates   = 1

Server B

server-id=2
log-bin="mysql-bin"
binlog-ignore-db=test
binlog-ignore-db=information_schema
replicate-ignore-db=test
replicate-ignore-db=information_schema
relay-log="mysql-relay-log"
auto-increment-increment = 2
auto-increment-offset = 2
expire_logs_days    = 10
max_binlog_size     = 100M
log_slave_updates   = 1

As you can see the cfg is almost identical, except for the server-id and auto-increment-offsets values.
Please notice that this configuration will replicate/mirror each and every db with the sole exception of test and information_schema. You might also want to keep out other databases such as mysql or other ones: if that’s the case, just add those you want to exclude by adding a binlog-ignore-db and replicate-ignore-db command for each one of them.

Just delete each and every binlog-ignore-db and replicate-ignore-db replacing them with binlog-add-db and replicate-add-db instead, specifying only the database you need/want to mirror.

Once completed, restart the MySQL server:

sudo service mysql restart

Step 2. Create the Replicator User(s)

Next thing you need to do is to create the replicator user in either Server A and Server B. You can do that using MySql shell using the following commands:

mysql -u root -p [root_password]
CREATE USER 'replicator'@'%' IDENTIFIED BY '[replicator_password]';
GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%' IDENTIFIED BY '[replicator_password]';

Step 3. Configure replication from Server A to Server B

The next thing we need to to is to replicate all the commands received by Server A to Server B: in other words, we need to configure Server B as a slave of Server A.

Login to Server A and run following command

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      107 | example      | test, informatio |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

You’re going to use the File and Position column values in a few

Now connect to Server B  and type the following sql commands:

STOP SLAVE;
CHANGE MASTER TO MASTER_HOST = 'Server A IP Address', MASTER_USER = 'replicator', MASTER_PASSWORD = '[replicator_password]', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 107; 
START SLAVE;

Replace Server A IP Address with Server A numerical IPv4 address, [replicator_password] with the password you choose for your replicator user during Step 2, mysql-bin.000001 and 107 with the File and Position column values retrieved above.

Step 4. Configure replication from Server B to Server A

Now you need to do the same thing from the other side. That is, replicate all the commands received by Server B to Server A, configuring the latter as a slave of the former. Connect to Server B and type the following sql command:

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      107 | example      | test, informatio |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

Now connect to Server A and type the following sql commands:

STOP SLAVE;
CHANGE MASTER TO MASTER_HOST = 'Server B IP Address', MASTER_USER = 'replicator', MASTER_PASSWORD = '[replicator_password]', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 107; 
START SLAVE;

Reboot both servers now

Step 5: Test the Replication

Your MySQL Servers are now in Master-Master replication mode. You can test it in a number of ways. such as:

  1. create a new database using your favorite tool (or via MySQL console) on Server A, then connect to Server B to see if it’s also there.
  2. create a new user on Server B, then connect to Server A to see if it’s also there.
  3. issue some insert, delete and/or update query commands from Server A, and check that the same data will be inserted, deleted and/or modified also in Server B.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: