How to Setup MariaDB Master and Slave Replication on Ubuntu 16.04

By Hitesh Jethva, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

MariaDB is a free, open source and one of the most popular open source relational database management system. It is a drop-in replacement for MySQL intended to remain free under the GNU GPL. You will need to increase the instances of your MariaDB server and replicate the data on multiple servers when your traffic grows. The Master-Slave replication provides load balancing for the databases. It is not used for any failover solution.

There are two ways to replicate data:

Master-Master Replication: In this mode, data to be copied from either server. In other words, perform reads or writes from either server. So whenever one server gets the write request it will sync data to other server. This mode will be very useful when you want the best redundancy.

Master-Slave Replication: In this mode, data changes happen on the master server, while the slave server automatically replicates the changes from the master server. This mode will be best suited for data backups.

In this tutorial, we will learn how to set up MariaDB Master-Slave replication on Alibaba Cloud Elastic Compute Service (ECS) with Ubuntu 16.04.


  1. Two fresh Alibaba Cloud instance with Ubuntu 16.04 installed.
  2. A static IP address is configured on the Master node and is configured on the Slave node.
  3. A Root password is set up on both instance.

Launch Alibaba Cloud ECS Instance

First, log in to your Alibaba Cloud ECS Console. Create a new ECS instance, choosing Ubuntu 16.04 as the operating system with at least 2GB RAM. Connect to your ECS instance and log in as the root user.

Once you are logged into your Ubuntu 16.04 instance, run the following command to update your base system with the latest available packages.

Install MariaDB

Before starting, you will need to install MariaDB server on both instance. You can install it by running the following command:

Once the installation is completed, start MariaDB service and enable it to start on boot time with the following command:

By default, MariaDB is not secured. So you will need to secure it first. You can do this by running the following command:

Answer all the questions as shown below:

Configure Master Node

First, you will need to edit /etc/mysql/my.cnf and make some changes inside it.

Make the following changes:

Save and close the file, when you are finished.

Next, restart MariaDB service to apply the chnages:

Next, log in to MariaDB shell and configure replication:

Enter your root password, then stop the slave and create a replication user and set password:

Next, flush the privileges:

Next, check the master server status:


Note: Remember the file mariadb-bin.000001 and position number 615.

Next, exit from the MariaDB shell:

Next, take a backup of all databases on the master server and transfer it to the slave server.

First, take all database backup with the following command:

Next, transfer alldatabase.sql file to the slave server with the following command:

Next, log in to MariaDB console again and unlock the tables:

Configure Slave Server

You will also need to edit /etc/mysql/my.cnf file and make some changes inside it:

Make the following changes:

Save the file, then restart MariaDB service:

Next, import the alldatabase.sql which you have transferred from master server:

Next, log in to MariaDB shell:

Enter your root password, then stop the slave:

Next, configure the slave to use the master with the following command:

Next, start the slave and check slave status:


Test Replication

Both MariaDB master and slave server are now configured. It’s time to test replication.

Navigate to the Master server, and log in to MariaDB shell:

Enter your root password, then create a database masterdb which you have specified in my.cnf file:

Next, add some tables and entries on it:

Now, check the mastertable:


Now, navigate to the slave server and verify whether the above created table has been replicated:

First, log in to MariaDB shell:

Enter your root password, then change the database to masterdb:

Next, check the mastertable:


You should see that the database and table are replicated successfully from the master server to the slave server.

Related Alibaba Cloud Products

Distributed Relational Database Service (DRDS) is a lightweight (stateless), flexible, stable, and efficient middleware product independently developed by Alibaba Group to resolve scalability issues with single-host relational databases DRDS is primarily used for large-scale online data operations to operate, maintain and manage distributed databases throughout their entire lifecycle. Due to its compartmentalized structure, DRDS provides an efficient way to perform operations to meet the demand of the online services for relational databases.

Alibaba Cloud ApsaraDB for RDS is an on-demand database service that frees you up from the administrative task of managing a database, and leaves you with more time to focus on your core business. ApsaraDB for RDS is a ready-to-use service that is offered on MySQL, SQL Server and PostgreSQL. RDS handles routine database tasks such as provisioning, patch up, backup, recovery, failure detection and repair.

ApsaraDB for RDS can also protect against network attacks and intercept SQL injections, brute force attacks and other types of database attacks. As a highly available managed service, ApsaraDB for RDS includes automated monitoring, backup and disaster recovery capabilities. The product includes automated provisioning and scalability, which allows you to scale your databases up and down based on your application’s real-time needs.


Follow me to keep abreast with the latest technology news, industry insights, and developer trends.