MySQL: How do you set up master-master replication in MySQL? (CentOS, RHEL, Fedora)

Setting up master-master replication in MySQL is very similar to how we set up master/slave replication. You can read up about how to setup master/slave replication in my previous post: How to set up master/slave replication in MySQL. There is obviously pros and cons about using master/master replication. But this is not a post which discuses advantages and disadvantages for using master/master replication. One of the differences between master/master set up and master/slave is that in master/master set up, you have true redundancy. If one server dies, second server can take all the inserts/selects. In master/slave setup, if master dies, you will have to go through steps to make slave become the master. Master/master set up we are going to set up is essentially master/slave and slave/master. Meaning, if you had two servers, db0 and db1, your setup will be db0(master)/db1(slave) and also db0(slave)/db1(master). Here are some assumptions:

Master1 server ip: 10.0.0.1
Master2 server ip: 10.0.0.2
Slave username: slaveuser
Slave pw: slavepw
Your data directory is: /usr/local/mysql/var/

Let us go through the steps you must take on Master1 to enable it to act as master and slave by using following configuration which goes under [mysqld] section:

# let's make it so auto increment columns behave by having different increments on both servers
auto_increment_increment=2
auto_increment_offset=1
# Replication Master Server
# binary logging is required for replication
log-bin=master1-bin
binlog-ignore-db=mysql
binlog-ignore-db=test
# required unique id between 1 and 2^32 - 1
server-id = 1
#following is the slave settings so this server can connect to master2
master-host = 10.0.0.2
master-user = slaveuser
master-password = slavepw
master-port = 3306

Following is the configuration which goes on master2 under [mysqld] section:

# let's make it so auto increment columns behave by having different increments on both servers
auto_increment_increment=2
auto_increment_offset=2
# Replication Master Server
# binary logging is required for replication
log-bin=master2-bin
binlog-ignore-db=mysql
binlog-ignore-db=test
# required unique id between 1 and 2^32 - 1
server-id = 2
#following is the slave settings so this server can connect to master2
master-host = 10.0.0.1
master-user = slaveuser
master-password = slavepw
master-port = 3306

On master1 server, go to mysql> prompt and add the appropriate user:

mysql> grant replication slave on *.* to slaveuser@'10.0.0.2' identified by 'slavepw';

On master2 server do the same but allow right ip:

mysql> grant replication slave on *.* to slaveuser@'10.0.0.1' identified by 'slavepw';

Restart both of the master servers and check slave status:

mysql> show slave status\G

That is all you have to do to set up the replication. Of course there are a lot more configuration options but this should get your replication going and you can tweak from here on.

————————————-
DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.

(3 votes, average: 3.33 out of 5)
Loading ... Loading ...

5 Responses to “ MySQL: How do you set up master-master replication in MySQL? (CentOS, RHEL, Fedora) ”

  1. March 1st, 2008 | 1:24 am

    Hi

    I believe, that the server-id should be different in the two master setup’s. One should have server-id=1 and the other server-id=2 or something similar.

    But thanks for a nice roundup!

    Karsten

  2. March 1st, 2008 | 1:50 am

    Karsten,

    Good catch. Thats what I get for copying pasting :) It has been corrected.

  3. Michael S Moody
    March 3rd, 2008 | 6:10 am

    I generally find it better to do auto-increment = 10, instead of odd/evens like this, in case I need to swap in another master with a different server-id on a regular basis. This gives me 10-19 as possibiliets for id’s, vs just even/odd.

    Michael

  4. March 7th, 2008 | 9:48 am

    [...] offers answers to the question, how do you set up master-master replication in MySQL?, laying out the basics of this [...]

  5. April 17th, 2008 | 9:01 am

    [...] following simple directions. You can learn setting up two type of replication MySQL offers here:  master-master replication and master-slave replication. Once you have replication set up, you can start playing with it [...]

Leave a reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image