DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Setup MariaDB Master and Slave Replication on Ubuntu 16.04

How to Setup MariaDB Master and Slave Replication on Ubuntu 16.04

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

Hitesh Jethva user avatar by
Hitesh Jethva
·
Mar. 07, 19 · Tutorial
Like (2)
Save
Tweet
Share
11.29K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

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.

Two Ways to Replicate Data

Master-Master Replication: In this mode, data to be copied from either server. In other words, it can perform reads or writes from either server. So, whenever one server gets the write request, it will sync data to another 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) instance with Ubuntu 16.04. 

Requirements

  • Two fresh Alibaba Cloud instances with Ubuntu 16.04 installed.

  • A static IP address 192.168.0.101 is configured on the Master node and 192.168.0.102 is configured on the Slave node.

  • A Root password is set up on both instances

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.

apt-get update -y

Install MariaDB

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

apt-get install mariadb-server -y

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

systemctl start mysql
systemctl enable mysql

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

mysql_secure_installation

Answer all the questions shown below:

Set root password? [Y/n] n
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y

Configure Master Node

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

nano /etc/mysql/my.cnf

Make the following changes:

[mysqld]
bind-address = 192.168.0.101
server_id=1
log-basename=master
log-bin=/var/log/mysql/mariadb-bin
binlog-format=row
binlog-do-db=masterdb

Save and close the file when you are finished. Next, restart the MariaDB service to apply the changes:

systemctl restart mysql

Next, log in to MariaDB shell and configure the replication:

mysql -u root -p

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

MariaDB [(none)]> STOP SLAVE;
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'password';

Next, flush the privileges:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;

Next, check the master server status:

MariaDB [(none)]> SHOW MASTER STATUS;

Output:

+--------------------+----------+--------------+------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000001 |      615 | masterdb     |                  |
+--------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

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

Next, exit from the MariaDB shell:

MariaDB [(none)]> exit;

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:

mysqldump --all-databases --user=root --password --master-data > alldatabase.sql

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

scp alldatabase.sql root@192.168.0.102:/root/

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

mysql -u root -p

MariaDB [(none)]> UNLOCK TABLES; 
MariaDB [(none)]> exit;

Configure Slave Server

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

nano /etc/mysql/my.cnf

Make the following changes:

[mysqld]
bind-address = 192.168.0.102
server-id = 2
replicate-do-db=masterdb

Save the file, then restart MariaDB service:

systemctl restart mysql

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

mysql -u root -p < alldatabase.sql

Next, log in to MariaDB shell:

mysql -u root -p

Enter your root password, then stop the slave:

MariaDB [(none)]> STOP SLAVE;

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

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.0.101', MASTER_USER='slave_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=615;

Next, start the slave and check slave status:

MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G;

Output:

*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 172.20.10.6
                  Master_User: slave_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 615
               Relay_Log_File: mysqld-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
              Replicate_Do_DB: masterdb

Test Replication

Both the MariaDB master and slave servers are now configured. It's time to test replication.

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

mysql -u root -p

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

MariaDB [(none)]> create database masterdb;

Next, add some tables and entries on it:

MariaDB [(none)]> use masterdb;
MariaDB [masterdb]> create table mastertable (c int);
MariaDB [masterdb]> insert into mastertable (c) values (1);

Now, check the mastertable:

MariaDB [masterdb]> select * from mastertable;

Output:

+------+
| c    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

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

First, log in to MariaDB shell:

mysql -u root -p

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

MariaDB [(none)]> use masterdb;

Next, check the mastertable:

MariaDB [masterdb]> select * from mastertable;

Output:

+------+
| c    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

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

Let us know your thoughts and questions in the comments.

MariaDB master Replication (computing) Database ubuntu Relational database Alibaba Cloud operating system Open source Cloud computing

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 7 Most Sought-After Front-End Frameworks for Web Developers
  • Using GPT-3 in Our Applications
  • Build an Automated Testing Pipeline With GitLab CI/CD and Selenium Grid
  • 3 Main Pillars in ReactJS

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: