Upload
stephenefange
View
2
Download
0
Embed Size (px)
DESCRIPTION
mmm
Citation preview
MySQL Master-Master ReplicationWhats MySQL ReplicationReplication enables data from one MySQL database server (the master) to be replicated to one or more MySQL database servers (the slaves).So MySQL Master-Master ReplicationIn other words MySQL replication is the process by which data stored in a database, will be copied to a second servers database. This is called MySQL master-slave replication. Today Im going to focus on MySQL master-master replication where it allows data to be copied from either server. In other words perform reads or writes from either server.
Image Courtesy : alexfilatov.comBelow are the two server we are going to use, Server X: 192.168.X.28 Server Y: 192.168.Y.29Step 1 Install and Configure MySQL on Server XMySQL
1sudo apt-get install mysql-server mysql-client
We need to edit /etc/mysql/my.cnf on Server X and add below lines.
123456server-id = 1log_bin = /var/log/mysql/mysql-bin.logbinlog_do_db = sampleauto-increment-increment = 2auto-increment-offset = 2#bind-address = 127.0.0.1
After you save and quit that file, you need to restart mysql:Shell
1sudo service mysql restart
Open up the MySQL shell.Shell
1mysql -u root -p
We have to create a user where the user is used for the replication. User replicator_user & replace password with the password you wish.MySQL
1create user 'replicator_user'@'%' identified by 'password';
Next, we need to give this user permissions to replicate the data:MySQL
1grant replication slave on *.* to 'replicator_user'@'%' identified by 'password'; flush privileges;
The following command will output important information which will be used later.MySQL
1show master status;
123456+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000001 | 1005 | sample ||+------------------+----------+--------------+------------------+1 row in set (0.00 sec)
lock the database to prevent any new changesMySQL
1flush tables with read lock;
Lets export the database using mysqldumpShell
1mysqldump -u root -p sample > sample.sql
unlock the databases (making itwriteable again).MySQL
1unlock tables;
Finish up by exiting the shell.Step 2 Install and Configure MySQL on Server YWe need to repeat the same steps that we followed on Server X.Shell
1sudo apt-get install mysql-server mysql-client
Lets start by editing the /etc/mysql/my.cnf file.
123456server-id = 2log_bin = /var/log/mysql/mysql-bin.logbinlog_do_db = sampleauto-increment-increment = 2auto-increment-offset = 1#bind-address = 127.0.0.1
After you save and quit thefile, you need to restart MySQL:Shell
1sudo service mysql restart
Its time to go into the MySQL shell.Shell
1mysql -u root -p
Next, we need to create the database that we are going to replicate.MySQL
1create database sample;
then exitMySQL
1exit;
Import the database that you previously exported.Shell
1mysql -u root -p sample < /path/to/sample.sql
Lets login the back to the shell. Just as on Server X, A user will be created which will be responsible for the replication.MySQL
1create user 'replicator_user'@'%' identified by 'password';
permissions for replicationMySQL
1grant replication slave on *.* to 'replicator_user'@'%' identified by 'password'; flush privileges;
This will allow replication to begin. The following should be typed at the mysql shellMySQL
123slave stop;CHANGE MASTER TO MASTER_HOST = '192.168.X.28', MASTER_USER = 'replicator_user', MASTER_PASSWORD = 'password', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 1005;slave start;
Now lets make a note of the master log file and position to use to replicate in the other direction (from Server Y to Server X).MySQL
1show master status;
MySQL
123456+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000001 | 433| sample ||+------------------+----------+--------------+------------------+1 row in set (0.00 sec)
Step 3 Completing Replication on Server XRunning this command will replicate all data from Server Y.MySQL
123slave stop;CHANGE MASTER TO MASTER_HOST = '192.168.Y.29', MASTER_USER = 'replicator_user', MASTER_PASSWORD = 'password', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 433;slave start;
You be able to see the details of the replication by typing in this command. The \G rearranges the text to make it more readable.MySQL
1show slave status\G;
As a verification that everything are working as expected examine the output of the above command and make surethat Slave_IO_Runningand Slave_SQL_Runningare both YES.Tips & TricksAbove mentioned configuration is the minimal configuration for a MySQL master-master replication. See the below for a full my.ini configurations.MySQL
12345678910server_id = 1log_bin = /var/log/mysql/mysql-bin.loglog_bin_index = /var/log/mysql/mysql-bin.log.indexrelay_log = /var/log/mysql/mysql-relay-binrelay_log_index = /var/log/mysql/mysql-relay-bin.indexexpire_logs_days = 10max_binlog_size = 100Mlog_slave_updates = 1auto-increment-increment = 2auto-increment-offset = 2
MySQL
12345678910server_id = 2log_bin = /var/log/mysql/mysql-bin.loglog_bin_index = /var/log/mysql/mysql-bin.log.indexrelay_log = /var/log/mysql/mysql-relay-binrelay_log_index = /var/log/mysql/mysql-relay-bin.indexexpire_logs_days = 10max_binlog_size = 100Mlog_slave_updates = 1auto-increment-increment = 2auto-increment-offset = 1
Hope you got an idea how to do a MySQL master-master replication. If you have any questions on MySQL master-master replication let me know in the comments below. Your feedback is highly appreciated(happy-face).