INDEXA. MySQL Mater-Slave and Master-Master Replication; Step by Step configurationinstructionsB. Setting up MySQL Master-Slave ReplicationC. Second Method to implementation for master-slave replication onMySQLD. MySQL Master- Master Replication
(A)MySQL master-slave and master-master replication. Step by stepconfigurationinstructions:If you are looking for the options to scale your MySQL installation you may be alsointerested in MySQL partitioning and sub partitioning. It may improve each node speedand capacity parameters.One may say that there are a lot of MySQL replication manuals, but latest versions ofMySQL server have changed the way how configuration should be applied. Most of themanuals do not reflect these changes.Just want to mention that this technique is usually used for load balancing on databaseservers. If you have a lot of read requests (most common for web applications) master-slave replication should suit your needs well.In this case you will do write transactions on master host and read requests on slavehosts, because data is populated from master to slave much faster than from slaves tomaster and to other slaves.But sometimes you might have more write requests or may have other (applicationrelated) reasons to start another type of replication. You can see it on the next figure andthat is so called master-master replication.
In this article I will describe simple master-slave architecture with 2 hosts and simplemaster-master replication with the same 2 hosts. Our final goal is to configure master-master replication, what includes several sub-steps, so let’s start. Sure you shouldconfigure network services on both systems.(B) Setting up MySQL Master-Slave Replication :This article is about setting up MySQL Master-Slave replication between two CloudServers.Setup Outline:For the purpose of this scenario, we will have two Servers, named INVIRH54DB3and INVIRH54DB2. These Servers have two IP addresses. For the duration of thescenario INVIRH54DB3 (10.65.43.200) is considered master MySQL server(running in read-write mode), and INVIRH54DB2 (10.65.42.199) is considered theslave (running in read-only mode).If you already have a MySQL database running on the master node, a dump andrestore into the slave would be required into the slave node before configuringreplication between them. We will be using the mysqldumpcommand to dump adatabase into a file, transferring it and restoring it to the slave. Once the necessaryconfiguration has been carried out, replication will be in effect from that pointonwards.Step: 1 Install MySQL in two machines.Step: 2 Configuration of master serverConfigure your Servers:Master: (INVIRH54DB3)Youll need to edit the my.cnf file on the Master server to enable binary logging and setthe servers id.Like: Add these lines under the [mysqld] section:
This will enable binary logging and set the Masters server-id to 1. Restart MySQL for thechanges to take effect. Then we restart MySQL.Master my.cnf configuration done.Slave (INVIRH54DB2)Youll need to edit the my.cnf file on the Slave server to set the servers id to somethingdifferent than the master.
Add these lines under the [mysqld] section.This assign the Slave a unique server-id. Nowlet’s restart MySQL so these changes take effect.Slave my.cnf configuration done.
Step: 3 Taking Backup on Mater ServerIn Master side we have following databases:In Slave side we have following databases:Next apply READ LOCK to databases to export all the database and master databaseinformation with mysqldump command.Grab the Master Information:On the Master server, well need to find out the binary log it is currently using and theposition it is at in that binary log. If you havent logged out of MySQL from creating thatreplication user, you should be at the MySQL prompt already. Go ahead and lock thetables so we can safely grab that data.Log into the master server.
Command :FLUSH TABLES WITH READ LOCK;After giving “FLUSH TABLES WITH READ LOCK” Command on master server, we need totake full back up on master server by using following command.# mysqldump -u root -p --all-databases --master-data > /root/dbdump.dbOr# mysqldump -u root -p --databases sample1 sample2 sample3 > /tmp/masterdata.sqlOnce you’ve dump all the databases, now again connect to mysql as root user and unlcoktables.mysql> UNLOCK TABLES;mysql> quit;
Step: 4 Creating a Replication User (In Master Server)The Slave server will connect to the Master via a standard MySQL user and password.This user will need to have been granted the REPLICATION SLAVE privilege. Lets createthat user on the Master server now.You should be at the mysql prompt on the Master server. Once there, lets create a newuser and grant him the proper privilege.This statement is basically saying grant the ‘replication’ user replication slave access toany database/table on this host from the Slave servers private IP. Now tell the server toreload the grant tables with the statement FLUSH PRIVILEGES.
At this point, its a good idea to make sure youre able to access the Master from theSlave using the credentials you just created. Alright, now back to the Slave server.Here password is replicationBe sure to use your private IP for your Master server. In this example, mine is10.65.43.200. Do you get a MySQL prompt? If so, youre successfully logged into yourMaster server using the new replication user you just created. Alright, were about halfway there.Upload the database dump file from master server to Slave Server (10.65.42.199) usingSCP command.Command: scp /firstname.lastname@example.org:/tmp/
Now we need to check that file copied or not on slave sideStep: 5 Import to the Databases on slave sideNow import the dump file that we exported in earlier command and restart the MySQLservice.Before importing DB’s on slave server :Commandis : (For importing)[root@INVIRH54DB2 ~]# mysql -u root -p < /tmp/masterdata.sqlEnter password:Here password is mysql
This time to check all databases are imported or not in slave serverEarlier we have only 4 databases in slave server, after importing DB’s from dump file weget 7 databases.Step: 6 Check the Mater Status (On Master Server)Note the current binary log and position. In our example, the Master server is currentlyon the mysql-bin.00002 binary log and is at position 347.Now well use the SHOW MASTER STATUS command to get all the data we need.
Step: 7Final Slave Config: (In Slave server)Changing the Master Information on the Slave:Up to this point, you should have collected the following information:Private IPsreplication username and passwordMaster server binary log and position.Now its time to use this information to connect the Slave server to the Master server.Lets start configuring the Slave.Restart the MySQL service. (Slave Server)# /etc/init.d/mysqld restart
Once at the mysql prompt, were going to change the Master information with theCHANGE MASTER TO statement (so the Slave server knows where/how its connecting).Note the values for each field. The MASTER_HOST is the private IP of the Master server,MASTER_USER is the user we created for replication, MASTER_PASSWORD is thepassword for the replication user, MASTER_LOG_FILE is the binary log that we recordedfrom the Master server status earlier, and MASTER_LOG_POS is the position the Masterwas in that we recorded.Step: 8Start the Slave Thread (In Slave Server)Ok, now start the slave thread on the Slave server.Let’s make sure that replication is working with the SHOW SLAVE STATUS statement.(In Slave Server)
If you see that Slave_IO_State is Waiting for master to send event andSeconds_Behind_Master isnt NULL, your replication is working! If it does show NULL,go back and double check your CHANGE MASTER TOstatement to make sure all data iscorrect. Thats it...
Step: 9Testing scenariosFor the purpose of testing our replication setup, we can create a new database(SAMPLE4) and associated table (EMP3) on INVIRH54DB3 (Master), inserting data toconfirm that the changes are mirrored on INVIRH54DB2. Here is an example:In Mater server:The changes should be visible on INVIRH54DB2 immediately.
In Slave server:In Case if the master is go down stage :In Slave need to check whether database and tables are accessible or not?
The slave server is never down.To see the server_ids on the mysql prompt type this command :Mysql> show variables like %server%id;
(C)This is the Second way to implementation on master-slave replicationonmysql:Database replication is possible under MySQL via binary logging. This is a log over allchanges that are done to a particular database configured in/etc/mysql/my.cnf.This "change log" is then shared with another MySQL server (the slave) which is the keptin a synchronous state. First we need to install bothon MySQL servers.In my Scenario,Let us assume: INVIRH54DB3 -10.65.43.200 ------ MasterAndINVIRH54DB2 – 10.65.42.199 ----- SlaveINVIRH54DB3(MASTER)INVIRH54DB2(SLAVE)
Step: 1 Configuration of master serverOn the master:Edit vi /etc/my.cnf#bind-address = 127.0.0.1Server-id = 1Log-bin = /var/lib/mysql/mysql-bin.logExpire-logs-days = 20max_binlog_size = 104857600Binlog-do-db = testdatabaseBinlog-ignore-db = MySQL, test,sample4We need to disable bind-address (slave needs to talk to master via the interface).Furthermore we tell the master that it is server 1 and that we want replication ofdatabase testdatabase, but not mysql and test.Then we restart MySQL:/etc/init.d/mysql restart
Then we log into the MySQL database as root and create a user with replicationprivileges:
Step: 2 Configuration of slave servermysql> create database testdatabase;Now we have to tell MySQL on the slave that it is the slave, that the master is10.65.43.200, and that the master database to watch is testdatabase. Therefore we addthe following lines to /etc/mysql/my.cnf:Then we restart MySQL./etc/init.d/mysql restart
From MySQL on the slave the master must be configured as well. Here theMASTER_LOG_FILE value of mysql-bin.000004 and the MASTER_LOG_POS valueof 197 are the values acquired from the SHOW MASTER STATUS on the master.If you are facing any issue like thisSolution:ERROR 1200(HY00) :The server is not configured as slave ;fix in config file or withCHANGES MASTER TOSo, you have restarted mysqld, and the slave did not start. You issue SHOW SLAVESTATUSG and notice that everything is fine but “Slave_IO_Running” and“Slave_SQL_Running” both say “No” .This has happened to me numerous throughout my time administering mysql servers.The variable server_id always need to be set to something when running replication.Check if server_id is set by using “SHOW VARIABLES LIKE ‘server_id;”.The value should be “0” or “1”.”1” is normally set by default.Also,the master’s server_idand the slave’s server_id should not be the same. You can set server id by using “SETGLOBAL server_id=;” then issue a START SLAVE;we should be fine now…
Step: 3 Testing the setupTable creationOn MasterCREATE TABLE example (id INT, dataVARCHAR (100));On slave: Check,MySQL>SHOW TABLES;
--Write test:On MasterOn Slave:Before inserting rows.After inserting rows from master to slave:
In Case of Master is down:On Slave:(D) MySQL Master- Master Replication:This tutorial describes how to set up MySQL master-master replication. We need toreplicate MySQL servers to achieve high-availability (HA). In my case I need two mastersthat are synchronized with each other so that if one of them drops down, other couldtake over and no data is lost. Similarly when the first one goes up again, it will still beused as slave for the live one.Here is a basic step by step tutorial that will cover the MySQL master and masterreplication.Note:We will call system 1 as master1 and slave2 and system2 as master2 and slave 1.
Step 1:Install MySQL on master 1 and slave 1. Configure network services on both systems, likeMaster 1/Slave 2 ip: 10.65.43.200 (INVIRH54DB3)Master 2/Slave 1 ip : 10.65.42.199 (INVIRH54DB2)Step 2:On Master 1, make changes in my.cnf:[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockold_passwords=1log-binbinlog-do-db=<database name> # input the database which should be replicatedbinlog-ignore-db=mysql # input the database that should be ignored for replicationbinlog-ignore-db=testserver-id=1[mysqld_safe]err-log=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid
Step 3:On master 1, create a replication slave account in MySQL.MySQL> grant replication slave on *.* to ‘replication’@10.65.42.199 identified byreplication;And restart the MySQL master1.
Step 4:Now edit my.cnf on Slave1 or Master2:[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockold_passwords=1Server-id=2master-host = 192.168.16.4master-user = replicationmaster-password = slavemaster-port = 3306[mysqld_safe]err-log=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pidStep 5:Restart MySQL slave 1 and atMySQL> start slave;MySQL> show slave statusG;
Above highlighted rows must be indicating related log files and Slave_IO_Running andSlave_SQL_Running: must be to YES.
Step 6:On master 1:MySQL> show master status;The above scenario is for master-slave, now we will create a slave master scenario forthe same systems and it will work as master-master.Step : 7On Master2/Slave 1, edit my.cnf and master entries into it: (10.65.42.199)[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock# Default to using old password format for compatibility with mysql 3.x# clients (those using the mysqlclient10 compatibility package).old_passwords=1server-id=2master-host = 10.65.43.200master-user = replicationmaster-password = slavemaster-port = 3306log-bin #information for becoming master addedbinlog-do-db=mysql
[mysqld_safe]err-log=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pidStep 8:Create a replication slave account on master2 for master1:MySQL> grant replication slave on *.* to email@example.com identified byslave2;Step 9:Edit my.cnf on master1 for information of its master. (INVIRH54DB3)[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock# Default to using old password format for compatibility with mysql 3.x# clients (those using the mysqlclient10 compatibility package).old_passwords=1log-binbinlog-do-db=mysqlserver-id=1#information for becoming slave.master-host = 10.65.42.199master-user = replicationmaster-password = slave2master-port = 3306
Step 10:Restart both MySQL master1 and master2.On MySQL master1:mysql> start slave;
On mysql master2:mysql> show master status;On mysql master 1:mysql> show slave statusG;@@@@@@@@@@@@@@@@@@@@@@@To setup Master and Slave Replication:Master to Slave Replication:In Master Server1. After installing mysql in master server. below are the steps to docd /etc/mysql/cpmy.cnf my.cnf_bkf_`date +%F`vim my.cnfuncommentlog_bin variable(To enable Binary logs)setserver_id = 1 ---- should be uniqueuncommentbind_address variableSave changes and restart the mysql service (/etc/init.d/mysql restart)In Slave Server2. After installing mysql in slave server. below are the steps to docd /etc/mysql/cpmy.cnf my.cnf_bkf_`date +%F`vim my.cnfsetserver_id = 2 ---- should be uniqueSave changes and restart the mysql service (/etc/init.d/mysql restart)In Master Server
3. Full Backup ProcessOpen one terminal andFlush tables with read lock; (Do not quit the server until the full backup process(Step4) completes )show master status; (Copy log file name and position)4. Open another terminal and login to master (Do not exit the previous terminal)mysqldump -u root -ppadnet --all-databases >fullbackup_`date +%F`.sql5. Now go to 1st terminal andunlock tables;exit;6. Move the full backup to slave using scp commandscp -Pportno(ifany) filename username@ip:/path7. In MasterGRANT REPLICATION SLAVE ON *.* TO repuser@slaveip identified by password;FLUSH PRIVILEGES;8.Login to slavemysql -u root -ppadnet<filename.sql
CHANGE MASTER TOmaster_host=master_ip_addresss,master_password=password,master_user=repuser,master_log_file=log file name,master_log_pos=pos;FLUSH PRIVILEGES;start slave;show slave statusG@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@DB3mysql> insert into user(host,user,password,select_priv) values(%,root,password(mysql),Y);ERROR 1364 (HY000): Field ssl_cipher doesnt have a default valueDb2mysql> insert into user(host,user,password,select_priv) values(%,root,password(mysql),Y);Query OK, 1 row affected, 3 warnings (0.00 sec)http://www.tecmint.com/how-to-setup-mysql-master-slave-replication-in-rhel-centos-fedora/