How To Set Up Database Replication In MySQL On Ubuntu
2010-07-26 20:58
561 查看
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:
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:
server2:
2. Installing MySQL 5
If MySQL 5 isn't already installed on server1 and server2, install it now:
server1/server2:
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:
Restart MySQL afterwards:
Then check with
that MySQL is really listening on all interfaces on the master:
Now we set up a replication user slave_user that can be used by server2 to access the MySQL database on server1:
On the MySQL shell, run the following commands:
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):
Then restart MySQL:
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:
On the MySQL shell, run the following commands:
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:
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:
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:
The value of server-id must be unique and thus different from the one on the master!
Restart MySQL afterwards:
Before we start setting up the replication, we create an empty database exampledb on server2:
On server2, we can now import the SQL dump snapshot.sql like this:
Now connect to MySQL again...
... 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!):
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:
Then check the slave status:
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!
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 |
sudo apt-get install ssh |
passvd root |
If MySQL 5 isn't already installed on server1 and server2, install it now:
server1/server2:
aptitude install mysql-server mysql-client |
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 [...] |
/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:~# |
mysql -u root -p |
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'slave_password'; FLUSH PRIVILEGES; quit; |
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 [...] |
/etc/init.d/mysql restart |
mysql -u root -p |
USE exampledb; FLUSH TABLES WITH READ LOCK; SHOW MASTER STATUS; |
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> |
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 |
server2:
UNLOCK TABLES; quit; |
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表 [...] |
Restart MySQL afterwards:
/etc/init.d/mysql restart |
mysql -u root -p |
CREATE DATABASE exampledb; quit; |
/usr/bin/mysqladmin --user=root --password=yourrootsqlpassword stop-slave cd /tmp mysql -u root -pyourrootsqlpassword exampledb < snapshot.sql; quit; |
mysql -u root -p |
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_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; |
SHOW SLAVE STATUS /G |
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> |
相关文章推荐
- How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 14.04 LTS
- How to set up a simple mail server on Debian in 5 easy steps
- How To Set Up a Private Docker Registry on Ubuntu 14.04
- How to set up IceScrum on Ubuntu 12.04
- How to set up Solr on Ubuntu 10.04 (or whatever)
- How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu
- How To Set Up vsftpd on Ubuntu 12.04
- How to Set Up Apache Virtual Hosts on Ubuntu 12.04 LTS
- How to Set Up SSH Keys In Ubuntu
- How To Set Up HTTP Authentication With Nginx On Ubuntu 12.10
- How to Set Up vsftpd on Ubuntu 12.04
- How to set up Eclipse with Android SDK on Ubuntu linux 9.04/9.10 (updated)
- How to set up an FTP server on Ubuntu 14.04
- How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS
- How to Set Up Replication--MySQL Concept
- Chinese Input Methods in Ubuntu 14, 13, 12, 11, and 10(How to Set Up Ubuntu Chinese IMEs)