You are on page 1of 3

Mysql master slave replication on CentOs 6.4 >>>On master server: # yum install mysql-server mysql # vim /etc/my.

cnf >>>Add the following entries under [mysqld] server-id = 1 binlog-do-db=db1 ###in this we define the database which we want to replicate. if we want to replicate all database then comment this line.### relay-log = /var/lib/mysql/mysql-relay-bin relay-log-index = /var/lib/mysql/mysql-relay-bin.index log-error = /var/lib/mysql/mysql.err master-info-file = /var/lib/mysql/mysql-master.info relay-log-info-file = /var/lib/mysql/mysql-relay-log.info log-bin = /var/lib/mysql/mysql-bin # /etc/init.d/mysqld restart # mysql -u root -p mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'your_pa ssword'; mysql> FLUSH PRIVILEGES; mysql> FLUSH TABLES WITH READ LOCK; mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 11128001 | tecmint | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql> quit; >>>Please write down the File (mysql-bin.000003) and Position (11128001) numbers , we required these numbers later on Slave server. Next apply READ LOCK to datab ases to export all the database and master database information with mysqldump c ommand. # mysqldump -u root -p --all-databases --master-data > /root/dbdump.db >>> scp dbdump.db to slave server. >>>Once you ve dump all the databases, now again connect to mysql as root user and unlcok tables. # mysql -u root -p mysql> UNLOCK TABLES; mysql> quit; >>>on slave server: # yum install mysql-server mysql

# vi /etc/my.cnf >>>Add the following entries under [mysqld] section and don t forget to replace IP address of Master. server-id = 2 master-host=192.168.1.1 master-connect-retry=60 master-user=slave_user master-password=yourpassword replicate-do-db=tecmint relay-log = /var/lib/mysql/mysql-relay-bin relay-log-index = /var/lib/mysql/mysql-relay-bin.index log-error = /var/lib/mysql/mysql.err master-info-file = /var/lib/mysql/mysql-master.info relay-log-info-file = /var/lib/mysql/mysql-relay-log.info log-bin = /var/lib/mysql/mysql-bin >>>Now import the dump file that we exported in earlier command and restart the MySQL service. # mysql -u root -p < /root/dbdump.db # /etc/init.d/mysqld restart # mysql -u root -p mysql> slave stop; mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.1', MASTER_USER='slave_user', MAS TER_PASSWORD='yourpassword', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS= 11128001; mysql> slave start; mysql> show slave status\G >>>>Verifying MySQL Replication on Master and Slave Server On Master Server mysql> mysql> mysql> mysql> mysql> create database tecmint; use tecmint; CREATE TABLE employee (c int); INSERT INTO employee (c) VALUES (1); SELECT * FROM employee;

+------+ | c | +------+ | 1 | +------+ 1 row in set (0.00 sec) On Slave Server mysql> use tecmint; mysql> SELECT * FROM employee; +------+ | c |

+------+ | 1 | +------+ 1 row in set (0.00 sec)

You might also like