Steps for MySQL DB Replication

Following are the steps for MySQL Replication implementation on Linux machine:

Pre-implementation steps for DB Replication:

1.    Identify the databases to be replicated

2.    Identify the tables to be ignored during replication per database for example log tables

3.  Carefully identify and replace the variables and paths(locations) mentioned (in bold) in the commands given below with appropriate values

4.  Schedule the maintenance activity in odd hours as these activities will affect all the databases on Master database server

Implementation steps for DB Replication:

1.    Configure the **/etc/**my.cnf file on Master database server to enable Binary logging, setting of server id and configuring of dbnames for which logging should be done.

[mysqld]

log-bin=mysql-bin

server-id=1

binlog-do-db = dbname

Note: You can specify multiple DB in binlog-do-db by using comma separated dbname values like: dbname1_, dbname2, …, dbnameN_

2.    On Master database, Grant Replication Slave Privileges, by executing following command on mysql prompt

mysql> GRANT REPLICATION SLAVE ON *.* TO slaveuser@ identified by ‘slavepassword’;

3.    Stop the Master & Slave database by giving the command

     mysqladmin shutdown

4.    Start the Master database by giving the command

     **/usr/local/mysql-5.0.22/**bin/mysqld_safe --user=user&

5.    mysql> FLUSH TABLES WITH READ LOCK;

Note: Leave the client (putty session) from which you issued the FLUSH TABLES statement running, so that the read lock remains in effect. If you exit the client, the lock is released.

6.    mysql > SHOW MASTER STATUS;

         +---------------+----------+--------------+------------------+

         | File          | Position | Binlog_Do_DB | Binlog_Ignore_DB |

         +---------------+----------+--------------+------------------+

         | mysql-bin.003 | 117       | dbname       |                  |

         +---------------+----------+--------------+------------------+

Note: Note this information as this will be required while starting of Slave and replication in later steps

7.    Take MySQL dump by giving the following command, In another session window (putty window) run the following command:

mysqldump –u user --ignore-table=dbname.tbl_name -–ignore-table=dbname.tbl_name2 --master-data dbname > dbname_dump.db

Note: When choosing databases to include in the dump, remember that you will need to filter out databases on each slave that you do not want to include in the replication process.

8.    Unlock the tables on Master by giving following command:

mysql> UNLOCK TABLES;

9.    Copy the dump file to Slave DB server

10.  Startup the Slave by using option --skip-slave

     /usr/local/mysql-5.0.22/bin/mysqld_safe --user=user --skip-slave&

11.  Restore the dump file on Slave DB server

     mysql –u user dbname < dbname_dump.db

12.  Stop the Slave database by giving the command

     mysqladmin shutdown

13.  Configure the **/etc/**my.cnf file on the Slave database server

[mysqld]

server-id=2

replicate-ignore-table = dbname.tablename

14.  Start the Slave Mysql Server with 'replicate-do-db=DB name' option.

     /usr/local/mysql-5.0.22/bin/mysqld_safe --user=user --replicate-do-db=dbname --skip-slave

15.  Configure the settings at Slave server for Master host name, log filename and position within the log file as shown in Step 6 above

Use Change Master statement in the MySQL session

mysql> CHANGE MASTER TO MASTER_HOST='**<master_host_name>**', MASTER_USER='**<replication_user_name>**', MASTER_PASSWORD='**<replication_password>**', MASTER_LOG_FILE='**<recorded_log_file_name>**', MASTER_LOG_POS=**<recorded_log_position>**;

16.  On Slave Servers mysql prompt give the following command:

a.     mysql > START SLAVE;

b.    mysql > SHOW SLAVE STATUS;

Note:

To stop slave for backup or any other activity you can use the following command on the Slave Servers mysql prompt:

mysql> STOP SLAVE

Refer following links for more information on MySQL DB Replication:

http://dev.mysql.com/doc/refman/5.0/en/replication-options.html

http://crazytoon.com/2008/04/21/mysql-replication-replicate-by-choice/

http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html

This article is part of the GWB Archives. Original Author: Manish Agrawal

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.