Steps in Mysql replication in Linux
This articles only guides you the steps to achieve a master-slave mysql replication. It won’t tell you any theory or even its advantages or disadvantages.
Login to the master instance.
Take a backup of my.cnf
Edit my.cnf as
—————
log-bin=mysql-bin
log-bin = /mnt/mysql/mysql-bin.log
server-id=1
binlog-do-db=db_name1
binlog-do-db=db_name2
binlog-do-db=db_name3
——–
The databases that need to be replicated.
Restart mysql
Login to the mysql prompt and create a user with replication privellage.
GRANT REPLICATION SLAVE ON *.* TO ’slaveuser’@'%’ IDENTIFIED BY ’slavepassword’;
FLUSH PRIVILEGES;
Execute the following in the shell.
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
mysql> SHOW MASTER STATUS;
+——————+———-+————–+——————+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000011 |Â Â Â 15040 |Â Â Â Â Â Â Â Â Â Â Â Â Â |Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
+——————+———-+————–+——————+
1 row in set (0.00 sec)
The output of show master (log file and position ) should be noted to use in slave.
quit;
mysqldump -u root -p –all-databases –lock-all-tables >dbdump.sql
mysql -u root -p
Enter password:
UNLOCK TABLES;
quit;
Login to the slave instance.
Execute the dump that is taken from master instance to slave.
Backup /etc/my.cnf
Open /etc/my.cnf
Add the following
——–
server-id=2
replicate-do-db=db_name1
replicate-do-db=db_name2
replicate-do-db=db_name3
——–
Restart mysql
—————————–
mast_ip - master
slave_ip - slave
—————————–
STOP SLAVE;
CHANGE MASTER TO MASTER_HOST=’mast_ip’, MASTER_USER=’slaveuser’, MASTER_PASSWORD=’slavepassword’, MASTER_LOG_FILE=’mysql-bin.000011′, MASTER_LOG_POS=98;
<<MASTER_HOST=Master server ip>>
Values of MASTER_LOG_FILE and MASTER_LOG_POS are getting from SHOW MASTER STATUS of master status
START SLAVE;
quit;
————————————
Also open necessary port 3306 from the hosts (master and slave)
————————————
How to test whether mysql is correct or not ???
Login to the master db
mysql -u root -p
show master status;
note down the log_pos and log_file
Login to the slave db
mysql -u root -p
show slave status;
Verifty the log_pos and log_file are correct.
Add a value in DB, and check the same procedure…
If the log_pos and log_file are correct then replication is DONE.
