Sunday, September 6, 2020

HOW TO SETUP MYSQL MASTER MASTER REPLICATION WITH GTID.

 #######################

HOW TO SETUP MYSQL MASTER MASTER REPLICATION WITH GTID.

A global transaction identifier (GTID) is a unique identifier created and associated with each transaction committed on the server of origin (master). 

This identifier is unique not only to the server on which it originated, but is unique across all servers in a given replication setup. 

There is a 1-to-1 mapping between all transactions and all GTIDs. 

A GTID is represented as a pair of coordinates, separated by a colon character (:), as shown here:


GTID = source_id:transaction_idMySQL_gtid-master-master


Advantages :

● Setting up MySQL replication is simple

● Consistency is guaranteed between master and slaves.

● Simple to determine inconsistency

● Fail-over process become much easier.


step.1. Get Both server  serverA and serverB ip.


serverA ip=xx.xx.xx.xx

serverB ip=xx.xx.xx.xx



Step 2 : Configure the Master serverA


To enable GTID-based replication, each server must be started with GTID mode enabled by setting the gtid_mode variable to ON, 

and with the enforce_gtid_consistency variable enabled to ensure that only statements which are safe for GTID-based replication are logged.


 vi /etc/my.cnf

Add the following parameter under [mysqld] section of my.cnf file


server-id = 1

log-bin = mysql-bin

binlog_format = row

gtid-mode=ON

enforce-gtid-consistency

log-slave-updates


Now start the mysqld server process


sudo service mysqld start


Step 3 : Create a replication user for Master serverB


create user 'repl_user'@'%' identified by 'XXXXXXXXXX';

Grant replication slave on *.* to 'repl_user'@'%';


Step 4 : Take backup with mysqldump with Master serverA


Now we take a backup with mysqldump from the master:


mysqldump --all-databases -flush-privileges --single-transaction --flush-logs --triggers --routines --events --user=root  --password=XXXXXXXX > mysqlbackup_dump.sql


Step 5 : Configure the Master serverB 


 vi /etc/my.cnf

Add the following parameter under [mysqld] section of my.cnf file


server-id = 2

log-bin = mysql-bin

relay-log = relay-log-server

read-only = ON

gtid-mode=ON

enforce-gtid-consistency

log-slave-updates


Now start the mysqld server process


sudo service mysqld start


Step 6: Create a replication user for Master serverA


create user 'repl_user'@'%' identified by 'XXXXXXXXXX';

Grant replication slave on *.* to 'repl_user'@'%';


Step 7 : Load the mysqldump in Master server 2 


mysql>  source mysqlbackup_dump.sql


Step 8 : Execute the Change Master  on Master serverB 


Now we will tell the Master serverB to use Master serverA as a data source


CHANGE MASTER TO

MASTER_HOST = 'xx.xx.xx.xx',

MASTER_PORT = 3306,

MASTER_USER = 'repl_user',

MASTER_PASSWORD = 'XXXXXXXXX',

MASTER_AUTO_POSITION = 1;

Start the slave And then check status.


start slave;

show slave status\G


If every thing fine then move to master serverA to apply change master.


Step 9 : Execute the Change Master  on Master serverA


Now we will tell the Master serverA to use Master serverB as a data source


CHANGE MASTER TO

MASTER_HOST = 'xx.xx.xx.xx',

MASTER_PORT = 3306,

MASTER_USER = 'repl_user',

MASTER_PASSWORD = 'XXXXXXXXX',

MASTER_AUTO_POSITION = 1;

Start the slave And then check status.


start slave;

show slave status\G

Test the Replication


Create a table on both server and check whether the replication is working fine.


Thank you.

No comments:

Post a Comment

HOW TO TO CALCULATE TABLE SIZE IN MB AND CREATE EXCEL SHEET IN MYSQL 5.7 USING CENTOS 7

 HOW TO TO CALCULATE TABLE SIZE IN MB AND CREATE EXCEL SHEET IN MYSQL 5.7 USING CENTOS 7 1.Given command below:- SELECT       table_schema a...