<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>My Notes</title>
    <link rel="alternate" type="text/html" href="http://www.chiramattel.com/sherry/blog/" />
    <link rel="self" type="application/atom+xml" href="http://www.chiramattel.com/sherry/blog/atom.xml" />
    <id>tag:www.chiramattel.com,2009-02-24:/sherry/blog/8</id>
    <updated>2009-02-24T16:42:20Z</updated>
    <subtitle>A place for me to consolidate various tips and tricks that I  find useful..</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 4.23-en</generator>

<entry>
    <title>MySQL replication</title>
    <link rel="alternate" type="text/html" href="http://www.chiramattel.com/sherry/blog/2009/02/mysql-replication.html" />
    <id>tag:www.chiramattel.com,2009:/sherry/blog//8.217</id>

    <published>2009-02-24T11:16:17Z</published>
    <updated>2009-02-24T16:42:20Z</updated>

    <summary>I have setup replication in MySQL many times. There are a lot of tutorials and how-tos on this subject on the internet. The best place for such a reference is the MySQL site itself. Here is a summary of this....</summary>
    <author>
        <name>Sherry C K</name>
        <uri>www.chiramattel.com/sherry</uri>
    </author>
    
        <category term="MySQL" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.chiramattel.com/sherry/blog/">
        <![CDATA[<p class="MsoNormal" style="margin: 5pt 0in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">I have setup replication in MySQL many times. There are a lot of tutorials and how-tos on this subject on the internet. The best place for such a reference is the MySQL site itself. </span><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><a href="editor-content.html?cs=utf-8#details">Here</a></span><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"> is a summary of this.<o:p></o:p></span></p>
<span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">Now,
everything is fine; until one of your databases crash. To restart
replication, one needs to clear all logs from the slave server, take a
valid snapshot and follow the steps as described above. <o:p></o:p></span>
<p class="MsoNormal" style="margin: 5pt 0in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">It
becomes tricky to create a valid snapshot especially if your database
server is running a mission critical application and cannot be shutdown
or can sustain a read lock for long durations. In this scenario, you
can use the --single-transaction option in mysqldump<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 5pt 0in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><i><span style="font-size: 10pt; font-family: 'Tahoma','sans-serif';"><font color="#000000">mysqldump --all-databases --master-data <strong>--single-transaction </strong>&gt; dbdump.db<o:p></o:p></font></span></i></p>
<span style="font-size: 10pt; font-family: 'Tahoma','sans-serif';"><font color="#000000">It
is to be noted that this is possible if you are using transactional
tables such as InnoDB. This option will dump a consistent state of the
DB without blocking any application. The option --master-data ensures
that when this dump is applied to the slave, it will set the slave to
the correct master position (as per the "show master status" command).<br /><br /></font></span><span style="font-size: 10pt; font-family: 'Tahoma','sans-serif';"></span><span style="font-size: 10pt; font-family: 'Tahoma','sans-serif';"></span><p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><a href="editor-content.html?cs=utf-8" name="details"><br /></a></p><p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><a href="editor-content.html?cs=utf-8" name="details"><br /></a></p><p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><a href="editor-content.html?cs=utf-8" name="details"> Steps involved in setting up MySQL replication</a><br /><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 5pt 0in 5pt 0.25in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">1.<span style="">&nbsp;&nbsp; </span>Create a User for Replication <o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 5pt 0in 5pt 0.25in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Logon to the master instance<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 5pt 1in; line-height: normal;"><i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">mysql -p -u root -h &lt;IP address of master server&gt;</span></i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 5pt -1in 5pt 0.5in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">Create a user with proper privileges on the master<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 1in; line-height: normal;"><i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">mysql&gt; grant&nbsp;REPLICATION SLAVE on &nbsp;*.*&nbsp;to repluser@&lt;IP Address of Slave&gt;&nbsp; identified by<span style="">&nbsp; </span>'&lt;password&gt;';<o:p></o:p></span></i></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 1in; line-height: normal;"><i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><o:p>&nbsp;</o:p></span></i></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 1in; line-height: normal;"><i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">mysql&gt; flush privileges;<o:p></o:p></span></i></p>
<p class="MsoNormal" style="margin: 5pt 0in 5pt 0.25in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">2.<span style="">&nbsp;&nbsp; </span>Setting the Replication Master Configuration <o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 1in; line-height: normal;"><i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">&nbsp;vi /etc/my.cnf<o:p></o:p></span></i></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 36.25pt; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 36.25pt; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">Under section [mysqld] add the following lines<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 1in; line-height: normal;"><i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">log-bin=mysql-bin<br />server-id=1</span></i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 5pt 0in 5pt 0.25in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">4.<span style="">&nbsp;&nbsp; </span>Setting the Replication Slave Configuration <o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 1in; line-height: normal;"><i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">&nbsp;vi /etc/my.cnf<o:p></o:p></span></i></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 36.25pt; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 36.25pt; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">Under section [mysqld] add the following lines<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 1in; line-height: normal;"><i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">log-bin=mysql-bin<br />server-id=2<span style="">&nbsp;&nbsp;&nbsp;&nbsp; </span><o:p></o:p></span></i></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 1in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 1in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">Note that this is a different ID from that of the master<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 5pt 0in 5pt 0.25in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">5.<span style="">&nbsp;&nbsp; </span>Creating a Data Snapshot Using mysqldump <o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 5pt 0in 5pt 0.25in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Logon to the master instance<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 5pt 0in 5pt 0.25in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><i>mysql -p -u root -h &lt;IP address of master server&gt;</i><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 1in; line-height: normal;"><i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">mysql&gt; flush tables with read lock;<o:p></o:p></span></i></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 1in; line-height: normal;"><i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><o:p>&nbsp;</o:p></span></i></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt 1in; text-indent: -0.5in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">While this session is active open another shell and run mysqldump<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 5pt 0in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><i><span style="font-size: 10pt; font-family: 'Tahoma','sans-serif';"><font color="#000000">mysqldump --all-databases --master-data &gt;dbdump.db<o:p></o:p></font></span></i></p>
<p class="MsoNormal" style="margin: 5pt 0in; line-height: normal;"><font color="#000000"><i><span style="font-size: 10pt; font-family: 'Tahoma','sans-serif';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></i><span style="font-size: 10pt; font-family: 'Tahoma','sans-serif';">Use of the --master-data parameter is important here.<o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 5pt 0in; line-height: normal;"><span style="font-size: 10pt; font-family: 'Tahoma','sans-serif';"><font color="#000000"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>After the dump is completed, exit from the master instance to release the read lock.<i><o:p></o:p></i></font></span></p>
<p class="MsoNormal" style="margin: 5pt 0in; line-height: normal;"><i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><br /></span></i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">6.<span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Setting Up Replication with New Master and Slaves <o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 5pt 0in; text-indent: 0.5in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">Apply the dump obtained on step 5 to the slave server<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 5pt 0in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><i>mysql -p -u root -h &lt;IP address of slave server&gt; &lt; dbdump.db<o:p></o:p></i></span></p>
<p class="MsoNormal" style="margin: 5pt 0in; line-height: normal;"><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Logon to the slave instance<span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 5pt 0in 5pt 0.25in; text-indent: -0.25in; line-height: normal;"><i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>mysql -p -u root -h &lt;IP address of slave server&gt; </span></i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 5pt 0in 5pt 1in; line-height: normal;"><i><span style="font-size: 10pt; color: rgb(51, 51, 51); font-family: 'Tahoma','sans-serif';">mysql&gt; change master to MASTER_HOST=&lt;IP address of master server&gt;, MASTER_USER='repluser', MASTER_PASSWORD='&lt;password&gt;';<o:p></o:p></span></i></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-size: 10pt; font-family: 'Tahoma','sans-serif';"><font color="#000000"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Master user and the master password are from step 1.<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-size: 10pt; font-family: 'Tahoma','sans-serif';"><font color="#000000">This should start the replication on the slave server. For more details, you could refer to <o:p></o:p></font></span></p>

<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-size: 10pt; font-family: 'Tahoma','sans-serif';"><o:p></o:p><font color="#000000"><a href="http://dev.mysql.com/doc/refman/5.1/en/replication-howto.html">http://dev.mysql.com/doc/refman/5.1/en/replication-howto.html<span style="color: rgb(51, 51, 51);"><o:p></o:p></span></a></font></span></p><br />]]>
        
    </content>
</entry>

</feed>
