Wednesday, June 4, 2014

Set Up Master Slave Replication in MySQL

                                    MySQL replication is the process by which a single data set, stored in a MySQL database, will be live-copied to a second server,This configuration, called "master-slave" replication.             This will cover a very simple example of mysql replication—one master will send information to a single slave.This will use the following IP addresses:
                         10.3.3.95 - Master Database   and 10.3.3.96 - Slave Database
Configuration on the Master Database Server
1. Installation of MYSQL : sudo apt-get install mysql-server mysql-client
2. Configure the Master Database

                     vi /etc/mysql/my.cnf                                
                                 #bind-address = 127.0.0.1
                                  server-id = 1
                                  log_bin = /var/log/mysql/mysql-bin.log
                                  binlog_do_db = jagan
After you make all of the changes, save and exit out of the configuration file.

3. Restart MySQL : sudo service mysql restart
4. In MySQL shell : mysql -u root -p

                  We need to grant privileges to the slave. You can use this line to name your slave and set up their password. The command should be in this format:
          GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'password';

eg : GRANT REPLICATION SLAVE ON *.* TO 'root'@'10.3.3.96' IDENTIFIED BY jagan123!';
FLUSH PRIVILEGES;
5. Check Status : SHOW MASTER STATUS;
 
6. Dump the DB in to the Master Server : mysqldump -u root -p --opt newdatabase > jagan.sql
Configuration on the Slave Database Server
1. Same As Master :So Execute the Following
sudo apt-get install mysql-server mysql-client
vi /etc/mysql/my.cnf
bind-address            = 127.0.0.1
bind-address            = 10.3.3.95  (Not Needed)
server-id                  =  2                                       
log_bin                      = /var/log/mysql/mysql-bin.log            
binlog_do_db             = jagan
2. Enable the replication from within the MySQL shell.
CHANGE MASTER TO MASTER_HOST='MASTERIPADDRESS', MASTER_USER='replication', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=107;

Eg:-
CHANGE MASTER TO MASTER_HOST='10.3.3.95',MASTER_USER='root', MASTER_PASSWORD='aview123!', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=  107;
3. Activate the slave server:START SLAVE;
4. Check the details of the slave replication.The \G rearranges the text to make it more readable.
SHOW SLAVE STATUS\G;
5. If there is an issue in connecting, you can try starting slave with a command to skip over it:
                         SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; SLAVE START;

No comments:

Post a Comment

bloggerwidgets