sudo apt-get install mysql-server mysql-clientBy default, the mysql process will only accept connections on localhost (127.0.0.1). To change this default behavior and change a few other settings necessary for replication to work properly, we need to edit /etc/mysql/my.cnf on Server C.
#server-id = 1 #log_bin = /var/log/mysql/mysql-bin.log #binlog_do_db = include_database_name bind-address = 127.0.0.1The first of those lines is to uniquely identify our particular server, in our replication configuration. We need to uncomment that line, by removing the "#" before it. The second line indicates the file in which changes to any mysql database or table will be logged. The third line indicates which databases we want to replicate between our servers. You can add as many databases to this line as you'd like. The article will use a single database named "example" for the purposes of simplicity. And the last line tells our server to accept connections from the internet (by not listening on 127.0.0.1).
server-id = 1 log_bin = /var/log/mysql/mysql-bin.log binlog_do_db = example # bind-address = 127.0.0.1Now we need to restart mysql:
sudo service mysql restartWe next need to change some command-line settings within our mysql instance. Back at our shell, we can get to our root mysql user by typing the following:
mysql -u root -pPlease note that the password this command will prompt you for is that of the root mysql user, not the root user on our control panel.
mysql>Once we are logged in, we need to run a few commands.
create user 'replicator'@'%' identified by 'password';Next, we need to give this user permissions to replicate our mysql data:
grant replication slave on *.* to 'replicator'@'%';Permissions for replication cannot, unfortunately, 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;The output will looking similiar to the following, and will have two pieces of critical information:
+------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 107 | example | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)We need to make a note of the file and position which will be used in the next step.
sudo apt-get install mysql-server mysql-clientOnce the two packages are properly installed, we need to configure it in much the same way as we configured Server C. We will start by editing the /etc/mysql/my.cnf file.
sudo nano /etc/mysql/my.cnfWe need to change the same four lines in the configuration file as we changed earlier.
#server-id = 1 #log_bin = /var/log/mysql/mysql-bin.log #binlog_do_db = include_database_name bind-address = 127.0.0.1We need to change these four lines to match the lines below. Please note, that unlike Server C, the server-id for Server D cannot be set to 1.
server-id = 2 log_bin = /var/log/mysql/mysql-bin.log binlog_do_db = example # bind-address = 127.0.0.1After you save and quit that file, you need to restart mysql:
sudo service mysql restartIt is time to go into the mysql shell and set some more configuration options.
mysql -u root -pFirst, just as on Server C, we are going to create the pseudo-user which will be responsible for the replication. Replace "password" with the password you wish to use.
create user 'replicator'@'%' identified by 'password';Next, we need to create the database that we are going to replicate across our server.
create database example;And we need to give our newly created 'replication' user permissions to replicate it.
grant replication slave on *.* to 'replicator'@'%';The next step involves taking the 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 = '3.3.3.3', MASTER_USER = 'replicator', MASTER_PASSWORD = 'password', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 107; slave start;You need to replace 'password' with the password that you have chosen for replication. Your values for MASTER_LOG_FILE and MASTER_LOG_POS may differ than those above. You should copy the values that "SHOW MASTER STATUS" returns on Server C.
SHOW MASTER STATUS;The output will look similiar to the following:
+------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 107 | example | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)Take note of the file and position, as we will have to enter those on server C, to complete the two-way replication. The next step will explain how to do that.
slave stop; CHANGE MASTER TO MASTER_HOST = '4.4.4.4', MASTER_USER = 'replicator', MASTER_PASSWORD = 'password', MASTER_LOG_FILE = 'mysql-bin.000004', MASTER_LOG_POS = 107; slave start;Keep in mind that your values may differ from those above. Please also replace the value of MASTER_PASSWORD with the password you created when setting up the replication user.
Query OK, 0 rows affected (0.01 sec)The last thing to do is to test that replication is working on both server. The last step will explain an easy way to test this configuration.
create database example;Once that's done, let's create a dummy table on Server C:
create table example.dummy (`id` varchar(10));We now are going to check Server D to see if our table exists.
show tables in example;We should see output similiar to the following:
+-------------------+ | Tables_in_example | +-------------------+ | dummy | +-------------------+ 1 row in set (0.00 sec)The last test to do is to delete our dummy table from Server D. It should also be deleted from Server C. We can do this by entering the following on Server D:
DROP TABLE dummy;To confirm this, running the "show tables" command on Server C will show no tables:
Empty set (0.00 sec)And there you have it! Working mysql master-master replication.
Article ID: 183
Created On: Thu, Dec 26, 2013 at 10:11 PM
Last Updated On: Sun, Jan 5, 2014 at 8:43 PM
Authored by: ASPHostServer Administrator [asphostserver@gmail.com]
Online URL: http://faq.asphosthelpdesk.com/article.php?id=183