{"id":1990,"date":"2018-10-30T16:40:33","date_gmt":"2018-10-30T16:40:33","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=1990"},"modified":"2018-10-31T03:50:09","modified_gmt":"2018-10-31T03:50:09","slug":"mysql-replication-master-slave-setup","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/30\/mysql-replication-master-slave-setup\/","title":{"rendered":"MySQL Replication Master Slave Setup"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxadmin.io\/wp-content\/uploads\/2017\/04\/MySQL-Logo-2-e1493431150113.png\" alt=\"Mysql Replication\" width=\"300\" height=\"155\" \/><\/p>\n<p>MySQL Replication allows you to synchronize slave copies of a MySQL server. You can then use the slave to perform backups and a recovery option if the master should go offline for any reason. MySQL needs to be installed on both servers.<\/p>\n<p>Install MySQL on both servers:<\/p>\n<p>yum install -y mysql-server mysql-client mysql-devel<\/p>\n<p>Edit \/etc\/my.cnf on both servers and set a unique numerical server id(any number is fine as long as they are not the same):<\/p>\n<p>server-id = 1<\/p>\n<h3>Configure MySQL Replication On The Master<\/h3>\n<p>On the master ensure a bin log is set in \/etc\/my.cnf<\/p>\n<p>log_bin = \/var\/log\/mysql\/mysql-bin.log<\/p>\n<p>Restart mysql<\/p>\n<p>service mysqld restart<\/p>\n<p>Connect o mysql on the master<\/p>\n<p>mysql -u root -p<\/p>\n<p>Grant privileges to the slave<\/p>\n<p>GRANT REPLICATION SLAVE ON *.* TO &#8216;slave&#8217;@&#8217;%&#8217; IDENTIFIED BY &#8216;password&#8217;;<\/p>\n<p>Load the new privileges<\/p>\n<p>FLUSH PRIVILEGES;<\/p>\n<p>Lock the MySQL master so no new updates can be written while you are creating the slave<\/p>\n<p>FLUSH TABLE WITH READ LOCK;<\/p>\n<p>Get the current master status<\/p>\n<p>SHOW MASTER STATUS;<\/p>\n<p>This will return a similar result to this:<\/p>\n<p>mysql&gt; SHOW MASTER STATUS;<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n| mysql-bin.000001 | 107 | | |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n1 row in set (0.00 sec)<\/p>\n<p>This is the position the slave will be on, save this information for later. You will need to keep the mysql client open on the master. If you close it the read lock will stop and will cause replication issues when trying to sync it.<\/p>\n<p>Open a new ssh client and dump the databases<\/p>\n<p>mysqldump -u root -p &#8211;all-databases &gt; all.sql<\/p>\n<p>If it is particularly large mysql server, you can rsync all of \/var\/lib\/mysql<\/p>\n<p>Once the copy has completed go ahead and type the following on the MySQL master:<\/p>\n<p>UNLOCK TABLES;<\/p>\n<p>Go ahead and quit on the master<\/p>\n<h3>Configure MySQL Replication On The Slave<\/h3>\n<p>Import the databases on the slave<\/p>\n<p>mysql &lt; all.sql<\/p>\n<p>You should also enabled the server-id in \/etc\/my.cnf and restart it<\/p>\n<p>Once it has been restarted and the databases have been imported. You can setup the replication with the following with the following command in the mysql client:<\/p>\n<p>CHANGE MASTER TO MASTER_HOST=&#8217;IP ADDRESS OF MASTER&#8217;,MASTER_USER=&#8217;slave&#8217;, MASTER_PASSWORD=&#8217;password&#8217;, MASTER_LOG_FILE=&#8217;mysql-bin.000001&#8242;, MASTER_LOG_POS= 107;<\/p>\n<p>Change MASTER_LOG_FILE and MASTER_LOG_POS to the values you got earlier from the master. Once you have entered the above command go ahead and start the slave:<\/p>\n<p>START SLAVE;<\/p>\n<p>To check current slave status to<\/p>\n<p>SHOW SLAVE STATUS;<\/p>\n<p>This is a basic Master-Slave Mysql replication configuration.<\/p>\n<p>Apr 29, 2017LinuxAdmin.io<\/p>\n<p><a href=\"https:\/\/linuxadmin.io\/mysql-replication-master-slave-setup\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL Replication allows you to synchronize slave copies of a MySQL server. You can then use the slave to perform backups and a recovery option if the master should go offline for any reason. MySQL needs to be installed on both servers. Install MySQL on both servers: yum install -y mysql-server mysql-client mysql-devel Edit \/etc\/my.cnf &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/30\/mysql-replication-master-slave-setup\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;MySQL Replication Master Slave Setup&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1990","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1990","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=1990"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1990\/revisions"}],"predecessor-version":[{"id":2059,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1990\/revisions\/2059"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=1990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=1990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=1990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}