MySQL Replication

Reading time: 4 - 7 minutes

A

1 Configure The Master

First we have to edit /etc/my.cnf. We have to enable networking for MySQL, and MySQL should listen on all IP addresses, therefore we comment out these lines (if exists):

#skip-networking
#bind-addressAAAAAAAAAAA = 127.0.0.1

A

Furthermore we have to tell MySQL for which database it should write logs (these logs are used by the slave to see what has changed on the master), which log file it should use, and we have to specify that this MySQL server is the master. We want to replicate the database dev, so we put the following lines into /etc/my.cnf:

log-bin = /var/log/mysql/mysql-bin.log
binlog-do-db=dev
server-id=1

A

Then we restart MySQL:

/etc/init.d/mysql restart

Then we log into the MySQL database as root and create a user with replication privileges:

mysql -u root -p
Enter password:

Now we are on the MySQL shell.

mysql>GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY '<some_password>'; (Replace <some_password> with a real password!)
mysql>FLUSH PRIVILEGES;

Next (still on the MySQL shell) do this:

mysql>USE dev;
mysql>FLUSH TABLES WITH READ LOCK;
mysql>SHOW MASTER STATUS;

The last command will show something like this:

+---------------+----------+--------------+------------------+
| FileAAAAAAAAA | Position | Binlog_do_db | Binlog_ignore_db |
+---------------+----------+--------------+------------------+
| mysql-bin.006 | 183AAAAA | devAAA |AAAAAAAAAAAAAAAAA |
+---------------+----------+--------------+------------------+
1 row in set (0.00 sec)

A

Write down this information, we will need it later on the slave!

Then leave the MySQL shell:

quit;

There are two possibilities to get the existing tables and data from dev from the master to the slave. The first one is to make a database dump, the second one is to use the LOAD DATA FROM MASTER; command on the slave. The latter has the disadvantage the the database on the master will be locked during this operation, so if you have a large database on a high-traffic production system

If you want to follow the first method, then do this:

mysqldump -u root -p<password> --opt dev > dev.sql

This will create an SQL dump of dev in the file dev.sql. Transfer this file to your slave server!

If you want to go the LOAD DATA FROM MASTER; way then there is nothing you must do right now.

A


Finally we have to unlock the tables in dev:

A

#mysql -u root -p
Enter password:
mysql>UNLOCK TABLES;
mysql>quit;

Now the configuration on the master is finished. On to the slave...

2 Configure The Slave

On the slave we first have to create the database dev:

#mysql -u root -p
Enter password:
mysql>CREATE DATABASE dev;
mysql>quit;

A


If you have made an SQL dump of dev on the master and have transferred it to the slave, then it is time now to import the SQL dump into our newly created dev on the slave:

A

#mysql -u root -p<password> dev < /path/to/dev.sql

If you want to go the LOAD DATA FROM MASTER; way then there is nothing you must do right now.

A


Now we have to tell MySQL on the slave that it is the slave, that the master is 192.168.0.100, and that the master database to watch is dev. Therefore we add the following lines to /etc/my.cnf:

A

server-id=2
master-host=192.168.0.100
master-user=slave_user
master-password=secret
master-connect-retry=60
replicate-do-db=dev

A

Then we restart MySQL:

/etc/init.d/mysql restart

A


If you have not imported the master dev with the help of an SQL dump, but want to go the LOAD DATA FROM MASTER; way, then it is time for you now to get the data from the master dev:

A

#mysql -u root -p
Enter password:
mysql>LOAD DATA FROM MASTER;
mysql>quit;

A


Finally, we must do this:

A

#mysql -u root -p
Enter password:
mysql>SLAVE STOP;

In the next command (still on the MySQL shell) you have to replace the values appropriately:

Mysql>CHANGE MASTER TO MASTER_HOST='192.168.0.100', MASTER_USER='slave_user', MASTER_PASSWORD='<some_password>', MASTER_LOG_FILE='mysql-bin.006', MASTER_LOG_POS=183;

  • MASTER_HOST is the IP address or hostname of the master (in this example it is 192.168.0.100).
  • MASTER_USER is the user we granted replication privileges on the master.
  • MASTER_PASSWORD is the password of MASTER_USER on the master.
  • MASTER_LOG_FILE is the file MySQL gave back when you ran SHOW MASTER STATUS; on the master.
  • MASTER_LOG_POS is the position MySQL gave back when you ran SHOW MASTER STATUS; on the master.

Now all that is left to do is start the slave. Still on the MySQL shell we run

mysql>START SLAVE;
mysql>quit;

That's it! Now whenever dev is updated on the master, all changes will be replicated to dev on the slave. Test it!

  • Share/Bookmark

Leave a Reply