Configure MySQL Multi-Master Replication

Configure MySQL Multi-Master Replication

Configure MySQL Multi-Master Replication

In this scenario we are configuring mysql master-master replication. The idea behing multimaster is to load-balance MySQL. This configuration will allow read/write on both database servers, as well as replciation.

first things first, install MySQL on your CentOS MySQL Servers

yum -y install mysql-server mysql-client on mysqlnode1

once installed, the next step is to set mysql to run on startup, and start the service now

service mysqld start on mysqlnode1 (shown where the run-at-startup step is described)
service mysqld start at the root prompt on mysqlnode1

perform the safe MySQL installation on each node

/usr/bin/mysql_secure_installation on mysqlnode1

modify node 1 /etc/my.cnf file, to allow remote connections to the MySQL Server, as well as binary logging, and a server id

Node 1 my.cnf, server-id = 1, log-bin=/var/lib/mysql/mysql-bin.log, bind-address=ip

as always, restart the MySQL service after a configuration change

service mysqld restart, Stopping mysqld OK, Starting mysqld OK

login to mysql on the master1 server

mysql -u root -p on mysqlnode1

now create a replication user account, and allow it for all IPs (trust me it’s easier to allow to all IPs)

create user 'replication'@'%' identified by 'password'; at the mysql> prompt

now give replication priviliges to the replication user

grant replication slave on *.* to 'replication'@'%'; on node 1

now do a “SHOW MASTER STATUS”

SHOW MASTER STATUS on node 1, file mysql-bin.000002, position 345

now setup server 2, start by editing the my.cnf

Node 2 my.cnf, server-id=2, log-bin=/var/lib/mysql/mysql-bin.log, bind-address=ip

login to MySQL on node2

mysql -u root -p on mysqlnode2

same as node1, we are going to create a replication user

create user 'replication'@'%' on node 2: Query OK, 0 rows affected

now create a database on node2

CREATE DATABASE CustomerDatabase on node2 returning Query OK
now grant replication permission

grant replication slave on *.* to 'replication'@'%'; on node 2

From here we go through the steps to allow replication to start occuring, start by stopping the slave on mysql2

slave stop; on mysqlnode2: Query OK, 1 warning

now change the master

CHANGE MASTER TO MASTER_HOST '1.2.3.4', user replication, log file mysql-bin.000002, position 345

start the slave again once completed

slave start; on node 2: Query OK, 0 rows affected

on node 2, we just need to make note of the master status

SHOW MASTER STATUS on node 2, file mysql-bin.000002, position 452

on master1, stop the slave service

slave stop; on master1: Query OK, 1 warning

now change the master on slave1

CHANGE MASTER TO MASTER_HOST '1.2.3.5' on master1, log file mysql-bin.000002, position 452

start the slave service

slave start; on master1: Query OK

now the last test is to test the replication.

create a new database on node 1

CREATE DATABASE test_database_2014 on node 1, then SHOW DATABASES lists it

now go to node2, and show all databases

SHOW DATABASES on node 2 lists the replicated test_database_2014

great, it replicated from node1 to node2, now let’s delete/drop the database from node2 and make sure it’s gone from node1

DROP DATABASE test_database_2014 on node 2: Query OK

ok, now on node 1, do SHOW DATABASES; it should have disappeared

SHOW DATABASES on node 1 shows only information_schema and mysql: the drop replicated back

perfect!

Troubleshooting:

SHOW SLAVE STATUSG; will show you if replication is working

Fixing broken replciation:

  1. stop slave
  2. re-verify master postition and adjust on slave
  3. start slave

Some applications are not multi-master aware such as Cacti and Drupal, to work around this, enter the following into /etc/my.cnf on each Master Server, plus any slaves, if you have them.

# Skip Slave Duplicate Data Error
slave-skip-errors=1062

That’s it, you have configured MySQL multi-master replication on CentOS

Hosted Linux Servers at www.zwiegnet.com/go

Related guides: MySQL Replication CentOS All Databases | Convert MySQL Slave to Master

Leave a Reply

Your email address will not be published. Required fields are marked *