Master / Slave Mysql

//TODO a traduire

Set up MySQL master-slave replication

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)

Configure replication

A MySQL user is required on the master server (db01) to be used for replication.

    1. 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.

      1. # mysql -u root -p mysql> grant replication slave on *.* TO [replication_username]@’[private IP of db02]’ identified by '[some password]'; mysql> flush privileges; mysql> quit

    2. Edit the /etc/my.cnf file and add the following entries:

      1. bind-address = server-id = 1 log-bin = mysql-bin binlog-ignore-db = “mysql”

    1. After you have finished updating the /etc/my.cnf file, restart the MySQL service.

      1. #service mysqld restart

      2. 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.

    1. 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.

      1. # mysql -u root –p mysql> FLUSH TABLES WITH READ LOCK; mysql> SHOW MASTER STATUS; +------------------+--------------------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+--------------------------+------------------+ | mysql-bin.000010 | 10 | | mysql | +------------------+--------------------------+------------------+ 1 row in set (0.00 sec)

    1. Perform a database dump by using mysqldump as follows. list all the databases barring mysql and information_schema:

      1. # mysqldump -u root -p --databases [database-1] [database-2] ... > /root/db_dump.sql

    1. Aftre the database dump has completed, lift the read lock from the master (db01):

      1. # mysql -u root –p mysql> UNLOCK TABLES;

    1. Copy the database dump file to the slave server so that it can be restored. You can use the scp command to accomplish this:

      1. scp /root/db_dump.sql [private-IP-of-db02]:/root/

    1. On db02, edit the /etc/my.cnf file and add the following entries:

      1. bind-address = server-id = 2 master-host = [private-IP-of-db01] master-user = [replication-username] master-password = [replication-password] master-connect-retry = 60

    1. Import the db_dump.sql file copied earlier and restart the MySQL service.

      1. # mysql –u root –p < /root/db_dump.sql # service mysqld restart

    1. Complete the slave replication steps:

      1. # mysql -u root –p mysql> SLAVE STOP; mysql> CHANGE MASTER TO MASTER_HOST='[private-IP-of-db01]', MASTER_USER='[replication-username]', MASTER_PASSWORD='[replication-password]', MASTER_LOG_FILE='[file-listed-on-master-status]', MASTER_LOG_POS=[log-position-listed–on-master-status]; mysql> START SLAVE; mysql> SHOW SLAVE STATUS\G

      2. 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.

Test replication

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:

# mysql -u root -p mysql> create database testing; mysql> use testing mysql> create table users(id int not null auto_increment, primary key(id), username varchar(30) not null); mysql> insert into users (username) values ('foo'); mysql> insert into users (username) values ('bar'); mysql> exit

The changes should be visible on db02 immediately.