Mysql – How to setup two thesql servers in Synchronization mode

database-administrationMySQLsynchronization

Infrastructure

I have two database servers, running on two different boxes. One works as a primary database for my web application and other works as secondary database. Secondary data base is updated by replication facility in mysql server. Primary database server works as master and secondary database server works as client.

Requirement

currently, my web application only interact with primary database. I want to add a functionality that will enable the application to use secondary database server if primary is down by some reason. Now, I need some tool/program to sync the changes made in secondary database to the primary database when it is up.

Basically I am looking for a feature that will enable my databases to be in sync all the time without fail. Please suggest me, how can I achieve this?

Thanks,

Vijay

Best Answer

You need to do something called multi-master replication (aka master-master replication).

There are a number of guides that walk you through the process.

There are two ways to use a master-master replication setup. The improper way is to write (INSERT/UPDATE etc.) to both masters at the same time. Although MySQL provides configuration parameters that can help you if you decide to do this, I suggest you not rely on this behavior. Particularly, don't think that you can split your writes across the machines as some kind of load balancing, there is no such thing. All writes will occur on all servers.

The proper way is to use one master as a hot standby and only write to one master at a time. Trust me this is will save you a lot of heartache when things go wrong. There is even a tool, MMM (Multi-Master Replication Manager for MySQL) that can assist with monitoring and failover for this. It does require a 3rd server to monitor the masters though.

One final thing. Do NOT, I repeat, DO NOT try to "scale" outward by adding additional masters to create a loop. Like I said earlier, this is not scaling at all since every server must do every write from every other server. It is also a complete nightmare to repair replication for a non-transient failure of one of the masters. The loss of one server "breaks" every server in the loop after it, because they no longer are getting updates from the servers downstream from them.