Step 0 Preparation
- Make sure you have two ubuntu systems with mysql installed.
- The master server is the main server, which is daily in use. and the slave server means a backup server.
- Modify
/etc/mysql/my.cnf
in both master and slave servers and uncomment#bind-address
to make remote access possible. - Use
mysql -u root -p
to login the master server and authorize withgrant replication slave,reload,super on *.* to ${username} @${slave server ip} identified by '${pwd}';
Note: You can test with
mysql -u ${username} -h ${master server ip} -p
on your slave server machine.
Step 1 Set up Master Server
- Run
sudo nano /etc/mysql/my.cnf
and modify the file like (default commented)
server-id = 100
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = ${the db name you want to configure}
binlog_ignore_db = mysql
- Run
service mysql restart
to restart msyql service. - Use
mysql -u root -p
to login mysql and entershow master status;
and you may see
mysql>show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 107 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
Please remember the File and Position.
Step 2 Set up Slave Server
- Run
sudo nano /etc/mysql/my.cnf
and modify the file like (default commented)
server-id = 111
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = ${the db name you want to configure}
binlog_ignore_db = mysql
- Run
service mysql restart
to restart msyql service. - Use
mysql -u root -p
to login mysql and runstop slave;
- Enter
change master to master_host='${master server ip}',master_user='${username}',master_password='${pwd}',master_log_file='${File}',master_log_pos=${Position};
- Then start slave by
start slave;
- Run
show slave status;
to test whether successfully configure. You may see as
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
if these two show YES, well done!