MySQL replication allows you to have multiple copies of data on many systems and data is automatically copied from one database (Master) to another database (Slave). If one server goes down, the clients still can access the data from another (Slave) server database.
In this article, let us see how to configure MySQL Master-Slave replication. I am using the following two systems to in this how-to:
MySQL Master system : RHEL 5
Master IP Address : 192.168.1.250/24 MySQL Slave system : RHEL 5 Slave IP Address: 192.168.1.150/24 |
Setting up MySQL Master
Now install MySQL packages using the following command:
[root@server1 ~]# yum install mysql-server mysql -y |
Start mysqld service.
[root@server ~]# service mysqld start
[root@server ~]# chkconfig mysqld on |
Configure MySQL Master
Open /etc/my.cnf file and add the following lines under [mysqld] section:
[root@server ~]# vi /etc/my.cnf [mysqld] [mysqld_safe] |
Here unixmen is the database name to be replicated to the Slave system.
Once you are done, restart MySQL service:
[root@server1 ~]# service mysqld restart |
[root@server ~]# mysql -u root -p Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.1.69-log Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> STOP SLAVE; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> GRANT REPLICATION SLAVE ON *.* TO 'slavek'@'%' IDENTIFIED BY 'centoslave'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH TABLES WITH READ LOCK; Query OK, 0 rows affected (0.00 sec) mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 106 | unixmen | | +------------------+----------+--------------+------------------+ 1 row in set (0.01 sec) mysql> exit Bye |
Note down the file(mysql-bin.000001) and position number (106), you would need these values later while configuring slave servers.
Backup Master server database
Enter the following command to dump all Master databases and save them. We will transfer these databases to Slave server later:
[root@server ~]# mysqldump --all-databases --user=root --password --master-data > masterdatabase.sql |
This will create a file called masterdatabase.sql. This will take some time depending upon the databases size.
Again login to MySQL as root user and unlock the tables:
[root@server ~]# mysql -u root -p Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> UNLOCK TABLES; mysql> quit |
Copy the masterdatabase.sql file to your Slave server. Here, I copy this file to /home folder. So the command should be:
[root@server ~]# scp masterdatabase.sql root@192.168.1.150:/home |
We have done Master side installation. Now we have to start on Slave side.
Setting up MySQL Slave
Install MySQL packages on Slave server:
[root@server1 ~]# yum install mysql-server mysql -y |
Start mysqld service.
[root@server ~]# service mysqld start
[root@server ~]# chkconfig mysqld on |
Seting up MySQL Root password:
Configure MySQL Slave
Open the file /etc/my.cnf and add the following entries under [mysqld] section as shown below. Replace the database name and master server IP Address with your own:
[root@server ~]# vi /etc/my.cnf [mysqld] server-id = 2 master-host=192.168.1.250 master-connect-retry=60 master-user=slavek master-password=centoslave replicate-do-db=unixmen relay-log = /var/lib/mysql/mysql-relay-bin relay-log-index = /var/lib/mysql/mysql-relay-bin.index log-error = /var/lib/mysql/mysql.err master-info-file = /var/lib/mysql/mysql-master.info relay-log-info-file = /var/lib/mysql/mysql-relay-log.info log-bin = mysql-bin [...] |
Here 192.168.1.250 is Master server IP address, slavek is Master server database user, centoslave is password of user slavek, unixmen is Master database name.
Save and exit the file.
Import the master database:
[root@server ~]# mysql -u root -p < /home/masterdatabase.sql Enter password: [root@server ~]# service mysqld restart |
Now log in to MySQL as root user and tell the Slave server to where to look for Master log file which is we have created on Master server using the command SHOW MASTER STATUS; (File – mysql-bin.000001 and Position – 106). Make sure that you changed the Master server IP address, username and password as your own:
[root@server ~]# mysql -u root -p Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SLAVE STOP; mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.250', MASTER_USER='slavek', MASTER_PASSWORD='centoslave', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=106; mysql> SLAVE START; mysql> SHOW SLAVE STATUS\G; |
Test MySQL Replication
Master side:
[root@server ~]# mysql -u root -p Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database unixmen; mysql> use unixmen; mysql> create table sample (c int); mysql> insert into sample (c) values (1); mysql> select * from sample; mysql> |
Slave side:
[root@server ~]# mysql -u root -p Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use unixmen; Database changed mysql> select * from sample; mysql> |
That’s it. Now the tables created in the Master server are automatically replicated to the Slave server.