Skip to content
Follow
Ubuntu Linux

How to Setup MariaDB Master Slave Replication on Ubuntu

Richard
Written by
Richard
Apr 1, 2019 Updated Jul 15, 2026 4 min read
Enable Auto Login on Ubuntu Linux: Step-by-Step Guide
Enable Auto Login on Ubuntu Linux: Step-by-Step Guide

MariaDB master-slave replication on Ubuntu copies data between two MariaDB servers in real-time, acting as a live backup and boosting speed.

One server, called the master, records all changes. Other servers, called slaves, automatically copy these changes from the master’s logs to stay updated.

This process typically involves setting up your main MariaDB instance as the master on Ubuntu, often version 20.04 LTS or newer. You then configure other MariaDB instances as slaves that constantly receive and apply changes from the master’s logs.

⚡ Quick Answer

Setup MariaDB master-slave replication by configuring the master server’s `/etc/mysql/mariadb.conf.d/50-server.cnf` to enable binary logging and setting a `server_id`. Then, configure the slave server’s `50-server.cnf` with its own `server_id` and restart both services.

Server Preparation

You need two servers for this. One will be the master and the other will be the slave.

💻Code
Primary = 192.168.1.1
Slave = 192.168.1.2

Install MariaDB

To set up MariaDB master slave replication, you first need to install MariaDB on both your main (master) and secondary (slave) servers. This involves running a few commands in your Ubuntu terminal to get the necessary server and client packages installed and then making sure the service is running correctly.

🐧Bash / Shell
sudo apt update
sudo apt-get install mariadb-server mariadb-client

After the installation, use these commands to make sure the service is running correctly.

For Ubuntu 16.04:

🐧Bash / Shell
sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service

For Ubuntu 18.04:

🐧Bash / Shell
sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

Next, secure your database by running this command. Note: This step requires admin privileges.

🐧Bash / Shell
sudo mysql_secure_installation

Follow the prompts to set a root password and remove test settings. To check if it works, log in with this command:

🐧Bash / Shell
sudo mysql -u root -p
mariadb welcome
mariadb ubuntu 1604

Configure the Master Server

Configuring the master server is crucial for MariaDB master slave replication because it holds the primary data. You'll need to edit a specific configuration file using a text editor to set it up correctly. This involves making changes to settings like the server ID and enabling binary logging, which is essential for replication.

🐧Bash / Shell
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Update the file with these settings:

💻Code
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.

log-bin
bind-address = 192.168.1.1
server_id = 1
log-basename = master1

Restart the server to apply the changes:

🐧Bash / Shell
sudo systemctl restart mariadb.service
sudo systemctl restart mysql.service

Now, log in to create a user for replication. Note: This step requires admin privileges.

🐧Bash / Shell
sudo mysql -u root -p

Create the user account:

💻Code
CREATE USER 'replication_user'@'192.168.1.2' IDENTIFIED BY 'new_password_here';

Grant permission to the user:

💻Code
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'192.168.1.2';

View the master details:

💻Code
SHOW MASTER STATUS;

You will see output similar to this:

💻Code
MariaDB [(none)]> SHOW MASTER STATUS;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| master1-bin.000001 | 315 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]>
⚠️Warning
Make sure to write down the File and Position numbers. You will need them for the next part.

Setup the Slave Server

Setting up the slave server is the next step in MariaDB master slave replication, and it involves making it aware of the master. You'll access its configuration file and make specific changes to connect it to the master server. This includes defining its own unique server ID and telling it where to find the master's data.

🐧Bash / Shell
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Update the file with these settings:

💻Code
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.

log-bin
bind-address = 192.168.1.2
server_id = 2

Restart the slave server:

🐧Bash / Shell
sudo systemctl restart mariadb.service
sudo systemctl restart mysql.service

Log in to the slave server:

🐧Bash / Shell
sudo mysql -u root -p

Stop the slave process:

💻Code
STOP SLAVE;

Link the slave to the master using the details you wrote down earlier:

💻Code
CHANGE MASTER TO
MASTER_HOST='192.168.1.1',
MASTER_USER='replication_user',
MASTER_PASSWORD='type_replication_user_password',
MASTER_LOG_FILE='master1-bin.000001',
MASTER_LOG_POS=315;

Start the slave process:

💻Code
START SLAVE;

Test the connection with these commands:

💻Code
SHOW SLAVE STATUS G

You should see confirmation like this:

💻Code
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

Summary

You've now set up MariaDB master slave replication, linking a slave server to your master so data copies over automatically.

[Y/n]

[Y/n]

[Y/n]

[Y/n]

[Y/n]

[mysqld]

[mysqld]









How do I set up master slave replication in MariaDB?

Go to the Replication tab click configure for Slave replication. Configure our master server (enter the name, the password and the host of our slave replication user). Now our master server is configured. Click on Control slave - Full start for the slave server in order to run Slave SQL and Slave IO threads.

Does MariaDB support replication?

MariaDB replication allows you to create redundant copies of your database, known as replicas, on other servers. This not only ensures data durability but also enables you to distribute the workload and perform analytics without impacting the performance of your primary database.

Was this guide helpful?

Was this helpful?
Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

📚 Related Tutorials

Install MySQL and MariaDB on Ubuntu: A Step-by-Step Guide
Ubuntu Linux Install MySQL and MariaDB on Ubuntu: A Step-by-Step Guide
Microsoft Edge Privacy Settings Explained: Protecting Your Data
Browsers Microsoft Edge Privacy Settings Explained: Protecting Your Data
How to Allow Remote Access to MariaDB in Ubuntu Linux
Ubuntu Linux How to Allow Remote Access to MariaDB in Ubuntu Linux
How to Backup and Restore MariaDB on Ubuntu
Ubuntu Linux How to Backup and Restore MariaDB on Ubuntu

No comments yet — be the first to share your thoughts!

Leave a Comment

Your email address will not be published. Required fields are marked *