In this tutorial I will show how to replicate the database exampledb from the master with the IP address 192.168.0.100 to a slave. Both systems (master and slave) are running CentOS BlueQuartz; however, the configuration should apply to almost all distributions with little or no modification. Both systems have MySQL installed, and the database exampledb with tables and data is already existing on the master, but not on the slave. I want to say first that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you! 1 Configure The Master First we have to edit /etc/my.cnf. By defaul that file looks like this on CentOS + BlueQuartz:
-----------------------------/etc/my.cnf (original)----------------------- [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Default to using old password format for compatibility with mysql 3.x # clients (those using the mysqlclient10 compatibility package). old_passwords=1
[mysql.server] user=mysql basedir=/var/lib
[mysqld_safe] err-log=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid -------------------------------------------------------------------------------- Change it to this: -----------------------------/etc/my.cnf (modified)----------------------- [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Default to using old password format for compatibility with mysql 3.x # clients (those using the mysqlclient10 compatibility package). old_passwords=1
log-bin = /var/log/mysql/mysql-bin.log binlog-do-db=mydns server-id=1 [mysql.server] user=mysql basedir=/var/lib [mysqld_safe] err-log=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid -------------------------------------------------------------------------------- Next create the directory /var/log/mysql and make it writeable for the UID and GID of the MySQL user and group: mkdir /var/log/mysql chown mysql:mysql /var/log/mysql 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. GRANT SUPER, REPLICATION, CLIENT, SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY '<some_password>'; (Replace <some_password> with a real password!) FLUSH PRIVILEGES; Next (still on the MySQL shell) do this: USE exampledb; FLUSH TABLES WITH READ LOCK; SHOW MASTER STATUS; The last command will show something like this: +---------------+----------+--------------+------------------+ | File | Position | Binlog_do_db | Binlog_ignore_db | +---------------+----------+--------------+------------------+ | mysql-bin.006 | 183 | exampledb | | +---------------+----------+--------------+------------------+ 1 row in set (0.00 sec) Write down this information, we will need it later on the slave! Then leave the MySQL shell: quit; Now get the existing tables and data from exampledb from the master to the slave. For that make a database dump: mysqldump -u root -p<password> --opt exampledb > exampledb.sql (Replace <password> with the real password for the MySQL user root! Important: There is no space between -p and <password>!) This will create an SQL dump of exampledb in the file exampledb.sql. Transfer this file to your slave server! Finally we have to unlock the tables in exampledb: mysql -u root -p Enter password: UNLOCK TABLES; 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 exampledb: mysql -u root -p Enter password: CREATE DATABASE exampledb; quit; Now import the SQL dump exampledb.sql that you created on the master: mysql -u root -p exampledb <exampledb.sql 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 exampledb. Therefore we change our /etc/my.cnf to this: -----------------------------/etc/my.cnf (modified)----------------------- [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Default to using old password format for compatibility with mysql 3.x # clients (those using the mysqlclient10 compatibility package). old_passwords=1 log-bin = /var/log/mysql/mysql-bin.log
server-id=2 master-host=192.168.0.100 master-user=slave_user master-password=<password> master-connect-retry=60 replicate-do-db=exampledb
[mysql.server] user=mysql basedir=/var/lib [mysqld_safe] err-log=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid -------------------------------------------------------------------------------- Then we restart MySQL: /etc/init.d/mysql restart Finally, we must do this: mysql -u root -p Enter password: SLAVE STOP; In the next command (still on the MySQL shell) you have to replace the values appropriately: 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 START SLAVE; quit; That's it! Now whenever exampledb is updated on the master, all changes will be replicated to exampledb on the slave. Test it! This information is based on the guide from Falko Timme, another guide from aciddrop.com and helpful suggestions from Guilhem Bichot. All credits and praise go to the individual contributors, I'll take the blame for procedural errors. :o) |