Database Replication in MySQL
Database Replication in MySQL
Database Replication in MySQL
By Ian Gilfillan
An introduction to replication
Recently, while having the knots pounded out of my body during a particularly
painful shiatsu lesson, I reflected on what put them there in the first place. Yes, 'the
database' was once more to blame. A busy database I work with saw one of its
tables jump from 3GB to 7GB overnight, as we imported archive data. As expected,
this had some performance impact. Unfortunately, I had not expected quite the
knock, and it turned out that this single database server could no longer handle the
load. No matter how much more I tried to optimize the queries, tweak the variables
or bump up the query cache, it was not enough. The machine could not take any
more memory, and a hardware upgrade would do little good (at least with the kind
of budget I have to play with). However, MySQL does not claim to be enterprise-
ready for nothing, and Yahoo and other high-volume users of MySQL certainly do not
run on one database server. There are a number of techniques to handle high
volumes, one of which I will introduce this month - MySQL replication (I will look at
others in future articles).
Replication allows you to take one database, make an exact copy of it on another
server, and set one of them (the slave) to take all its updates from the other (the
master). The slave reads the master's binary logs, which store all statements that
change a database, and repeats these on its database, keeping the two in exact
sync. Since a replicating database simply repeats statements, the databases are not
necessarily exactly in sync, and advanced users can take advantage of this. That is a
topic for another article however, and we will look at simple replication this month -
getting one database to be an exact copy of another one.
• Make a backup of the database. You will use this to start the slave server.
Note the comments about the binary log above. You can also skip this step if
you use the LOAD DATA FROM MASTER statement, but see the comments
about locking the master below first.
The slave user and slave password are those to which you set when you
granted REPLICATION SLAVE permission on the master. The server-id must
be a unique number, different to the master or any other slaves in the
system. There are also two other options: master-port, used if the master is
running on a non-standard port (3306 is default), and master-connect-retry,
a time in seconds for the slave to attempt to reconnect if the master goes
down. 60 seconds is default.
• Restore the data from the master, either as you would normally restore a
backup, or with the statement LOAD DATA FROM MASTER. The latter will lock
the master for the duration of the operation, which could be quite lengthy, so
you may not be able to spare the downtime.
Replication in action
Once the slave has started, replication should begin. Besides the obvious SELECT
queries, you can make sure this is working correctly with the following statements:
This is a mature slave that has been running a while. The master is already on the
54th binary log. You can see if the slave is running correctly by looking at the
Slave_IO_Running and Slave_SQL_Running. The most important field is the
Last_error field.
The above is from a master that has been running a while. It is already on binlog 54.
MySQL will not allow you to remove a log that the master is still using though.
These options can all be used multiple times in a single configuration. A couple of
other useful options:
A few complexities
Replication is not the salvation it seems to be at first glance, and will improve greatly
in future versions of MySQL, but it is a useful addition to a DBA's armory. I hope that
you will find replication easy to get going. Good luck!