Follow
Ubuntu Linux

How to Setup MariaDB Master Slave Replication on Ubuntu

Richard
Written by
Richard
Apr 1, 2019 Updated Apr 18, 2026 5 min read
How to Setup MariaDB Master Slave Replication on Ubuntu

MariaDB master-slave replication on Ubuntu synchronizes data between two MariaDB database servers, designating one as the master and others as slaves that automatically mirror its changes.

This setup provides critical data redundancy, acting as a real-time backup. It also enhances performance by allowing read operations to be distributed across multiple slave servers.

You configure this by setting up a primary MariaDB instance (the master) and then configuring one or more secondary instances (slaves) to continuously receive and apply binary log events from the master. This ensures your data is safe and your application scales effectively.

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

You must install MariaDB on both servers. Note: This step requires admin privileges.

🐧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

Configure the Master Server

The master server is the source of truth. Open the configuration file to make changes. Note: This step requires admin privileges.

🐧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)]>

Make sure to write down the File and Position numbers. You will need them for the next part.

Setup the Slave Server

Log in to your slave server and open its configuration file. Note: This step requires admin privileges.

🐧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 have successfully configured MariaDB replication. By following these steps, you have linked a slave server to a master server. Any data written to the master will now be automatically copied to the slave, ensuring your data is protected against failure. For further reading, check out %📂%LNK0%% and %📂%LNK1%%.

[Y/n]

[Y/n]

[Y/n]

[Y/n]

[Y/n]

[mysqld]

[mysqld]

What is MariaDB master and slave replication?

MariaDB master and slave replication is a database setup where one server (the master) handles all write operations, while one or more servers (the slaves) replicate the data from the master. This configuration enhances data availability, fault tolerance, and allows for load balancing.


How do I install MariaDB on Ubuntu?

To install MariaDB on Ubuntu, you can use the command 'sudo apt update' followed by 'sudo apt-get install mariadb-server mariadb-client'. This will install both the server and client packages needed for MariaDB.


What are the steps to secure my MariaDB installation?

To secure your MariaDB installation, run 'sudo mysql_secure_installation' and follow the prompts to set a root password, remove anonymous users, and disallow remote root login. This process helps protect your database from unauthorized access.


How can I check if MariaDB is running on my server?

You can check if MariaDB is running by executing 'sudo systemctl status mariadb.service' on Ubuntu 18.04 or 'sudo systemctl status mysql.service' on Ubuntu 16.04. This command will show you the current status of the MariaDB service.


What is the purpose of the server ID in MariaDB replication?

The server ID in MariaDB replication is a unique identifier for each server in the replication setup. It is essential for distinguishing between the master and slave servers, ensuring that data is correctly replicated without conflicts.

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

How to Install BoxBilling with Apache on Ubuntu Linux
Ubuntu Linux How to Install BoxBilling with Apache on Ubuntu Linux
Install UNetbootin on Ubuntu 24.04: A Step-by-Step Guide
Ubuntu Linux Install UNetbootin on Ubuntu 24.04: A Step-by-Step Guide
How to Uninstall Windows Subsystem for Linux (WSL) in Windows 11
Ubuntu Linux How to Uninstall Windows Subsystem for Linux (WSL) in Windows 11
How to Install MongoDB with PHP on Ubuntu 24.04
Ubuntu Linux How to Install MongoDB with PHP on Ubuntu 24.04

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

Leave a Comment

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