Master-slave data replication allows for replicated data to be copied to multiple computers for backup and analysis by multiple parties. Needed changes identified by a group member must to be submitted to the designated "master" of the node. This differs from Master-Master replication, in which data can be updated by any authorized contributor of the group.
This article provides steps for setting up MySQL master-slave database replication between two cloud servers. The operating system used for the examples in the article is CentOS 6, built from a Rackspace Cloud Servers base image.
Before you begin
The steps in this article use two servers named db01 and db02. Servers have two IP addresses. For the duration of the article, db01 is considered the master MySQL server (running in read-write mode), and db02 is considered the slave server (running in read-only mode).
If you already have a MySQL database running on the master node, a dump and restore into the slave node is required before configuring replication between them. You use the mysqldump command to dump a database into a file, then transfer it and restore it to the slave. After the necessary configuration has been performed, replication is in effect. For more information, see the Configure replication section.
Install MySQL (if not yet done)
A MySQL user is required on the master server (db01) to be used for replication.
- Run the following commands to set up the MySQL user, updatng the entries in brackets with strings or values you that you want to use with your setup.
- Edit the /etc/my.cnf file and add the following entries:
- After you have finished updating the /etc/my.cnf file, restart the MySQL service.
Before starting replication, the data on each server (master and slave) must be the same. To accomplish this duplication, dump the data from the master (db01) server and add it to the slave (db02) server, as instructed in the following.
- Use the following command to ensure that nothing can write to the master database during a database dump. Also note the filename and position of the binary log because you will need these values to complete the replication configuration on db02.
- Perform a database dump by using mysqldump as follows. list all the databases barring mysql and information_schema:
- Aftre the database dump has completed, lift the read lock from the master (db01):
- Copy the database dump file to the slave server so that it can be restored. You can use the scp command to accomplish this:
- On db02, edit the /etc/my.cnf file and add the following entries:
- Import the db_dump.sql file copied earlier and restart the MySQL service.
- Complete the slave replication steps:
The Slave_IO_State field should show "Waiting for master to send event". If it shows "Connecting to Master" please check your MySQL log file. By default it is (CODE)/var/log/mysqld.log(/CODE) but it may be configured differently on your system. As always (CODE)/etc/my.cnf(/CODE) will define the location of your log file.
To test the replication setup, create a new database and associated table on db01, and insert data to confirm that the changes are mirrored on db02. In the following example, the new database is called testing and the new table is calledusers:
The changes should be visible on db02 immediately.