{"id":2165,"date":"2013-07-07T11:36:44","date_gmt":"2013-07-07T03:36:44","guid":{"rendered":"http:\/\/rmohan.com\/?p=2165"},"modified":"2013-07-07T11:36:44","modified_gmt":"2013-07-07T03:36:44","slug":"database-replication-in-mysql","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=2165","title":{"rendered":"Database Replication In MySQL"},"content":{"rendered":"<h1>Database Replication In\u00a0MySQL<\/h1>\n<p>&nbsp;<\/p>\n<p><strong><em>Configure the MySQL Master<\/em><em>\u00a0Server<\/em><\/strong><\/p>\n<p><strong>Step 1 : edit\u00a0\/etc\/mysql\/my.cnf file.\u00a0<\/strong><\/p>\n<p>#skip-networking<\/p>\n<p>#bind-address\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= 127.0.0.1<\/p>\n<p>(add below line in \/etc\/mysql\/my.cnf file)<\/p>\n<p>server-id\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= 1<\/p>\n<p>log_bin\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= \/var\/log\/mysql\/mysql-bin.log<\/p>\n<p>binlog_do_db\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0= replicationdb<\/p>\n<p><strong>step 2 : \u00a0Restart Mysql server<\/strong><\/p>\n<p>#\/etc\/init.d\/mysql restart<\/p>\n<p><strong>Step 3 : create a user with replication privileges:<\/strong><\/p>\n<p>#mysql -u root -p<\/p>\n<p>mysql&gt; GRANT REPLICATION SLAVE ON *.* TO \u2019replicationuser\u2019@\u2019%\u2019 IDENTIFIED BY \u2018&lt;some_password&gt;\u2019;<\/p>\n<p>mysql&gt;FLUSH PRIVILEGES;<\/p>\n<p>mysql&gt;USE replicationdb;<\/p>\n<p>mysql&gt;FLUSH TABLES WITH READ LOCK;<\/p>\n<p>mysql&gt;SHOW MASTER STATUS;<\/p>\n<p>Result of above command:<\/p>\n<p>+\u2014\u2014\u2014\u2014\u2014+\u2014\u2014\u2014-+\u2014\u2014\u2014\u2014\u2013+\u2014\u2014\u2014\u2014\u2014\u2014+<\/p>\n<p>| File\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0| Position | Binlog_do_db | Binlog_ignore_db |<\/p>\n<p>+\u2014\u2014\u2014\u2014\u2014+\u2014\u2014\u2014-+\u2014\u2014\u2014\u2014\u2013+\u2014\u2014\u2014\u2014\u2014\u2014+<\/p>\n<p>| mysql-bin.005 | 180\u00a0\u00a0 \u00a0\u00a0\u00a0| replicationdb\u00a0\u00a0\u00a0\u00a0|\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0|<\/p>\n<p>+\u2014\u2014\u2014\u2014\u2014+\u2014\u2014\u2014-+\u2014\u2014\u2014\u2014\u2013+\u2014\u2014\u2014\u2014\u2014\u2014+<\/p>\n<p>1 row in set (0.00 sec)<\/p>\n<p>Please remeber above information will need in slave server configuration<\/p>\n<p>mysql&gt;quit;<\/p>\n<p><strong>Step 4 : Dump replicationdb from the Master server<\/strong><\/p>\n<p>#mysqldump -u root -p&lt;password&gt; replicationdb &gt; replicationdb.sql<\/p>\n<p>Above command create \u00a0dump of replicationdb\u00a0in the file replication.sql. Transfer this file to your slave server!<\/p>\n<p><strong>Step \u00a05 : Unlock the tables<\/strong><\/p>\n<p>#mysql -u root -p<\/p>\n<p>mysql&gt;UNLOCK TABLES;<\/p>\n<p>mysql&gt;quit;<\/p>\n<p>&nbsp;<\/p>\n<p><strong><em>Configure The Slave Server<\/em><\/strong><\/p>\n<p><strong>Step 1 : Create the database replicationdb<\/strong><\/p>\n<p>#mysql -u root -p<br \/>\nmysql&gt;CREATE DATABASE replicationdb;<br \/>\nmysql&gt;quit;<\/p>\n<p><strong>Step 2 : Extract the dump database in replicationdb database<\/strong><\/p>\n<p>#mysql -u root -p&lt;password&gt; replicationdb &lt; \/path\/to\/replicationdb.sql<\/p>\n<p>Step 3 : Add the below lines into\u00a0\/etc\/mysql\/my.cnf<\/p>\n<p>server-id=2<\/p>\n<p>master-host=192.168.10.175<\/p>\n<p>master-user=replicationuser<\/p>\n<p>master-password=secret<\/p>\n<p>replicate-do-db=replicationdb<\/p>\n<p><strong>Step 3 : Restart MySQL:<\/strong><\/p>\n<p># \/etc\/init.d\/mysql restart<\/p>\n<div><strong>Step 4 : \u00a0Allow slave user to connet to Remote Master server<\/strong><\/div>\n<div># mysql -u root -p<\/div>\n<p>mysql&gt;SLAVE STOP;<\/p>\n<p>mysql&gt;CHANGE MASTER TO MASTER_HOST=\u2019192.168.10.175?, MASTER_USER=\u2019replicationuser\u2019, MASTER_PASSWORD=\u2019&lt;some_password&gt;\u2019, MASTER_LOG_FILE=\u2019mysql-bin.005?, MASTER_LOG_POS=180;<\/p>\n<p>mysql&gt;START SLAVE;<\/p>\n<p>mysql&gt;quit;<\/p>\n<p>Configuration is Complete now! \u00a0So whenever replicationdb\u00a0is updated on the master server, all changes will be replicated to replicationdb\u00a0on the slave server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Database Replication In MySQL <\/p>\n<p>&nbsp;<\/p>\n<p>Configure the MySQL Master Server<\/p>\n<p>Step 1 : edit \/etc\/mysql\/my.cnf file. <\/p>\n<p>#skip-networking<\/p>\n<p>#bind-address = 127.0.0.1<\/p>\n<p>(add below line in \/etc\/mysql\/my.cnf file)<\/p>\n<p>server-id = 1<\/p>\n<p>log_bin = \/var\/log\/mysql\/mysql-bin.log<\/p>\n<p>binlog_do_db = replicationdb<\/p>\n<p>step 2 : Restart Mysql server<\/p>\n<p>#\/etc\/init.d\/mysql restart<\/p>\n<p>Step 3 : create a user with replication privileges:<\/p>\n<p>#mysql -u [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2165"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2165"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2165\/revisions"}],"predecessor-version":[{"id":2166,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2165\/revisions\/2166"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}