<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Think API&#187; mysql replication</title>
	<atom:link href="http://thinkapi.com/blog/tag/mysql-replication/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinkapi.com/blog</link>
	<description>An interface to tech thoughts</description>
	<lastBuildDate>Mon, 10 May 2010 10:32:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Codeigniter: Separating reads and writes for scaling MySQL</title>
		<link>http://thinkapi.com/blog/codeigniter-separating-reads-and-writes-for-scaling-mysql/</link>
		<comments>http://thinkapi.com/blog/codeigniter-separating-reads-and-writes-for-scaling-mysql/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 09:41:12 +0000</pubDate>
		<dc:creator>Sukumar</dc:creator>
				<category><![CDATA[Codeigniter]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[database replication]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mysql replication]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://thinkapi.com/blog/?p=173</guid>
		<description><![CDATA[Generally websites average a ratio of 9:1 or more for reads:writes for their applications which makes MySQL replication as one of the ways to scale you web application. The simplest configuration is to separate reads and writes with all the reads coming from the slave servers. We can implement this in codeigniter using database groups. [...]
Related posts:<ol>
<li><a href='http://thinkapi.com/blog/making-codeigniter-clean-urls-cleaner/' rel='bookmark' title='Making Codeigniter clean URLs cleaner'>Making Codeigniter clean URLs cleaner</a> <small>Usually when working with codeigniter you come across the use...</small></li>
<li><a href='http://thinkapi.com/blog/how-to-skip-mysql-replication-counter/' rel='bookmark' title='How to skip MySQL replication counter'>How to skip MySQL replication counter</a> <small>There are such times when MySQL replication stops when you...</small></li>
<li><a href='http://thinkapi.com/blog/upgrading-to-mysql-51-ga-better-wait/' rel='bookmark' title='Upgrading to MySQL 5.1 GA? Better Wait'>Upgrading to MySQL 5.1 GA? Better Wait</a> <small>MySQL 5.1 General Availability has been released to the public....</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Generally websites average a ratio of 9:1 or more for reads:writes for their applications which makes MySQL replication as one of the ways to scale you web application. The simplest configuration is to separate reads and writes with all the reads coming from the slave servers.<br />
<span id="more-173"></span><br />
<div id="attachment_174" class="wp-caption aligncenter" style="width: 440px"><a href="http://www.teonator.net/2008/10/23/mysql-replication/"><img class="size-full wp-image-174" title="MySQL Database replication" src="http://thinkapi.com/blog/wp-content/uploads/2009/08/db_replication.jpg" alt="MySQL Database replication" width="430" height="450" /></a><p class="wp-caption-text">MySQL Database replication</p></div></p>
<p>We can implement this in <a href="http://codeigniter.com/" target="_blank">codeigniter</a> using database groups.<br />
Below is a sample execution.</p>
<p>Create two active groups &#8211; one for read and one for write.</p>
<p>File: system/application/config/database.php</p>
<pre class="brush: php; title: ; notranslate">
$active_group = &quot;write&quot;;
$active_record = TRUE;

$db['read']['hostname'] = &quot;localhost&quot;;
$db['read']['username'] = &quot;root&quot;;
$db['read']['password'] = &quot;&quot;;
$db['read']['database'] = &quot;read_database_name&quot;;
$db['read']['dbdriver'] = &quot;mysql&quot;;
$db['read']['dbprefix'] = &quot;&quot;;
$db['read']['pconnect'] = TRUE;
$db['read']['db_debug'] = FALSE;
$db['read']['cache_on'] = FALSE;
$db['read']['cachedir'] = &quot;&quot;;
$db['read']['char_set'] = &quot;utf8&quot;;
$db['read']['dbcollat'] = &quot;utf8_general_ci&quot;;

$db['write']['hostname'] = &quot;localhost&quot;;
$db['write']['username'] = &quot;root&quot;;
$db['write']['password'] = &quot;&quot;;
$db['write']['database'] = &quot;write_database_name&quot;;
$db['write']['dbdriver'] = &quot;mysql&quot;;
$db['write']['dbprefix'] = &quot;&quot;;
$db['write']['pconnect'] = TRUE;
$db['write']['db_debug'] = FALSE;
$db['write']['cache_on'] = FALSE;
$db['write']['cachedir'] = &quot;&quot;;
$db['write']['char_set'] = &quot;utf8&quot;;
$db['write']['dbcollat'] = &quot;utf8_general_ci&quot;;
</pre>
<p>Create separate database connections to access read and write databases separately where needed (constructor is generally a good place for this).</p>
<pre class="brush: php; title: ; notranslate">
$read_db = $this-&gt;load-&gt;database('read', TRUE);
$write_db = $this-&gt;load-&gt;database('write', TRUE);
</pre>
<p>You can now go about running queries in the usual way.</p>
<pre class="brush: php; title: ; notranslate">
/* For reads */
$query = $read_db-&gt;get('table_name');

foreach ($query-&gt;result() as $row)
{
    echo $row-&gt;title;
}

/* For writes */
$data = array(
               'title' =&gt; $title,
               'name' =&gt; $name,
               'date' =&gt; $date
            );

$write_db-&gt;insert('mytable', $data);
</pre>
<p>In case you want to try this on an application you are already running, you can leave the &#8220;default&#8221; connection group intact and create only the &#8220;read&#8221; connection group. Use it where you think you are running read heavy queries.</p>
<p>Related posts:<ol>
<li><a href='http://thinkapi.com/blog/making-codeigniter-clean-urls-cleaner/' rel='bookmark' title='Making Codeigniter clean URLs cleaner'>Making Codeigniter clean URLs cleaner</a> <small>Usually when working with codeigniter you come across the use...</small></li>
<li><a href='http://thinkapi.com/blog/how-to-skip-mysql-replication-counter/' rel='bookmark' title='How to skip MySQL replication counter'>How to skip MySQL replication counter</a> <small>There are such times when MySQL replication stops when you...</small></li>
<li><a href='http://thinkapi.com/blog/upgrading-to-mysql-51-ga-better-wait/' rel='bookmark' title='Upgrading to MySQL 5.1 GA? Better Wait'>Upgrading to MySQL 5.1 GA? Better Wait</a> <small>MySQL 5.1 General Availability has been released to the public....</small></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://thinkapi.com/blog/codeigniter-separating-reads-and-writes-for-scaling-mysql/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

