Thursday, June 15, 2017

How to Setup MySQL 5.7 Master/Master Replication Ubuntu 16.04 LTS

MySQL Replication: - Master / Master Replication

Master / Master MySQL Replication used to achieve high-availability (HA).
1- Applications can read from both masters
2- Distributes write load across both master nodes.
3- Simple, automatic and quick failover.

Requirement: -

 For MySQL Master - Master replication required two servers, it could be VPS, VMs and hosted server at any Cloud.

1- Server A (10.0.1.168) - Master-1
2- Server A (10.0.0.116) - Master -2

Step 1. Installing MySQL Server: -

Use the following command to install MySQL on both the servers:

Or you can follow this link to install MySQL: Install MySQL

root@ip-10-0-1-168:~# apt install mysql-server

Once the installation has done on Server A and Server B, Let's make the changes in my.cnf file as follow:

Step 2. Create database

Let's create  a database that we will replicate:
Server A 

mysql> create database db11;
Query OK, 1 row affected (0.00 sec)

Check created database using command below

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db11               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

Step 3. Configuration changes:

Server A 
root@ip-10-0-1-168:~# vi /etc/mysql/my.cnf
Append the below config in the end of my.cnf file
[mysqld]
bind-address = 0.0.0.0
server-id = 1
log-bin="mysql-bin"
binlog-do-db=db11
binlog-ignore-db=information_schema
replicate-ignore-db=test
replicate-ignore-db=information_schema
relay-log="mysql-relay-log"
auto-increment-increment = 2
auto-increment-offset = 1

 Save and Exit from the file
Restart Mysql service to apply changes
root@ip-10-0-1-168:~# systemctl restart mysql.service
Server B
Append the below config in the end of my.cnf file
root@ip-10-0-0-116:~# vi /etc/mysql/my.cnf

[mysqld]
bind-address = 0.0.0.0
server-id = 2
log-bin="mysql-bin"
binlog-do-db=db11
binlog-ignore-db=test
binlog-ignore-db=information_schema
replicate-ignore-db=information_schema
relay-log="mysql-relay-log"
auto-increment-increment = 2
auto-increment-offset = 2



 Save and Exit from the file
Restart Mysql service to apply changes

root@ip-10-0-0-116:~# systemctl restart mysql.service

Step 4. Create the replicator user

you need to do is to create the replicator user in either Server A and Server B. You can do that using MySql shell using the following commands:
Server A 
mysql> CREATE USER 'replicator'@'%' IDENTIFIED BY '[password]';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%' IDENTIFIED BY '[password]';
Query OK, 0 rows affected, 1 warning (0.00 sec)

Server B
mysql> CREATE USER 'replicator'@'%' IDENTIFIED BY '[password]';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%' IDENTIFIED BY '[password]';
Query OK, 0 rows affected, 1 warning (0.00 sec)

Step 5. Backup database db11 from Server A and Restore over Server B:

Server A Take the backup of database using command below
root@ip-10-0-1-168:~# mysqldump -u root -p db11>db11.sql

Server B

First, create database with same Name (db11)
mysql> create database db11;
Query OK, 1 row affected (0.01 sec)
mysql>

Let's restore backup to this Database
root@ip-10-0-0-116:~# mysqldump -u root -p db11<db11.sql
Enter password:  *****
-- MySQL dump 10.13  Distrib 5.7.18, for Linux (x86_64)
--
-- Host: localhost    Database: db11
-- ------------------------------------------------------
-- Server version       5.7.18-0ubuntu0.16.04.1-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2017-06-15 10:19:10
root@ip-10-0-0-116:~#

Step 6. Configure Replication Server A to Server B




Server A- Side Configuration
we need to configure Server B as a slave of Server A. In order to do so, connect to Server A  console and type the following sql command:
root@ip-10-0-1-168:~# mysql -u root -p

mysql> show master status;
+------------------+----------+--------------+-------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB        | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------+-------------------+
| mysql-bin.000001 |      704 |              | test,information_schema |                   |
+------------------+----------+--------------+-------------------------+-------------------+
1 row in set (0.00 sec)

You will receive output as above and we need File and position section to enable replication from Server A to Server B.
Server B- Side configuration
mysql> stop slave;


CHANGE MASTER TO MASTER_HOST = '10.0.1.168', MASTER_USER = 'replicator', MASTER_PASSWORD = '[password]', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 704; START SLAVE;

Step 7. Configure Replication Server B to Server A.

 Server B- Side configuration
root@ip-10-0-0-116:~# mysql -u root -p

mysql> show master status;
+------------------+----------+--------------+-------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB        | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------+-------------------+
| mysql-bin.000001 |      704 |              | test,information_schema |                   |
+------------------+----------+--------------+-------------------------+-------------------+
1 row in set (0.00 sec)

Server A- Side Configuration
mysql> stop slave;

CHANGE MASTER TO MASTER_HOST = '10.0.0.116', MASTER_USER = 'replicator', MASTER_PASSWORD = '[password]', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 704; START SLAVE;



Step 8. Verify Replication Server A to Server B and vice versa.

 Server A -  Check Slave status on Server A by command below
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.116
                  Master_User: replicator
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000010
          Read_Master_Log_Pos: 318
               Relay_Log_File: mysql-relay-log.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000010
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB: test,information_schema
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 318
              Relay_Log_Space: 527
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 2
                  Master_UUID: db835c01-50dd-11e7-88bb-12946d4e87cc
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)
Replication has been configured successfully

 Server B -  Check Slave status on Server B by command below

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.1.168
                  Master_User: replicator
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000014
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-log.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000014
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB: information_schema
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 527
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: 7b0a64f2-4dbb-11e7-8349-023b8b30f372
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)


Replication has been configured successfully on both the servers;
Now Create a Table on Server A by command below:
First Select database db11
Server A:
mysql> use db11
Database changed

Let's create a table using the command below:
mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
Query OK, 0 rows affected (0.03 sec)
Check created table by hitting below command

mysql> show tables;
+----------------+
| Tables_in_db11 |
+----------------+
| pet            |
+----------------+
1 row in set (0.00 sec)

Let's check this table on Server B:
Server B: Change database db11 using command below
mysql> use db11
Database changed
Use command below to show tables
mysql> show tables;
+----------------+
| Tables_in_db11 |
+----------------+
| pet            |
+----------------+
1 row in set (0.00 sec)


Table created over Server A has been successfully replicated to Server B

Now create a Table on Server B and check it over Server A
Server B:


mysql> CREATE TABLE pet11 (name VARCHAR(20), owner VARCHAR(20),species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
Query OK, 0 rows affected (0.02 sec)

mysql>

Let's check it over Server A


Server A
-
mysql> show tables;
+----------------+
| Tables_in_db11 |
+----------------+
| pet            |
| pet11          |
+----------------+
2 rows in set (0.00 sec)


!!! Master-Master replication has been successfully configured and verified.!!!!



No comments:

Post a Comment