Sometimes we may need to replicate data from one MySQL DB server to one or more MySQL DB servers automatically. Basically, database administrators perform database-related tasks such as replication, clustering, and auto-failover. Managing MySQL database servers and performing replication tasks are the responsibilities of database experts. In this article, we will learn how to set up master-slave MySQL database replication in Ubuntu 20.04. In this article, we will use MySQL 8 for replication.
In this example, the following servers are being used.
Installation of MySQL is a simple and straightforward process. In the Ubuntu system, MySQL can be installed using the APT package repository. Before installation, update the Ubuntu package index using the following command.
$apt update
Install MySQL server package using the command below.
$apt install mysql-server -y
Fresh installation of MySQL server leaves it non-secured. Run the script “mysql_secure_installation” to change some of the less secure default options such as remote root logins, test database, sample users, etc.
$ mysql_secure_installation
Start and enable MySQL service
$ systemctl start mysql
$ systemctl enable mysql
Now we need to make some changes on the master server for replication. Edit the mysqld.conf file under the directory /etc/mysql/mysql.conf.d and add the following lines. Remember to add the configuration variables under the [mysqld] section.
$ vi /etc/mysql/mysql.conf.d/mysqld.conf
[mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql log-error = /var/log/mysql/error.log server-id = 1 log-bin = /var/log/mysql/mysql-bin.log tmpdir = /tmp binlog_format = ROW max_binlog_size = 800M sync_binlog = 1 expire-logs-days = 5 slow_query_log=1 slow_query_log_file=/var/lib/mysql/mysqld-slow.log
Where:
Also, comment the bind-address as:
#bind-address =127.0.0.1
Restart mysql service
$systemctl restart mysql
Now we need to create a database user in a master node which will be used by slaves when connecting. Log in to the database using the root user and create the user.
$ mysql -u root -p
$ mysql> CREATE USER replication_user@192.168.178.137 IDENTIFIED BY 'StrongP@ssw0rd';
Note: Specifying IP address while creating user will accept MySQL connections only from the specified host. Connections from other hosts will be refused. In this example, I have used my MySQL slave server’s IP address. You may need to change this IP address to your environment accordingly.
The user needs REPLICATION SLAVE privileges to be able to replicate the MySQL data. Use the following query to grant the privileges.
mysql> GRANT REPLICATION SLAVE on *.* to replication_user@192.168.178.137 ;
Verify the privileges granted by using the following query.
mysql> SHOW GRANTS FOR replication_user@192.168.178.137;
We have already installed the MySQL server in the previous step. Now we will change some configuration files to enable the replication process. Edit the mysqld.conf file under /etc/mysql/mysql.conf.d directory and add the following contents.
$vi /etc/mysql/mysql.conf.d/mysqld.conf
Configuration file content:
[mysqld] log_bin = /var/log/mysql/mysql-bin.log server-id = 2 read_only = 1 tmpdir = /tmp binlog_format = ROW max_binlog_size = 800M sync_binlog = 1 expire-logs-days = 5 slow_query_log = 2
Where,
Also, comment out the bind-address line.
# bind-address = 127.0.0.1
Now restart MySQL service
$ systemctl restart mysql
Before configuring the slave server, go to the master server and check the status.
$ mysql -u root -p
mysql> SHOW MASTER STATUS\G
Take a note of the following flags which we will use in the slave server.
Now configure the parameters in the slave server which will be used to connect the master server. Follow the below steps.
$ mysql -u root -p
mysql> STOP SLAVE;
mysql> CHANGE MASTER TO MASTER_HOST='192.168.178.137', MASTER_USER='replication_user', MASTER_PASSWORD='StrongP@ssw0rd', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=1050;
Where,
Now activate the slave server.
mysql> START SLAVE;
Verify the slave status by using the following query
mysql> SHOW SLAVE STATUS\G
Note the status of the following flag.
Master and slave are configured to replicate. We can verify if master-slave replication is working or not. In the master server, log in to the MySQL shell and create a test database.
$ mysql -u root -p
mysql> CREATE DATABASE example_db;
Verify the created database.
mysql> SHOW DATABASES;
Now login to the slave server
$ mysql -u root -p
Run the following query to list the databases.
mysql> SHOW DATABASES;
You can see the database created in the master server is replicated to the slave server.
In this article, we learned how to set up MySQL master and slave node on Ubuntu 20.04. You can follow this article to set up master-slave MySQL replication on Ubunutu 18.04 too.
Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…
ISPConfig is an open-source control panel that allows users to manage multiple servers from a…
As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…
Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…
Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…
phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…