Skip to content

vikrantkakad/Master-Slave-Replication-MySQL-5.7

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

Master-Slave-Replication-MySQL-5.7

License

This document expains the step-by-step process to
         a. Create Master-Slave Replication
         b. Pause Replication
         c. Resume Replication
         d. Disable Replication
         e. Restore Database

alt text


Create Master-Slave Replication:

Step 1: Master Database Configuration:

  1. Open MySql Configuration File

  sudo vi /etc/mysql/my.cnf

         a. Add Master Server's IP Address in place of <master_ip_address>:

      bind-address = <master_ip_address>

         b. Uncomment/Insert below lines:

      server-id = 1
      log_bin = /var/log/mysql/mysql-bin.log

         c. Add the Master Database name in place of <database_name>:

      binlog_do_db = <database_name>
  1. Restart Master MySql Server:

  sudo service mysql restart
  1. Connect to Master MySql Database:

  mysql -u <username> -p
  1. Give rights to slave_user on Master database:

  GRANT REPLICATION SLAVE ON *.* TO '<username>'@'%' IDENTIFIED BY '<password>';
  FLUSH PRIVILEGES;
  1. Check Pointer Status and Record the values:

  USE <database_name>;
  FLUSH TABLES WITH READ LOCK;
  SHOW MASTER STATUS

         * Note down the values for status response.
         * If required, then take the backup of Master database for creating Slave using below command:

        mysqldump -u <username> -p --opt <database_name> > /path/to/database.sql
  1. Then complete the Master database configuration by releasing the Locks on tables:

  UNLOCK TABLES;
  QUIT;

alt text

Step 2: Slave Database Configuration:

  1. Connect to Slave MySql Database:

  mysql -u <username> -p
  1. Create Slave Database Schema:

  CREATE DATABASE <database_name>;
  EXIT;
  1. Import the database that you previously exported from the Master:

  mysql -u <username> -p <database_name> < /path/to/database.sql;
  1. Open the MySql Configuration File:

  sudo vi /etc/mysql/my.cnf

         a. Uncomment/Insert below line as:

      server-id = 2
      log_bin = /var/log/mysql/mysql-bin.log
      binlog_do_db = <database_name>
      relay-log = /var/log/mysql/mysql-relay-bin.log
  1. Restart the Slave MySql Server:

  sudo service mysql restart
  1. Connect to Slave MySql Database:

  mysql -u <username> -p
  1. Enable the replication from slave MySql (here enter the values of MASTER_LOG_FILE & MASTER_LOG_POS noted earlier from SHOW MASTER STATUS; query on Master Database Server):

  CHANGE MASTER TO MASTER_HOST=<master_ip_address>, MASTER_USER='<username>', MASTER_PASSWORD='<password>', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=107;
  1. Start the Slave Server:

  START SLAVE;
  1. Check Slave Server Status:

  SHOW SLAVE STATUS\G

         * If there is an issue in connecting, you can try starting slave with a command to skip over it by:

        SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
        SLAVE START;
        EXIT;

Temporarily Pause/Disable the Replication

Steps to Temporarily Pause/Disable the Replication on Slave Server:

  1. Connect to Slave MySql Database:

  mysql -u <username> -p
  1. Stop the SQL Thread from Execution Relay Logs on Slave Server:

  STOP SLAVE SQL_THREAD;
  1. Now as the Slave has stopped, we can perform deployment/maintenance/etc. tasks on the Master Database. These changes will be automatically reflected on the Slave Database once you restart the Slave Server.

  2. Restart the SQL thread again on Slave Server:

  START SLAVE SQL_THREAD;
  EXIT;

Permanently Disable the Replication:

Steps to Permanently/Completely Disable the MySQL replication on Slave Server:

  1. Connect to Slave MySql Database:

  mysql -u <username> -p
  1. Stop the SQL Thread on Slave Server:

  STOP SLAVE;
  1. Reset the SQL Thread on Slave Server:

  RESET SLAVE ALL;
  EXIT;
  1. Edit the my.cnf of Slave:

         * Edit the my.cnf file and remove any information (if present) which refers to "master-..." or "replicate-..." options. Sometimes you may not have anything in the my.cnf, since replication can be setup dynamically as well.

  1. Restart the Slave Server:

  sudo service mysql restart

Database Restoration:

Steps to Restore the Master Databases from Slave Server in Replication:

  1. Connect to Slave MySql Database:

  mysql -u <username> -p
  1. Stop the SQL Thread on Slave Server:

  STOP SLAVE;
  EXIT;
  1. Copy the database schema dump from Master to Slave Server:

  scp <remote_user>@<remote_host_ip>:/path/to/remote/database.sql /path/to/local/file;
  1. Connect to Master MySql Database:

  mysql -u <username> -p
  1. Drop the Database Schema on Master Server:

  DROP DATABASE <database_name>;
  1. Re-Create Master Database Schema:

  CREATE DATABASE <database_name>;
  EXIT;
  1. Import the database that you previously exported from the Master:

  mysql -u root -f -p <database_name> < /path/to/database.sql
  1. Connect to Slave MySql Database:

  mysql -u <username> -p
  1. Drop the Database Schema on Slave Server:

  DROP DATABASE <database_name>;
  1. Create Slave Database Schema:

  CREATE DATABASE <database_name>;
  EXIT;
  1. Import the database that you previously exported from the Master:

  mysql -u root -f -p <database_name> < /path/to/database.sql
  1. Connect to Master MySql Database:

  mysql -u <username> -p
  1. Check Pointer Status and Record the values on Master Server:

  USE <database_name>;
  SHOW MASTER STATUS;
  EXIT;
  1. Connect to Slave MySql Database:

  mysql -u <username> -p
  1. Check Pointer Status and Record the values on Slave Server:

  USE <database_name>;
  SHOW SLAVE STATUS;
  EXIT;
  1. Enable the replication from slave MySql (here enter the values of MASTER_LOG_FILE & MASTER_LOG_POS noted earlier from SHOW MASTER STATUS; query on Master Database Server):

  CHANGE MASTER TO MASTER_HOST=<master_ip_address>, MASTER_USER='<username>', MASTER_PASSWORD='<password>', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=107;
  1. Start the Slave Server:

  START SLAVE;
  1. Check Slave Server Status:

  SHOW SLAVE STATUS\G
  EXIT;

         * Slave Status now should be similar to Master Server. Also test if Master-Slave Replication is working or not by doing some changes at any test table in Master Database; which should be reflected in Slave Database.


Copyright © Vikrant Kakad 2018