Find us on facebook

Jun 1, 2015

Set Up MySQL Master-Master Replication

Server A - 54.154.46.139 (Ubuntu)
Server B - 52.17.26.158 (Linux)

Server A Config
sudo apt-get install mysql-server mysql-client

sudo nano /etc/mysql/my.cnf

Change following lines
#server-id              = 1
#log_bin                = /var/log/mysql/mysql-bin.log
#binlog_do_db           = include_database_name
bind-address            = 127.0.0.1

To
server-id               = 1
log_bin                 = /var/log/mysql/mysql-bin.log
binlog_do_db            = example1
# bind-address            = 127.0.0.1

To save Ctrl+O Then press Enter.
To exit use Ctrl+X

sudo service mysql restart

mysql -u root -p

Create a pseudo-user that will be used for replicating data between our two VPS. Assume that you name this user "replicator". And password is abc123
create user 'replicator'@'%' identified by 'abc123';

Give this user permissions to replicate our mysql data:
grant replication slave on *.* to 'replicator'@'%';

Permissions for replication cannot, be given on a per-database basis. Our user will only replicate the database(s) that we instruct it to in our config file.

show master status;


Server B Config

sudo apt-get install mysql-server mysql-client

sudo nano /etc/my.cnf

server-id = 2
binlog-do-db=example1
relay-log = /var/lib/mysql/mysql-relay-bin
relay-log-index = /var/lib/mysql/mysql-relay-bin.index
log-error = /var/lib/mysql/mysql.err
master-info-file = /var/lib/mysql/mysql-master.info
relay-log-info-file = /var/lib/mysql/mysql-relay-log.info
log-bin = /var/lib/mysql/mysql-bin

To save Ctrl+O Then press Enter.
To exit use Ctrl+X

sudo /etc/init.d/mysqld restart

mysql -u root -p

Create a pseudo-user . Assume that you name this user "replicator". And password is abc123
create user 'replicator'@'%' identified by 'abc123';

Give newly created 'replication' user permissions to replicate it.

grant replication slave on *.* to 'replicator'@'%';

Use information that we took a note of earlier and applying it to our mysql instance. This will allow replication to begin. The following should be typed at the mysql shell:

slave stop;
CHANGE MASTER TO MASTER_HOST = '54.154.46.139', MASTER_USER = 'replicator', MASTER_PASSWORD = 'abc123', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 346;
slave start;

Now Get master status of this server
SHOW MASTER STATUS;


Go back to server A

mysql -u root -p

slave stop;
CHANGE MASTER TO MASTER_HOST = '52.17.26.158', MASTER_USER = 'replicator', MASTER_PASSWORD = 'abc123', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 437;
slave start;

That's all

Testing (Both servers should have example1db)

Create table on Server A
create table example.abc(`id` varchar(10));

Go to server B and check
show tables in example1;

DROP TABLE abc;

Go back to Server A and check
No tables will display

No comments:

Post a Comment