This tutorial describes how to set up database replication in MySQL. MySQL replication allows you to have an exact copy of a database from a master server on another server (slave), and all updates to the database on the master server are immediately replicated to the database on the slave server so that both databases are in sync. This is not a backup policy because an accidentally issued DELETE command will also be carried out on the slave; but replication can help protect against hardware failures though.I do not issue any guarantee that this will work for you!
1.Preliminary Note
In this tutorial I will show how to replicate the database exampledb from the server server1.example.com (master) with the IP address 192.168.0.100 to the server server2.example.com (slave) with the IP address 192.168.0.101. Both systems are running Ubuntu 9.10; however, the configuration should apply to almost all distributions with little or no modifications. The database exampledb with tables and data is already existing on the master, but not on the slave.
I'm running all the steps in this tutorial with root privileges, so make sure you're logged in as root:
sudo -u |
Please enable the root account on server2 (in case you haven't already done so) so that we can transfer the files from server1 to server2 with scp:
sudo apt-get install ssh |
server2:
passvd root |
2. Installing MySQL 5
If MySQL 5 isn't already installed on server1 and server2, install it now:
server1/server2:
aptitude install mysql-server mysql-client |
You will be asked to provide a password for the MySQL root user - this password is valid for the user root@localhost as well as root@server1.example.com / root@server2.example.com, so we don't have to specify a MySQL root password manually later on:
New password for the MySQL "root" user: <-- yourrootsqlpassword
Repeat password for the MySQL "root" user: <-- yourrootsqlpassword
3. Configuring the Master
To make sure that the replication can work, we must make MySQL listen on all interfaces on the master (server1), therefore we comment out the line bind-address = 127.0.0.1 in /etc/mysql/my.cnf:
server1:
vi /etc/mysql/my.cnf |
[...] # Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. #bind-address = 127.0.0.1 [...] |
Restart MySQL afterwards:
/etc/init.d/mysql restart |
netstat -tap | grep mysql |
that MySQL is really listening on all interfaces on the master:
root@server1:~# netstat -tap | grep mysql tcp 0 0 *:mysql *:* LISTEN 2166/mysqld root@server1:~# |
Now we set up a replication user slave_user that can be used by server2 to access the MySQL database on server1:
mysql -u root -p |
On the MySQL shell, run the following commands:
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'slave_password'; FLUSH PRIVILEGES; quit; |
Furthermore we have to tell MySQL for which database it should write logs (these logs are used by the slave to see what has changed on the master), which log file it should use, and we have to specify that this MySQL server is the master. We want to replicate the database exampledb, so we add/enable the following lines in /etc/mysql/my.cnf (in the [mysqld]section):
vi /etc/mysql/my.cnf |
[...] # The following can be used as easy to replay backup logs or for replication. # note: if you are setting up a replication slave, see README.Debian about # other settings you may need to change. server-id = 1 log_bin = /var/log/mysql/mysql-bin.log expire_logs_days = 10 max_binlog_size = 100M binlog_do_db = exampledb [...] |
Then restart MySQL:
/etc/init.d/mysql restart |
Next we lock the exampledb database on server1, find out about the master status of server1, create an SQL dump of exampledb (that we will import into exampledb on server2 so that both databases contain the same data), and unlock the database so that it can be used again:
mysql -u root -p |
USE exampledb; FLUSH TABLES WITH READ LOCK; SHOW MASTER STATUS; |
On the MySQL shell, run the following commands:
mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 19467 | exampledb | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
mysql> |
The last command should show something like this (please write it down, we'll need it later on):
Now don't leave the MySQL shell, because if you leave it, the database lock will be removed, and this is not what we want right now because we must create a database dump now. While the MySQL shell is still open, we open a second command line window where we create the SQL dump snapshot.sql and transfer it to server2 (using scp):
server1:
cd /tmp mysqldump -u root -pyourrootsqlpassword --opt exampledb > snapshot.sql scp snapshot.sql root@192.168.0.101:/tmp |
Afterwards, you can close the second command line window. On the first command line window, we can now unlock the database and leave the MySQL shell:
server2:
UNLOCK TABLES; quit; |
4. Configuring the Slave
Now we must configure the slave. Open /etc/mysql/my.cnf and make sure you have the following settings in the [mysqld] section:
Server2:
vi /etc/mysql/my.cnf |
[...] server-id=2 master-connect-retry=60 replicate-do-db=exampledb
server-id = 2 master-user =username master-password =password master-connect-retry =60 read-only = 1 relay-log-purge = 1 replicate-do-db = metro report-host =192.168.5.7 slave-skip-errors =all replicate-wild-ignore-table=metro.b//不同步的表,例如discuz中的cdb_sessions表 [...] |
The value of server-id must be unique and thus different from the one on the master!
Restart MySQL afterwards:
/etc/init.d/mysql restart |
Before we start setting up the replication, we create an empty database exampledb on server2:
mysql -u root -p |
CREATE DATABASE exampledb; quit; |
On server2, we can now import the SQL dump snapshot.sql like this:
/usr/bin/mysqladmin --user=root --password=yourrootsqlpassword stop-slave cd /tmp mysql -u root -pyourrootsqlpassword exampledb < snapshot.sql; quit; |
Now connect to MySQL again...
mysql -u root -p |
... and run the following command to make server2 a slave of server1 (it is important that you replace the values in the following command with the values you got from the SHOW MASTER STATUS; command that we ran on server1!):
CHANGE MASTER TO MASTER_HOST='192.168.0.100', MASTER_USER='slave_user', MASTER_PASSWORD='slave_password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=19467;
|
l MASTER_HOST is the IP address or hostname of the master (in this example it is 192.168.0.100).
l MASTER_USER is the user we granted replication privileges on the master.
l MASTER_PASSWORD is the password of MASTER_USER on the master.
l MASTER_LOG_FILE is the file MySQL gave back when you ran SHOW MASTER STATUS; on the master.
l MASTER_LOG_POS is the position MySQL gave back when you ran SHOW MASTER STATUS; on the master.
Finally start the slave:
START SLAVE; |
Then check the slave status:
SHOW SLAVE STATUS /G |
It is important that both Slave_IO_Running and Slave_SQL_Running have the value Yes in the output (otherwise something went wrong, and you should check your setup again and take a look at /var/log/syslog to find out about any errors):
See the next page.
Afterwards, you can leave the MySQL shell on server2:/q
That's it! Now whenever exampledb is updated on the master, all changes will be replicated to exampledb on the slave. Test it!
mysql> SHOW SLAVE STATUS /G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.100 Master_User: slave_user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 19467 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: exampledb Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 19467 Relay_Log_Space: 407 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 1 row in set (0.00 sec) mysql>
|