Setup MariaDB Master-Slave Replication In CentOS 7
Why MariaDB Replication?
As you may know, MariaDB is a drop in replacement for MySQL. It is a robust, scalable and reliable SQL server that comes rich set of enhancements.
MariaDB replication is a method to store multiple copies of data on many systems, and the data will automatically be 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 MariaDB Master-Slave replication in CentOS 7. This method will work on all linux distributions, including RHEL, CentOS, Ubuntu, and openSUSE etc. All you need to know is how to install MariaDB in the specific distribution you use.
Here is my testing systems details:
Master IP Address: 192.168.1.150/24
MariaDB Slave: CentOS 7 64bit Minimal Server
Slave IP Address: 192.168.1.151/24
Install MariaDB on Master and Slave server
Run the following command on Master and Slave server to install MariaDB.
yum install mariadb-server mariadb
Start MariaDB service and let it to start automatically on every reboot:
systemctl start mariadb systemctl enable mariadb
Set MariaDB root password on Master and Slave server
Enter the following command to setup mysql root user password:
mysql_secure_installation
Sample output:
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] y ## Enter Y and press Enter New password: ## Enter new password Re-enter new password: ## Enter password again Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] y ## Enter Y and press Enter ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] y ## Enter Y and press Enter ... Success! By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] y ## Enter Y and press Enter - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y ## Enter Y and press Enter ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!
After installing MariaDB on both servers, let us jump into the replication setup.
First, we will configure Master server.
Configure MariaDB Master
The first and foremost step is to allow mysql default port “3306” through Firewall or Router.
As we use CentOS 7, we can allow the port as shown below.
firewall-cmd --permanent --add-port=3306/tcp
Reload firewall rules using command:
firewall-cmd --reload
Then, Edit /etc/my.cnf file,
nano /etc/my.cnf
And add the following lines under [mysqld] section:
[mysqld]
datadir = /var/lib/mysql
server_id=1
log-basename=master
#log-bin
binlog-format=row
binlog-do-db=DATABASE
log_bin = /var/log/mysql/mysql-bin.log
bind-address = 0.0.0.0
socket = /var/lib/mysql/mysql.sock
log-error = /var/log/mysqld/mysqld.log
log-bin = mysql-bin
log-warnings=3
sql_mode="NO_ENGINE_SUBSTITUTION"
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
Backup Master server database and transfer it to the Slave
Configure MariaDB Slave
We have done Master side installation. Now we have to start on Slave side. Install MySQL packages on Slave server as stated in the MariaDB installation section. Also, don’t forget to allow the port “3306” through the firewall/router.
Then, Edit file /etc/my.cnf,
nano /etc/my.cnf
And add the following entries under [mysqld] section as shown below.
[mysqld]
server-id = 2
replicate-do-db=DATABASE
[...]
Here, DATABASE is the database name of Master server. Be mindful that you should use different server-id for both master and slave servers.
Save and exit the file.
Then, Import the master database using command:
mysql -u root -p < /root/masterdatabase.sql
Remember, we already have copied the masterdatabase.sql file from the master server to /home/ directory of the slave server.
Restart MariaDB service to take effect the changes.
systemctl restart mariadb
Now, log in to MariaDB as root user using command:
mysql -u root -p
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 –mariadb-bin.000001 and Position – 460). Make sure that you changed the Master server IP address, username and password as your own:
MariaDB [(none)]> STOP SLAVE;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.1.150', MASTER_USER='sk', MASTER_PASSWORD='centos', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=460;
Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> SLAVE START;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.150
Master_User: sk
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 460
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 531
Relay_Master_Log_File: mariadb-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: unixmen
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: 460
Relay_Log_Space: 827
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
Test MariaDB Replication
Master side:
Go to your MariaDB master server and log in to mysql prompt using command:
mysql -u root -p
Then, create a database called DATABASE, and add some tables and entries in it. Be mindful that the newly created name should be same as in the my.cnf file of both Master and Slave server.
MariaDB [(none)]> create database DATABASE;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use DATABASE;
Database changed
MariaDB [DATABASE]> create table sample (c int);
Query OK, 0 rows affected (0.01 sec)
MariaDB [DATABASE]> insert into sample (c) values (1);
Query OK, 1 row affected (0.01 sec)
MariaDB [DATABASE]> select * from sample;
+------+
| c |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
MariaDB [DATABASE]>
Slave side:
Now, go to slave server and check the above created entries have been replicated in slave server database.
Log in to MariaDB prompt as root user:
mysql -u root -p
Then, run the following commands to verify whether the entries have been replicated correctly.
MariaDB [(none)]> use DATABASE;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [DATABASE]> select * from sample;
+------+
| c |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
MariaDB [DATABASE]>
That’s it. Now the tables created in the Master server have been automatically replicated to the Slave server.
Comments
Post a Comment