How to setup MySQL replication on CentOS


logo-mysql-300x218In 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:

Login to the master server as user root

ssh root@IP_Address

and install MySQL server if it is not already installed

yum install mysql-server mysql

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

systemctl start mysql
systemctl enable mysql

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.

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

systemctl restart mysql

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:

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

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

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 do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to setup MySQL replication for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

RoseHosting
Manage your domains’ DNS records with ease using RoseHosting’s DNS service
Tutorials
Run WordPress+W3TotalCache with LEMP (Nginx, PHP-FPM+APC and MySQL) stack on CentOS 6 VPS for maximum performance
Tutorials
Install phpMyBackupPro on a CentOS 6 VPS
There are currently no comments.