Seems like almost all new MySQL dbas stumble a bit trying to set up master-master replication. Here's my (hopefully) fool proof check list to try and make it work on the first try.
Given two mySql instances we'll call them host1.mydomain.com and host2.mydomain.com and we presume they are using the standard port 3306 to communicate, that you have a super mysql user we'll call root with password rootpw (name and password of course could be different), and that you can ssh to the hosts. (Sorry but Windows is not my forte however the commands below should be very similar.)
Commands are in bold.
begin by checking on access on host1.mydomain.com and access from host1.mydomain.com to host 2.mydomain.com
- ssh host1.mydomain.com
- telnet host2.mydomain.com 3306 You should get something like "connected". Then ctrl+v and quit If it hangs then get someone on your network team to help you figure out why you cannot reach the other host. In my experience this is 75% of the issues setting up replication.
- which mysql (is the mysql client binary in your path?) if not fix your path or in any mysql command specify the fully qualified location, something like /usr/bin/mysql
- login to mysql thusly mysql -h host1.mydomain.com -u root -p you will be prompted for the password
- record your server id. show global variables like 'server_id';
- make sure you have binary logging as show binary logs; If it says you are not using binary logging then add log-bin to your /etc/my.cnf file and bounce mysql.
- exit from this instance exit;
- try to reach host2 from host1.mydomain.com using the mysql client as mysql -h host2.mydomain.com -u root -p you will be prompted for the password. If this fails figure out why you cannot login. Is it a user or password issue? Are you sure the ports are open between the two hosts? Is the root user not allowed to do remote logins in which case you may need another super user (select user from mysql.user where user='root'; and there should be a wildcard for host1 and host2 should be in the host name field)
now check on access on host2 and access to host1 from host2
- ssh host2.mydomain.com
- telnet host1.mydomain.com 3306 You should get something like "connected". Then ctrl+v and quit If it hangs then get someone on your network team to help you figure out why you cannot reach the other host. In my experience this is 75% of the issues setting up replication.
- which mysql (is the mysql client binary in your path?) if not fix your path or in any mysql command specify the fully qualified location, something like /usr/bin/mysql
- login to mysql thusly mysql -h host2.mydomain.com -u root -p you will be prompted for the password
- record your server id. show global variables like 'server_id';
- make sure you have binary logging as show binary logs; If it says you are not using binary logging then add log-bin to your /etc/my.cnf file and bounce mysql.
- exit from this instance exit;
- try to reach host1.mydomain.com from host2.mydomain.com using the mysql client as mysql -h host1.mydomain.com -u root -p you will be prompted for the password. If this fails figure out why you cannot login. Is it a user or password issue? Are you sure the ports are open between the two hosts? Is the root user not allowed to do remote logins in which case you may need another super user (select user from mysql.user where user='root'; and there should be a wildcard for host1 and host2 should be in the host name field)
At this point we have proved that host1.mydomain.com
and host2.mydomain.com can communicate to and from each other. And we have the server ids, which must be different between the two hosts. If the server ids are undefined or the same for the two hosts you must update your /etc/my.cnf configuration file to set a valid number and bounce the mysql instance. The entry in the /etc/my.cnf file should look something like:
server_id=900
and the numbers are not really important as long as all hosts participating in a replication setup have a unique number.
Create a replication user on each mysql instance. We use the same user and password on both instances to keep this simple. Do this on both host1 and host2.
grant replication slave on *.* to 'repl'@'%' identified by 'password';
grant replication client on *.* to 'repl'@'%' identified by 'password';
I'm using the user repl and you should pick a nice password. (Purists will argue about allowing access to all schemas from all hosts; you can certainly restrict this but for simplicity I suggest you get this working first then make granularity adjustments.)
As a final check, try logging into each server from the other server using your new repl user and password. You should be able to connect.
Now let's define the first master and slave. On host1.mydomain.com we find our master log position and log file name by doing show master status; We use the file name and file position in the command below.
On host2 we define host1.mydomain.com as our master by doing these three commands:
change master to master_host='host1.mydomain.com',master_user='repl',master_password='password';
change master to master_log_file='log file name from show master status';
change master to master_log_pos=number_from_the_show_master_status;
Note the log_pos is not quoted since it's a number.
Now start the slave by doing start slave; Check your values by doing show master status\G which will show you a tabular list of all the settings and the status of the slave.
If all your settings are correct look for
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
which says the threads for replication are now running on your host1.mydomain.com and receiving data from host2.mydomain.com. If they are not Yes then check your mysql error log which will likely pinpoint the problem. Most of the time the errors are due to host1.mydomain.com and host2.mydomain.com not being able to talk to one another or binary logging not running or server ids not being set.
Now repeat this process on host2.mydomain.com. You will do the change master commands on host2.mydomain.com and you will do the show master status command on host1.mydomain.com