{"id":1466,"date":"2018-01-19T19:22:52","date_gmt":"2018-01-19T19:22:52","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/using-mysqls-built-in-replication\/"},"modified":"2018-01-19T19:24:48","modified_gmt":"2018-01-19T19:24:48","slug":"using-mysqls-built-in-replication","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/using-mysqls-built-in-replication\/","title":{"rendered":"Using MySQL&#8217;s Built-In Replication"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Michael Tanoviceanu<\/div>\n<div class=\"\">on September 12, 2000<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/phpbuilder.com\/wp-content\/uploads\/2018\/01\/mtanoviceanu.jpg\" alt=\"picture of Michael Tanoviceanu\" class=\"articleAuthorImg\" height=\"127\" width=\"95\" align=\"left\"\/><\/p>\n<div class=\"articlePara\">\nHere at <a href=\"http:\/\/www.soundbreak.com\/\" target=\"_blank\">Soundbreak<\/a> we stream out live audio<br \/>\nand video 24 hours a day, so it didn&#8217;t take much convincing to be able to run some<br \/>\ntesting on MySQL&#8217;s new replication feature.  We discoverd that we could use it to<br \/>\nkeep an up to date standby database server that could handle all of the queries<br \/>\nshould the primary fail for any reason.  It&#8217;s not dificult to configure two servers<br \/>\nfor just such a purpose, and I&#8217;ll walk through the procedure, as well as a discussion<br \/>\nas to how you can use PHP to redirect the queries should the primary server fail.<\/div>\n<div class=\"articlePara\">\nMySQL&#8217;s internal replication is built on a master-slave relationship between two or<br \/>\nmore servers, with one acting as the master, and any number acting as slaves.  I&#8217;ll<br \/>\nwalk through configuring two servers as a master slave pair, describing the process<br \/>\nas we move through it.  I initially performed the procedure outlined below on 3.23.22,<br \/>\nand have also tested it on 3.23.23.  The MySQL developers recommend that you use the<br \/>\nmost recent version, and that both the master and slave use the same version while<br \/>\nthe 3.23 series is still in beta as versions may not be backward compatible.  I<br \/>\ncurrently have not yet used this procedure on a live site for that reason, as one of<br \/>\nthe advantages of having fail-over capabilities is to be able to upgrade one server<br \/>\nwithout interrupting any of the queries.<\/div>\n<h2>Step One: Configure The Master<\/h2>\n<div class=\"articlePara\">\nFor the remainder of the article, I&#8217;ll refer to two servers, A (10.1.1.1), the primary<br \/>\nserver, and B (10.1.1.2), the standby server.<\/div>\n<div class=\"articlePara\">\nMySQL&#8217;s replication is done by having the slave server (B) connect to the master (A)<br \/>\nand read the binary update log, incorporating those changes into its own databases.<br \/>\nThe slave needs a user account to connect to the master, so on the master (A) create<br \/>\nan account with only the FILE privilege with the following:\n<\/div>\n<div class=\"example\">\n<pre>\nGRANT FILE ON *.* TO replicate@10.1.1.2 IDENTIFIED BY 'password';\n<\/pre>\n<\/div>\n<div class=\"articlePara\">\nDon&#8217;t worry about running &#8216;FLUSH PRIVILEGES&#8217; on the master to ensure that the slave can connect,<br \/>\nas we&#8217;ll be stopping the server in the next step.\n<\/div>\n<div class=\"articlePara\">\nNow we need a snapshot of the codesent data, and to configure the master to start generating binary update logs.<br \/>\nFirst edit the &#8216;my.cnf&#8217; file to enable the binary update log, so somewhere under [mysqld] portion add the line:<br \/>\n&#8216;log-bin&#8217;.  Now the next time the server starts, we&#8217;ll be generating the binary update log (named <hostname>-bin.<br \/>\n<rotation>).  When you shut down the MySQL server to enable the binary update logging, copy all of the database<br \/>\ndirectories of the master to another directory, then restart mysqld  Be sure to get all of the databases, or<br \/>\nyou could end up with errors when replicating if a table or database exists on the master but not on the slave.<br \/>\nNow you have a snapshot of the data, as well as a binary log of any updates since the snapshot.  Please note<br \/>\nthat the MySQL data files (the *.MYD, *.MYI, and *.frm) are file system dependent, so you can&#8217;t transfer files<br \/>\nfrom Solaris to Linux.  If you are in a heterogeneous server environment you&#8217;ll have to use mysqldump or other<br \/>\ncustom script to get your data snapshot.<br \/>\n<\/rotation><\/hostname><\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"tanoviceanu200009124658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"tanoviceanu200009129ba9.html?page=3\">3<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"tanoviceanu20000912fdb0.html?page=4\">4<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"tanoviceanu200009124658.html?page=2\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>By Michael Tanoviceanu on September 12, 2000 Here at Soundbreak we stream out live audio and video 24 hours a day, so it didn&#8217;t take much convincing to be able to run some testing on MySQL&#8217;s new replication feature. We discoverd that we could use it to keep an up&#8230; <a href=\"https:\/\/phpbuilder.com\/using-mysqls-built-in-replication\/\" class=\"readmore\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1466","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1466"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1466\/revisions"}],"predecessor-version":[{"id":2245,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1466\/revisions\/2245"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}