This tutorial will go through the setup of MySQL database replication. I will also talk about how to get everything working smoothly again after a server crash, or if you wish to switch databases. I will try to explain what is going on behind the scenes for every step (something I've found missing from other tutorials). This is written specifically for MySQL 5.0 on Centos 6, but should be very similar on other Linux distributions. It should also work this way with MySql 5.x.
The Scenario
We have 2 servers, one of which is a Master and the other which is a Slave. We tell the Master that it should keep a log of every action performed on it. We tell the slave server that it should look at this log on the Master and whenever something new happens, it should do the same thing.You should follow the instructions below with two console windows open - one for the Master and one for the Slave. Also note that I will capitalize the first letters of Master and Slave to indicate I am talking about the servers.
Configuring the Master
First of all, we need to create a user on the Master server that the Slave will connect as. I call mine 'repuser'. Log into mysql as root and create the user:
#mysql -u root -p (log into MySQL)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repuser'@'%' IDENTIFIED BY ‘repuserpw’; FLUSH PRIVILEGES;
Now, we should edit the my.cnf file (usually in /etc/my.cnf), in the [mysqld] section and tell MySQL that it's going to be a Master:
log-bin = /home/mysql/logs/mysql-bin.log
binlog-do-db=repdatabase
server-id=1
The first line tells MySQL to start writing a log, and tells it where to write the log. Make sure this directory is empty of all replication logs, especially if you're starting again after replication has already been used. The second line chooses the database to write the log for. You should change this to your database. The third line gives the server an ID (to distinguish it from the Slave. You should also make sure skip-networking has not been enabled.You should now restart the Master:
#/etc/rc.d/init.d/mysqld restart OR
#service mysqld
restart
Configuring the Slave
Again, we should change the /etc/my.cnf of the Slave server, in the [mysqld] section:
server-id=2
master-host=192.168.1.222
master-connect-retry=60
master-user=repuser
master-password=repuserpw
replicate-do-db=repdatabase
relay-log = /var/lib/mysql/slave-relay.log
relay-log-index = /var/lib/mysql/slave-relay-log.index
Line 1 gives the Slave its unique ID. Line 2, tells the Slave the I.P address of the Master server - so you need to change the I.P here.The remaining lines set a retry limit, and
tell the Slave the user, password and database it needs to replicate. We also tell the slave what to use as its relay log. It's best to set this directly, or MySQL will create the name from the hostname and should you change
hostname, replication will fail.You should also make sure skip-networking has not been enabled.You should now restart the Slave:
#/etc/rc.d/init.d/mysqld restart OR
#service mysqld
restart
Getting the data onto the Slave
On the Master...
I'm assuming you have a live Master server, and an as yet empty Slave server. This stage depends on whether data is constantly being added to the Master. If so, we will have to prevent all database access on the Master so nothing can be added. This means your server will hang during the next step. If no data is being added to the server, you can skip this step. On the Master server, log into MySQL and do the following:
#mysql -u root -p (log into MySQL)
mysql>FLUSH TABLES WITH READ LOCK;
Now we will use
mysqldump to get the data out. So, still on the Master server:
#mysqldump repdatabase -u root -p > /tmp/database.sql;
Make sure you change repdatabase to your database name and you wll now have a file called database.sql in your temp directory.
On the Slave...
Now we need to copy over the backup file. On the Slave run the following:
#scp root@192.168.1.222:/tmp/database.sql /tmp/
Make sure 192.168.1.222 is the I.P of the Master. This will copy the file from the Master and put it in your home directory on the Slave. Now we just need to import into MySQL:
#mysql -u root -p (log into MySQL)
mysql>CREATE DATABASE `repdatabase`;
mysql>exit;
#mysql -u root -p repdatabase </tmp/database.sql
Ready to rumble...
On the Master...
Now we're ready to kick things off. We need to find the position the Master is at in the logs. So, log into MySQL and run the following:
#mysql -u root -p (log into MySQL)
mysql>SHOW MASTER STATUS;
This should give you an output along these lines:
+---------------------+----------+-----------------------------+-------------------+
| File | Position | Binlog_Do_DB
| Binlog_Ignore_DB |
+---------------------+----------+-----------------------------+-------------------+
| mysql-bin.000001 | 21197930 | my_database,my_database |
|
+---------------------+----------+-----------------------------+-------------------+
Keep that on-screen.
On the Slave...
Log into MySQL and do the following:
#mysql -u root -p (log into MySQL)
mysql> slave stop;
mysql>CHANGE MASTER TO MASTER HOST=’192.168.1.222’, MASTER
USER=’repuser’,MASTER PASSWORD=’repuserpw’, MASTER LOG FILE=’mysql-bin.000001’,MASTER LOG POS=21197930;
mysql>slave start;
mysql>exit;
First we stop the Slave. Then we tell it exactly where to look in the Master log file. We use the values for our previous SHOW MASTER STATUS; command on the Master. You should change 192.168.1.222
to the I.P of the Master, and change the user and password accordingly.The Slave will now be waiting. So all that's left is to...
Back on the Master...
We should already be logged into MySQL, so all you have to do is:
#mysql -u root -p (log into MySQL)
mysql> unlock tables;
To release the tables from lock.Note you only have to do this if you previously ran FLUSH TABLES WITH READ LOCK;
And the recovery part?
Several times it's happened to me that a server has crashed, or a hostname changed or whatever and I've had a real trouble getting replication to work again. The solution has been to clear out the logs.
On the Slave
Clear out any replication logs from /var/lib/mysql or whever the logs are being stored, as stated in my.cnf. This usually does the trick:
# rm *relay*
# rm master.info
On the Master
Again, get rid of the logs, as per where they are stored in my.cnf. For me it's the following:
# cd /home/mysql/logs/
# rm -f *
This should give you a fresh start on things. You can now start again from the beginning...
Final Notes
My database doesn't use InnoDB tables - it's all MyISAM. However, the MySQL manual recommends adding this to my.cnf for InnoDB databases:
innodb_flush_log_at_trx_commit=1
sync_binlog=1