How to setup MySQL replication on CentOS

How to setup MySQL replication on CentOS

In this tutorial we will show you how to setup basic MySQL replication on two CentOS 7 servers, but the same steps should work on all RPM based systems. We will use two test CentOS 7 virtual servers for this tutorial with the following IP addresses:


MASTER: 123.456.789.1
SLAVE: 123.456.789.2

Master Server:

1. Login via SSH

Login to the master server as user root

ssh root@IP_Address

2. Install MySQL server

and install MySQL server if it is not already installed

yum install mysql-server mysql

3. Start the MySQL server

Start the MySQL server and enable it to start at boot time

systemctl start mysql
systemctl enable mysql

4. Configure MySQL server

Next, open the MySQL configuration file with a text editor and add the following lines

vi /etc/my.cnf
server-id = 1
binlog-do-db=database
relay-log = mysql-relay-bin
relay-log-index = mysql-relay-bin.index
log-bin = mysql-bin

Don’t forget to replace ‘database‘ with the actual name of the database that you want to replicate to the slave server.

5.  Restart MySQL server

Save the configuration file and restart the MySQL server for the changes to take effect

systemctl restart mysql

6. Create MySQL user for the slave server and configure it

Login to the MySQL server as user root and create a user that will be used by the slave

mysql -u root -p
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slaveuser'@'%' IDENTIFIED BY 'PASSWORD'
mysql> FLUSH PRIVILEGES;
mysql> FLUSH TABLES WITH READ LOCK;
mysql> SHOW MASTER STATUS;
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      245 | database     |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

mysql> exit

We will need the master’s coordinates that are going to be used for the slave server, so write them down.

File: mysql-bin.000001
Position: 245

Replace ‘PASSWORD‘ with an actual strong password.

Now, if the database you want to replicate is not empty, create a dump from the database

mysqldump -u root -p --master-data database > /root/database.sql

and transfer the dump file to the slave server

rsync -Waq -e ssh /root/database.sql 123.456.789.2:/root

Slave Server:

7. Login via SSH and install MySQL Server on the Slave Server

Next step is to configure the slave server. Login to the slave server via SSH as user root and install MySQL server as we did on the master server.

After the MySQL server is installed and started, open its configuration file and add the following lines:

vi /etc/my.cnf
server-id = 2
master-host=123.456.789.1
master-connect-retry=60
master-user=slaveuser
master-password=PASSWORD
replicate-do-db=database
relay-log = mysql-relay-bin
log-bin = mysql-bin

8. Restart MySQL Server

Save the file and restart the MySQL server for the changes to change effect.

systemctl restart mysql

Import the MySQL dump file we created on the master server

mysql -u root -p < /root/database.sql

9. Configure MySQL Server

Login to MySQL as user root stop the slave and configure it where to look for the ‘master log file’

mysql -u root -p
mysql> slave stop;
mysql> CHANGE MASTER TO MASTER_HOST='123.456.789.1', MASTER_USER='slaveuser', MASTER_PASSWORD='PASSWORD', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=245;
mysql> slave start;

Use the MySQL username and password we created on the master server.

You can check the slave status by executing the following MySQL command:

mysql> show slave status;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 123.456.789.1
                  Master_User: slaveuser
                  Master_Port: 3306
              Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: database

If ‘Slave_IO_State‘ reports ‘Waiting for master to send event’ the replication is successful.

That is all. It’s that easy. If you need more information on how to setup a replication master configuration you can find it on MySQL’s official website.


Of course you don’t have to setup MySQL replication on CentOS, if you use one of our MySQL Hosting services, in which case you can simply ask our expert Linux admins to setup MySQL replication on CentOS for you. They are available 24×7 and will take care of your request immediately. You can also check new updates on How to Install MySQL on CentOS 7.

PS. If you liked this post on how to setup MySQL replication on CentOS, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment