• Get In Touch
December 15, 2016

How to Setup MySQL Master Master Replication on Ubuntu 16.04

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month.
Get a Cloud Server

You can setup two types of replication in MySQL. First is the master master replication and other one is master slave replication.

But master slave replication provides only load balancing for the databases, it does not provide failover. It means if your master server goes down, you can not execute queries directly on the slave server. In master master replication, it will allows data to be copied from one server to the other. This will adds redundancy and increases efficiency when accessing database.

In this tutorial, we will learn how to configure mysql master master replication between two Ubuntu 16.04 host.

Requirements

  • Two server running Ubuntu 16.04.
  • Static IP address 192.168.15.237 configured on first master server.
  • Static IP address 192.168.15.100 configured on second master server.
  • A non-root user with sudo privileges configure on both server.

Install and Configure MySQL on First Master Server

First, you will need to install mysql server and client on first server. You can install it with the following command:

sudo apt-get install mysql-server mysql-client

Next, you will need to make some changes in my.cnf file:

sudo nano /etc/mysql/my.cnf

Change the file as shown below:

server_id           = 1
bind-address        = 192.168.15.237
log_bin             = /var/log/mysql/mysql-bin.log
log_bin_index       = /var/log/mysql/mysql-bin.log.index
relay_log           = /var/log/mysql/mysql-relay-bin
relay_log_index     = /var/log/mysql/mysql-relay-bin.index
expire_logs_days    = 10
max_binlog_size     = 100M
log_slave_updates   = 1
auto-increment-increment = 2
auto-increment-offset = 1

Once you are done, restart the mysql service:

sudo /etc/init.d/mysql restart

Next, you will need to secure mysql installation, because default mysql installation is not secure.

You can secure it by running the secure_mysql_installation script:

sudo mysql_secure_installation

Next, you will need to create a replication user.

You can do this with the following command:

First, log in to mysql shell with the following command:

mysql -u root -p

Enter your root password to login mysql shell.

Then, create a replication user with the following command:

mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.15.100' IDENTIFIED BY 'password';

Where 192.168.15.100 is a IP address of the second server.

Install and Configure MySQL on Second Master Server

First, you will need to install mysql server and client on second server. You can install it with the following command:

sudo apt-get install mysql-server mysql-client

Next, you will need to make some changes in my.cnf file:

sudo nano /etc/mysql/my.cnf

Change the file as shown below:

server_id           = 2
bind-address        = 192.168.15.100
log_bin             = /var/log/mysql/mysql-bin.log
log_bin_index       = /var/log/mysql/mysql-bin.log.index
relay_log           = /var/log/mysql/mysql-relay-bin
relay_log_index     = /var/log/mysql/mysql-relay-bin.index
expire_logs_days    = 10
max_binlog_size     = 100M
log_slave_updates   = 1
auto-increment-increment = 2
auto-increment-offset = 2

Once you are done, restart the mysql service:

sudo /etc/init.d/mysql

Next, you will need to secure mysql installation, because default mysql installation is not secure.

You can secure it by running the secure_mysql_installation script:

sudo mysql_secure_installation

Next, you will need to create a replication user.
You can do this with the following command:

First, log in to mysql shell with the following command:

mysql -u root -p

Enter your root password to login mysql shell.

Then, create a replication user with the following command:

mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.15.237' IDENTIFIED BY 'password';

Where 192.168.15.237 is a IP address of the second server.

Once you are done, you can proceed to next step.

Configure MySQL Master on Both Server

Now, you will need to tell each server that other server is master.

Configure Second Server as Master

Before starting, you will need to check master status on the first server.

On the first server, log in to mysql shell and check the master server status with the following command:

mysql -u root -p
mysql> SHOW MASTER STATUS;

You should see the following output:

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      276 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

From above output, you will need to note mysql-bin.000001 and Position 276.
Next, tell second server that first server is it’s master.

On the second server, run the following command:

mysql -u root -p
mysql> SLAVE STOP;
mysql> CHANGE MASTER TO master_host='192.168.15.237', master_port=3306, master_user='replication', master_password='password', master_log_file='mysql-bin.000001', master_log_pos=276;mysql> SLAVE START;

Configure First Server as Master

First, check the master status of the second server and note the file and position values.

mysql> SHOW MASTER STATUS;

You should see the following output:

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      276 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

From the above output, you will need to note mysql-bin.000001 and Position 276.

Next, tell first server that second server is it’s master.

On the first server, run the following command:

mysql> SLAVE STOP;
mysql> CHANGE MASTER TO master_host='192.168.15.100', master_port=3306, master_user='replication', master_password='password', master_log_file='mysql-bin.000001', master_log_pos=276;
mysql> SLAVE START;

Once you are done, you can proceed to next step.

Testing Replication

Once everything is setup properlly, it’s time to check whether database replication is working or not.

To do so, create a database with name testingdb on the first server:

mysql -u root -p
mysql>create database testingdb;

Next, on the second server. Check whether this database is created or not.

mysql -u root -p
mysql>show databases;

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| limesurvey         |
| mysql              |
| performance_schema |
| testingdb          |
+--------------------+

In the above output, you should see that database testingdb is created.

Next, on the second server. Create a table in this database.

mysql> CREATE TABLE testingdb.testuser ( id INT, name VARCHAR(20));

Next, on the first server. Check whether this table is created or not:

mysql> use testingdb;
mysql> show tables;

+---------------------+
| Tables_in_testingdb |
+---------------------+
|                     |
| testuser            |
+---------------------+

In the above output, you should see that table testuser is created in testingdb database.

Conclusion

Congratulations! You now have a working MySQL master master replication. You can run query on any of the two servers, thus providing us with a fault-tolerant and safe environment.

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month.
Get a Cloud Server

Share this Article!

Related Posts

Node.js Authentication – A Complete Guide with Passport and JWT

Node.js Authentication – A Complete Guide with Passport and JWT

Truth be told, it’s difficult for a web application that doesn’t have some kind of identification, even if you don’t see it as a security measure in and of itself. The Internet is a kind of lawless land, and even on free services like Google’s, authentication ensures that abuses will be avoided or at least […]

Node.js and MongoDB: How to Connect MongoDB With Node

Node.js and MongoDB: How to Connect MongoDB With Node

MongoDB is a document-oriented NoSQL database, which was born in 2007 in California as a service to be used within a larger project, but which soon became an independent and open-source product. It stores documents in JSON, a format based on JavaScript and simpler than XML, but still with good expressiveness. It is the dominant […]

Using MySQL with Node.js: A Complete Tutorial

Using MySQL with Node.js: A Complete Tutorial

Although data persistence is almost always a fundamental element of applications, Node.js has no native integration with databases. Everything is delegated to third-party libraries to be included manually, in addition to the standard APIs. Although MongoDB and other non-relational databases are the most common choice with Node because if you need to scale an application, […]

Node.Js Vs Django: Which Is the Best for Your Project

Node.Js Vs Django: Which Is the Best for Your Project

Django and NodeJs are two powerful technologies for web development, both have great functionality, versatile applications, and a great user interface. Both are open source and can be used for free. But which one fits your project best? NodeJs is based on JavaScript, while Django is written in Python. These are two equally popular technologies […]

Nodejs Vs PHP:  Which Works Best?

Nodejs Vs PHP: Which Works Best?

Before getting into the “battle” between Node.js and PHP we need to understand why the issue is still ongoing. It all started with the increased demand for smartphone applications, their success forcing developers to adapt to new back-end technologies that could handle a multitude of simultaneous requests. JavaScript has always been identified as a client-side […]