<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>IScale &#187; database</title>
	<atom:link href="http://dotimes.com/iscale/category/database/feed" rel="self" type="application/rss+xml" />
	<link>http://dotimes.com/iscale</link>
	<description>Living within Dot Times</description>
	<pubDate>Tue, 18 Nov 2008 06:04:39 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<item>
		<title>Configuration of MySQL Replication</title>
		<link>http://dotimes.com/iscale/2007/12/configuration-of-mysql-replication.html</link>
		<comments>http://dotimes.com/iscale/2007/12/configuration-of-mysql-replication.html#comments</comments>
		<pubDate>Mon, 31 Dec 2007 09:25:33 +0000</pubDate>
		<dc:creator>Cherife Li</dc:creator>
		
		<category><![CDATA[database]]></category>

		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://dotimes.com/iscale/2007/12/configuration-of-mysql-replication.html</guid>
		<description><![CDATA[The Replication Process
Before this how to set up replication, take a quickly look at the steps that MySQL goes through to maintain a replicated server. The process is different depending on the version of MySQL. For purposes of this article, my comments will be for version 4.0 or higher, since most systems now are using [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The Replication Process</strong></p>
<p>Before this how to set up replication, take a quickly look at the steps that MySQL goes through to maintain a replicated server. The process is different depending on the version of MySQL. For purposes of this article, my comments will be for version 4.0 or higher, since most systems now are using the later versions.</p>
<p>When replication is running, basically, as SQL statements are executed on the master server, MySQL records them in a binary log (bin.log) along with a log position identification number. The slave server in turn, through an IO thread, regularly and very often reads the master&#8217;s binary log for any changes. If it finds a change, it copies the new statements to its relay log (relay.log). It then records the new position identification number in a file (master.info) on the slave server. The slave then goes back to checking the master binary log, using the same IO thread. When the slave server detects a change to its relay log, through an SQL thread the slave executes the new SQL statement recorded in the relay log. As a safeguard, the slave also queries the master server through the SQL thread to compare its data with the master&#8217;s data. If the comparison shows inconsistency, the replication process is stopped and an error message is recorded in the slave&#8217;s error log (error.log). If the results of the query match, the new log position identification number is recorded in a file on the slave (relay-log.info) and the slave waits for another change to the relay log file.</p>
<p>This process may seem involved and complicated at first glance, but it all occurs quickly, it isn&#8217;t a significant drain on the master server, and it ensures reliable replication. Also, it&#8217;s surprisingly easy to set up. It only requires a few lines of options to be added to the configuration file (i.e., my.cnf) on the master and slave servers.</p>
<p><strong>How to Set Up Replication</strong></p>
<p>There are a number of different methods for setting up replication, and the exact method that you use will depend on how you are setting up replication, and whether you already have data within your master database.</p>
<p><strong>I. Creating a User for Replication</strong></p>
<p>Each Slave must connect to the Master using a standard username and password. It&#8217;s best not to use an existing account for security reasons, for the username and password will be stored in plain text within the <em>master.info</em> file. To do this, enter an SQL statement like the following on the master server, logged in as root or a user that has <em>GRANT OPTION</em> privileges:</p>
<pre>
mysql> GRANT REPLICATION SLAVE ON *.*
       -> TO 'replic'@'192.168.0.6' IDENTIFIED BY 'passwdhere';
</pre>
<p>
In this SQL statement, the user account <em>replic</em> is granted only what&#8217;s needed for replication. The user name can be almost anything. The IP address(or host name) <em>192.168.0.6</em> is given in quotes which is allowed to connect for replication. You should enter this same statement on the slave server with the same user name and password, but with the master&#8217;s host name or IP address. This way, if the master fails and will be down for a while, you could redirect users to the slave with DNS or by some other method. When the master is back up, you can then use replication to get it up to date by temporarily making it a slave to the former slave server. Incidentally, if you upgraded MySQL to version 4.0 recently, but didn&#8217;t upgrade your mysql database, the GRANT statement above won&#8217;t work because these privileges didn&#8217;t exist in the earlier versions.
</p>
<p><strong>II. Configuring Replication Master</strong></p>
<p>For replication to work you <em>must</em> enable binary logging on the master, for it is the binary log that is used to exchange data between the master and slaves.</p>
<p>Each server within a replication group must have a unique <em>server-id</em>. The server-id is used to identify individual servers within the group. You will need to add the following options to the configuration file <em>my.cnf</em> within the <em>[mysqld]</em> section. For example, to enable binary logging, using a log filename prefix of mysql-bin, and setting a server ID of 1:</p>
<pre>
[mysqld]
log-bin=mysql-bin
server-id=1
</pre>
<p>
Ensure that the skip-networking option has not been enabled on your replication master. If networking has been disabled, then your slave will not able to communicate with the master and replication will fail.</p>
<p><strong>III. Configuring Replication Slave</strong></p>
<p>
The only option you must configure on the slave is to set the unique server ID. Ensure that the slave server has the followling effective option:</p>
<pre>
[mysqld]
server-id=2
</pre>
<p>
If you are setting up multiple slaves, each one must have a unique <em>server-id</em> value that differs from that of the master and from each of the other slaves. You do not have to enable binary logging on the slave for replication to be enabled. However, if you enable binary logging on the slave then you can use the binary log for data backups and crash recovery on the slave, and also use the slave as part of a more complex replication topology.
</p>
<p><strong>IV. Obtaining the Master Replication Information</strong></p>
<p>
To configure replication on the slave you must determine the masters current point within the master binary log. You will need this information so that when the slave starts the replication process, it is able to start processing events from the binary log at the correct point.</p>
<p>If you have existing data on your master that you want to synchronize on your slaves before starting the replication process, then you must stop processing statements on the master, obtain the current position, and then dump the data, before allowing the master to continue executing statements. If you do not stop the execution of statements then the data dump, the master status information that you use will not match and you will end up with inconsistent or corrupted databases on the slaves.
</p>
<p>Start the command line client and flush all tables and block write statements by executing the FLUSH TABLES WITH READ LOCK statement:</p>
<pre>
mysql> FLUSH TABLES WITH READ LOCK;
</pre>
<p>Use the SHOW MASTER STATUS statement to determine the current binary log name and offset on the master:</p>
<pre>
mysql > SHOW MASTER STATUS;
+-----------------+----------+-----------------+---------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-----------------+----------+-----------------+---------------------+
|mysql-bin.0006| 98         |                      |                          |
+-----------------+----------+-----------------+---------------------+
</pre>
<p>
The File column shows the name of the log and Position shows the offset within the file. In this example, the binary log file is mysql-bin.006 and the offset is 98. Record these values. You need them later when you are setting up the slave.
</p>
<p>Keep the databases tables with read lock and follow the next step.</p>
<p><strong>V. Creating a Data Snapshot of Master</strong></p>
<p>There are several different ways to take a databases snapshot. From my experience, I prefer <em>mysqlhotcopy</em>(only for backing up <em>MyISAM</em> tables). For example, on master:</p>
<pre>
shell> mysqlhotcopy --allowold --noindices -u root -p passwdhere database-names /path-to-destdir
</pre>
<p>
When choosing databases to include in the copy, remember that you will need to filter out databases on each slave that you do not want to include in the replication process. Once you have created the archive or copy of the database, you will need to copy the files to each slave before starting the slave replication process. Now you could free the read lock on master:</p>
<pre>
mysql> UNLOCK TABLES;
</pre>
<p><strong>VI. Making Replication Process Running</strong></p>
<p>Importing the databases into slave, then starting the slave, skipping replication by using the <em>&#8211;skip-slave</em> option.</p>
<p>
To set up the slave to communicate with the master for replication, you must tell the slave the necessary connection information. To do this, execute the following statement on the slave, replacing the option values with the actual values relevant to your system:</p>
<pre>
mysql> CHANGE MASTER TO
       -> MASTER_HOST='master_host_name',
       -> MASTER_USER='replication_user_name',
       -> MASTER_PASSWORD='replication_password',
       -> MASTER_LOG_FILE='recorded_log_file_name',
       -> MASTER_LOG_POS=recorded_log_position;
</pre>
<p>After that, start the replication threads:</p>
<pre>
mysql> START SLAVE;
</pre>
<p>After you have performed this procedure, the slave should connect to the master and catch up on any updates that have occurred since the snapshot was taken.</p>
<p>There are more replication startup options and variables, check the manual for details.</p>
<p><strong>VII. Checking Replication Status</strong></p>
<p>
It&#8217;s required that you ensure that replication is taking place and that there have been no errors between the slave and the master. One command for this is SHOW SLAVE STATUS which you must execute on each slave:</p>
<pre>
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
                  Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.0.16
                     Master_User: replic
                      Master_Port: 3306
                  Connect_Retry: 60
                 Master_Log_File: mysql-bin.000024
         Read_Master_Log_Pos: 4279
                   Relay_Log_File: slave1-relay-bin.000056
                   Relay_Log_Pos: 950
         Relay_Master_Log_File: mysql-bin.000024
               Slave_IO_Running: Yes
             Slave_SQL_Running: Yes
                 Replicate_Do_DB:
            Replicate_Ignore_DB:
             Replicate_Do_Table:
       Replicate_Ignore_Table:
     Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
                           Last_Errno: 0
                            Last_Error:
                       Skip_Counter: 0
           Exec_Master_Log_Pos: 4279
                 Relay_Log_Space: 4418
                     Until_Condition: None
                      Until_Log_File:
                      Until_Log_Pos: 0
            Master_SSL_Allowed: No
            Master_SSL_CA_File:
           Master_SSL_CA_Path:
                 Master_SSL_Cert:
              Master_SSL_Cipher:
                  Master_SSL_Key:
       Seconds_Behind_Master: 0
1 row in set (0.01 sec)
</pre>
<p>The key fields from the status report to examine are:</p>
<blockquote><p>Slave_IO_State &#8212; indicates the current status of the slave.<br />
Slave_IO_Running &#8212; shows whether the IO thread for the reading the master&#8217;s binary log is running.<br />
Slave_SQL_Running &#8212; shows whether the SQL thread for the executing events in the relay log is running.<br />
Last_Error &#8212; shows the last error registered when processing the relay log. Ideally this should be blank, indicating no errors.<br />
Seconds_Behind_Master &#8212; shows the number of seconds that the slave SQL thread is behind processing the master binary log.<br />
    A high number (or an increasing one) can indicate that the slave is unable to cope with the large number of queries from the master.</p></blockquote>
<p>On the master, you can check the status of slaves by examining the list of running processes. Slaves execute the Binlog Dump command:</p>
<pre>
*************************** 1. row ***************************
     Id: 112
   User: syn
   Host: 192.168.0.16:56644
     db: NULL
Command: Binlog Dump
   Time: 71268
  State: Has sent all binlog to slave; waiting for binlog to be updated
   Info: NULL
*************************** 2. row ***************************
     Id: 112935
   User: root
   Host: localhost
     db: NULL
Command: Query
   Time: 0
  State: NULL
   Info: SHOW PROCESSLIST
2 rows in set (0.00 sec)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://dotimes.com/iscale/2007/12/configuration-of-mysql-replication.html/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
