<?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>Ivan Villareal</title>
	<atom:link href="http://blog.ivanvillareal.info/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ivanvillareal.info</link>
	<description>IT stuff and more...</description>
	<lastBuildDate>Fri, 13 Aug 2010 23:05:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting first and last row from a group set without sub queries in mysql</title>
		<link>http://blog.ivanvillareal.info/development/mysql-development/getting-first-and-last-row-from-a-group-set-without-sub-queries-in-mysql/</link>
		<comments>http://blog.ivanvillareal.info/development/mysql-development/getting-first-and-last-row-from-a-group-set-without-sub-queries-in-mysql/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 01:45:24 +0000</pubDate>
		<dc:creator>Ivan Villareal</dc:creator>
				<category><![CDATA[Mysql]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[group]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.ivanvillareal.info/?p=206</guid>
		<description><![CDATA[How I use group_concat to get the last row from a GROUP_BY clause]]></description>
			<content:encoded><![CDATA[<p>When I use the GROUP BY clause in mysql It starts grouping by the first different row in the resultset so for example if I have this table:</p>
<p>&nbsp;</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="MySQL"><div class="devcodeoverflow">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">select</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">from</span> grouptest<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+----+---------+-------+------------+</span>
<span style="color: #CC0099;">|</span> id <span style="color: #CC0099;">|</span> city    <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">value</span> <span style="color: #CC0099;">|</span> <span style="color: #999900; font-weight: bold;">date</span>       <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+---------+-------+------------+</span>
<span style="color: #CC0099;">|</span>  1 <span style="color: #CC0099;">|</span> tijuana <span style="color: #CC0099;">|</span>    10 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>02 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>  2 <span style="color: #CC0099;">|</span> tijuana <span style="color: #CC0099;">|</span>    11 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>09 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>  3 <span style="color: #CC0099;">|</span> tijuana <span style="color: #CC0099;">|</span>    15 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>12 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>  4 <span style="color: #CC0099;">|</span> tijuana <span style="color: #CC0099;">|</span>    17 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>17 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>  5 <span style="color: #CC0099;">|</span> tecate  <span style="color: #CC0099;">|</span>    12 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>10 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>  6 <span style="color: #CC0099;">|</span> tecate  <span style="color: #CC0099;">|</span>     1 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>14 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>  7 <span style="color: #CC0099;">|</span> tecate  <span style="color: #CC0099;">|</span>    29 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>17 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+----+---------+-------+------------+</span>
7 rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span></div></pre><!--END_DEVFMTCODE--></p>
<p>&nbsp;</p>
<p>If I group by city this is what I get:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="MySQL"><div class="devcodeoverflow">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> grouptest g <span style="color: #990099; font-weight: bold;">GROUP BY</span> city<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+----+---------+-------+------------+</span>
<span style="color: #CC0099;">|</span> id <span style="color: #CC0099;">|</span> city    <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">value</span> <span style="color: #CC0099;">|</span> <span style="color: #999900; font-weight: bold;">date</span>       <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+---------+-------+------------+</span>
<span style="color: #CC0099;">|</span>  5 <span style="color: #CC0099;">|</span> tecate  <span style="color: #CC0099;">|</span>    12 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>10 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>  1 <span style="color: #CC0099;">|</span> tijuana <span style="color: #CC0099;">|</span>    10 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>02 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+----+---------+-------+------------+</span>
2 rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span></div></pre><!--END_DEVFMTCODE--></p>
<p>&nbsp;</p>
<p>But I want to get the last inserted rows, so could do a sub query like this:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="MySQL"><div class="devcodeoverflow">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> grouptest g
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #990099; font-weight: bold;">WHERE</span> g.<span style="color: #999900; font-weight: bold;">date</span> <span style="color: #CC0099;">=</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">MAX</span><span style="color: #FF00FF;">&#40;</span><span style="color: #999900; font-weight: bold;">date</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">FROM</span> grouptest gt <span style="color: #990099; font-weight: bold;">WHERE</span> g.city <span style="color: #CC0099;">=</span> gt.city<span style="color: #FF00FF;">&#41;</span>
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #990099; font-weight: bold;">GROUP BY</span> g.city<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+----+---------+-------+------------+</span>
<span style="color: #CC0099;">|</span> id <span style="color: #CC0099;">|</span> city    <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">value</span> <span style="color: #CC0099;">|</span> <span style="color: #999900; font-weight: bold;">date</span>       <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+---------+-------+------------+</span>
<span style="color: #CC0099;">|</span>  7 <span style="color: #CC0099;">|</span> tecate  <span style="color: #CC0099;">|</span>    29 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>17 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>  4 <span style="color: #CC0099;">|</span> tijuana <span style="color: #CC0099;">|</span>    17 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>17 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+----+---------+-------+------------+</span>
2 rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span></div></pre><!--END_DEVFMTCODE--></p>
<p>&nbsp;</p>
<p>But if the db is large making subqueries could be expensive, so a cleaner solution might be using the <a href="http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat" target="_blank">GROUP_CONCAT</a> function, so for example the above query could be written like this:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="MySQL"><div class="devcodeoverflow">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SELECT</span> id<span style="color: #000033;">,</span> city<span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">value</span><span style="color: #000033;">,</span> <span style="color: #999900; font-weight: bold;">date</span><span style="color: #000033;">,</span> <span style="color: #000099;">GROUP_CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #999900; font-weight: bold;">date</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> all_dates
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #990099; font-weight: bold;">FROM</span> grouptest g
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #990099; font-weight: bold;">GROUP BY</span> city<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+----+---------+-------+------------+---------------------------------------------+</span>
<span style="color: #CC0099;">|</span> id <span style="color: #CC0099;">|</span> city    <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">value</span> <span style="color: #CC0099;">|</span> <span style="color: #999900; font-weight: bold;">date</span>       <span style="color: #CC0099;">|</span> all_dates                                   <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+---------+-------+------------+---------------------------------------------+</span>
<span style="color: #CC0099;">|</span>  5 <span style="color: #CC0099;">|</span> tecate  <span style="color: #CC0099;">|</span>    12 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>10 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>10<span style="color: #000033;">,</span>2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>14<span style="color: #000033;">,</span>2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>17            <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>  1 <span style="color: #CC0099;">|</span> tijuana <span style="color: #CC0099;">|</span>    10 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>02 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>02<span style="color: #000033;">,</span>2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>09<span style="color: #000033;">,</span>2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>12<span style="color: #000033;">,</span>2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>17 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+----+---------+-------+------------+---------------------------------------------+</span>
2 rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span></div></pre><!--END_DEVFMTCODE--><br />
&nbsp;<br />
At this point I can explode the all_dates column in php or split them in perl to get the last date for each city, but a little more work on our query could do our job simpler, something like this for instance:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="MySQL"><div class="devcodeoverflow">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SELECT</span> id<span style="color: #000033;">,</span> city<span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">value</span><span style="color: #000033;">,</span> <span style="color: #000099;">SUBSTRING_INDEX</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">GROUP_CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #999900; font-weight: bold;">date</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #008000;">','</span><span style="color: #000033;">,</span><span style="color: #CC0099;">-</span>1<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #999900; font-weight: bold;">date</span>
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #990099; font-weight: bold;">FROM</span> grouptest g
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #990099; font-weight: bold;">GROUP BY</span> city<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+----+---------+-------+------------+</span>
<span style="color: #CC0099;">|</span> id <span style="color: #CC0099;">|</span> city    <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">value</span> <span style="color: #CC0099;">|</span> <span style="color: #999900; font-weight: bold;">date</span>       <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+---------+-------+------------+</span>
<span style="color: #CC0099;">|</span>  5 <span style="color: #CC0099;">|</span> tecate  <span style="color: #CC0099;">|</span>    12 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>17 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>  1 <span style="color: #CC0099;">|</span> tijuana <span style="color: #CC0099;">|</span>    10 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>17 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+----+---------+-------+------------+</span>
2 rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
</div></pre><!--END_DEVFMTCODE--><br />
&nbsp;<br />
Or if I need to get the first and last rows for each group set I just add the date column like this:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="MySQL"><div class="devcodeoverflow">mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">SELECT</span> id<span style="color: #000033;">,</span> city<span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">value</span><span style="color: #000033;">,</span> <span style="color: #999900; font-weight: bold;">date</span> <span style="color: #990099; font-weight: bold;">as</span> first_date<span style="color: #000033;">,</span> <span style="color: #000099;">SUBSTRING_INDEX</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">GROUP_CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #999900; font-weight: bold;">date</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #008000;">','</span><span style="color: #000033;">,</span><span style="color: #CC0099;">-</span>1<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> last_date
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #990099; font-weight: bold;">FROM</span> grouptest g
    <span style="color: #CC0099;">-&gt;</span> <span style="color: #990099; font-weight: bold;">GROUP BY</span> city<span style="color: #000033;">;</span>
<span style="color: #CC0099;">+----+---------+-------+------------+------------+</span>
<span style="color: #CC0099;">|</span> id <span style="color: #CC0099;">|</span> city    <span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">value</span> <span style="color: #CC0099;">|</span> first_date <span style="color: #CC0099;">|</span> last_date  <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+----+---------+-------+------------+------------+</span>
<span style="color: #CC0099;">|</span>  5 <span style="color: #CC0099;">|</span> tecate  <span style="color: #CC0099;">|</span>    12 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>10 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>17 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">|</span>  1 <span style="color: #CC0099;">|</span> tijuana <span style="color: #CC0099;">|</span>    10 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>02 <span style="color: #CC0099;">|</span> 2010<span style="color: #CC0099;">-</span>02<span style="color: #CC0099;">-</span>17 <span style="color: #CC0099;">|</span> 
<span style="color: #CC0099;">+----+---------+-------+------------+------------+</span>
2 rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
</div></pre><!--END_DEVFMTCODE--></p>
<p>&nbsp;</p>
<p>There may be several other ways around this, but for now this approach is enough, I’m not sure about the impact this 2 functions may have on the server but I’m sure it is less than executing a subquery.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ivanvillareal.info/development/mysql-development/getting-first-and-last-row-from-a-group-set-without-sub-queries-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Compressing/Decompressing mysql dumps on the fly</title>
		<link>http://blog.ivanvillareal.info/linux/compressingdecompressing-mysql-dumps-on-the-fly/</link>
		<comments>http://blog.ivanvillareal.info/linux/compressingdecompressing-mysql-dumps-on-the-fly/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 20:17:06 +0000</pubDate>
		<dc:creator>Ivan Villareal</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[pipes]]></category>
		<category><![CDATA[restore]]></category>

		<guid isPermaLink="false">http://blog.ivanvillareal.info/?p=197</guid>
		<description><![CDATA[One line commands I use to compress or decompress mysql dumps.]]></description>
			<content:encoded><![CDATA[<p>Using pipes and redirection in the shell we can do backups or restore them, performing compression or decompression on the fly, this are the commands I frequently use:</p>
<p>Create a backup <dbname>with mysqldump and compress the stream with bzip2<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow">ivan<span style="color: #000000; font-weight: bold;">@</span>mini:~$ mysqldump –h dbHost –u dbUser –pdbPass –add-drop-database –databases <span style="color: #000000; font-weight: bold;">&lt;</span>dbname<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">bzip2</span> <span style="color: #000000; font-weight: bold;">&gt;</span> dbName-02-<span style="color: #000000;">17</span>-2010.sql.bz2<span style="color: #000000; font-weight: bold;">&lt;/</span>dbname<span style="color: #000000; font-weight: bold;">&gt;</span></div></pre><!--END_DEVFMTCODE--></dbname></p>
<p>If I want to restore the backup I use:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow">ivan<span style="color: #000000; font-weight: bold;">@</span>mini:~$ <span style="color: #c20cb9; font-weight: bold;">bunzip2</span> <span style="color: #000000; font-weight: bold;">&lt;</span> dbName-02-17-2010.sql.bz2 <span style="color: #000000; font-weight: bold;">|</span> mysql –h dbHost –u dbUser –pdbPass</div></pre><!--END_DEVFMTCODE--></p>
<p>I can switch bzip2 compression by changing only the program name for example gzip/gunzip lzma/unlzma</code></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ivanvillareal.info/linux/compressingdecompressing-mysql-dumps-on-the-fly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sync mysql tables from one db to another with php</title>
		<link>http://blog.ivanvillareal.info/development/sync-mysql-tables-from-one-db-to-another-with-ph/</link>
		<comments>http://blog.ivanvillareal.info/development/sync-mysql-tables-from-one-db-to-another-with-ph/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 02:05:29 +0000</pubDate>
		<dc:creator>Ivan Villareal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.ivanvillareal.info/?p=192</guid>
		<description><![CDATA[How I did a php class to syncronize 2 tables on different mysql servers, the intention was to have a process that could be run from a cron job.]]></description>
			<content:encoded><![CDATA[<p>I needed a process to move a table from one database to another, the databases are on separate servers, so renaming the table will not work, I did a little research and found this <a href="http://code.google.com/p/ruby-roger-useful-functions/wiki/TableSyncer">Table Syncer</a> it seemed fine, the problem is that it was on ruby gem, and I said the problem because the server where this will be running don’t have this installed.</p>
<p>So instead of doing more research I spent that time developing my own php class to sync two tables on different server, I have little time to create this so I’m re-using a class I already did for handling several databases and it already has some useful methods, right now its 3:12pm I plan to finish this in 30 minutes lets see how it goes.</p>
<p>In the end I spent about an hour but I have a working file that can put in a cron job to sync the table on a regular basis.</p>
<p>I&#8217;ve changed the way I fetch the entire result, to avoid having problems exhausting the dedicated php memory with large arrays, instead I&#8217;m fetching one row at a time, another problem I had, was that the null values came blank so, when I was inserting on a unique index, I had an error, to avoid this I remove the empty values from the insert, and use the default table value instead, this results in a bit different tables Null instead of blanks, but because this isn&#8217;t important I didn&#8217;t want to spend more with this.</p>
<p>The final file, contains the db class and the sync class, and here it is:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow"><span style="color: #339933;">&lt;</span> ?php
&nbsp;
<span style="color: #009933; font-style: italic;">/**</span>
<span style="color: #009933; font-style: italic;">* The purpose of this file is to sync</span>
<span style="color: #009933; font-style: italic;">* tables between 2 databases</span>
<span style="color: #009933; font-style: italic;">*/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> tableSync <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_sourceDbUser</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ivan'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_sourceDbPass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'pass'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_sourceDbName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'bounty'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_sourceDbHost</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_targetDbUser</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ivan'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_targetDbPass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'pass'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_targetDbName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'testing'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_targetDbHost</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_sourceDb</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_targetDb</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_primaryKey</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_table</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$table</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_sourceDb <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_sourceDbUser<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_sourceDbPass<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_sourceDbName<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_sourceDbHost<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_targetDb <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Db<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_targetDbUser<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_targetDbPass<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_targetDbName<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_targetDbHost<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_table <span style="color: #339933;">=</span> <span style="color: #000088;">$table</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> begin<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//Check that the table exists</span>
        <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'SHOW TABLES'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$tbl</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_targetDb<span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAssoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_table<span style="color: #339933;">,</span> <span style="color: #990000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tbl</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">//create the table</span>
                <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW CREATE TABLE `&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_table<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;`&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$tbl</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_sourceDb<span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAssoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$createQry</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tbl</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Create Table'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_targetDb<span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$createQry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Get The primary Key</span>
        <span style="color: #000088;">$sql</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW indexes FROM &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_table<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; WHERE Key_name = 'PRIMARY'&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$indexes</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_sourceDb<span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAssoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_primaryKey <span style="color: #339933;">=</span> <span style="color: #000088;">$indexes</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Column_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//Query source  table</span>
        <span style="color: #000088;">$sql</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">'SELECT * FROM '</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_table<span style="color: #339933;">;</span>
        <span style="color: #000088;">$rowset</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_sourceDb<span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rowset</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">//remove empty fields</span>
            <span style="color: #000088;">$newRow</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$field</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$newRow</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_primaryKey <span style="color: #339933;">!=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Inserting row &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_primaryKey<span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_targetDb<span style="color: #339933;">-&gt;</span><span style="color: #004000;">insertUpdate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_table<span style="color: #339933;">,</span> <span style="color: #000088;">$newRow</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_primaryKey<span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_primaryKey<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_targetDb<span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryInsert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_table<span style="color: #339933;">,</span> <span style="color: #000088;">$newRow</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Db
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span>	<span style="color: #000088;">$_link</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_affectedRows</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_lastQueryStatus</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**</span>
<span style="color: #009933; font-style: italic;">	* Instantiate the object</span>
<span style="color: #009933; font-style: italic;">	**/</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_link <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_link<span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Could not select database'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Connect failed: <span style="color: #009933; font-weight: bold;">%s</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SET NAMES `utf8`&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_link<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SET CHARACTER SET 'utf8';&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_link<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_lastQueryStatus <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_link<span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Query failed: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt; SQL: '</span><span style="color: #339933;">.</span> <span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_lastQueryStatus<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;b&gt;MySQL Query fail:&lt;/b&gt; <span style="color: #006699; font-weight: bold;">$sql</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_affectedRows    <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">mysql_affected_rows</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_lastQueryStatus<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fetchAssoc<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$table</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$table</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> escape<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fetchField<span style="color: #009900;">&#40;</span><span style="color: #000088;">$table</span><span style="color: #339933;">,</span> <span style="color: #000088;">$field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$where</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'1'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT `<span style="color: #006699; font-weight: bold;">$field</span>` FROM `<span style="color: #006699; font-weight: bold;">$table</span>` WHERE <span style="color: #006699; font-weight: bold;">$where</span>&quot;</span><span style="color: #339933;">;</span>
	    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #000088;">$field</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$field</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insertUpdate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$table</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$uniqueValue</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	    <span style="color: #000088;">$where</span> <span style="color: #339933;">=</span> <span style="color: #990000;">is_int</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$uniqueValue</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$field</span> = <span style="color: #006699; font-weight: bold;">$uniqueValue</span>&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$field</span> = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">escape</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$uniqueValue</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
	    <span style="color: #000088;">$e</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchField</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$table</span><span style="color: #339933;">,</span> <span style="color: #000088;">$field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$where</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	        <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryUpdate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$table</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$where</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	        <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryInsert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$table</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span>
	    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$res</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> queryInsert<span style="color: #009900;">&#40;</span><span style="color: #000088;">$table</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$q</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;INSERT INTO `&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;` &quot;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$v</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$n</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$n</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;`<span style="color: #006699; font-weight: bold;">$key</span>`, &quot;</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'null'</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$v</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;NULL, &quot;</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'now()'</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$v</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;NOW(), &quot;</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'utc_timestamp()'</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$v</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;UTC_TIMESTAMP(), &quot;</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">else</span> <span style="color: #000088;">$v</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">escape</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;', &quot;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000088;">$q</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #339933;">.</span> <span style="color: #990000;">rtrim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">', '</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;) VALUES (&quot;</span><span style="color: #339933;">.</span> <span style="color: #990000;">rtrim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">', '</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;);&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_insert_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> queryUpdate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$table</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$where</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'1'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$q</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UPDATE `&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;` SET &quot;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'null'</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$q</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;`<span style="color: #006699; font-weight: bold;">$key</span>` = NULL, &quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'now()'</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$q</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;`<span style="color: #006699; font-weight: bold;">$key</span>` = NOW(), &quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">else</span> <span style="color: #000088;">$q</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;`<span style="color: #006699; font-weight: bold;">$key</span>`='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">escape</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;', &quot;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rtrim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">', '</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' WHERE '</span><span style="color: #339933;">.</span><span style="color: #000088;">$where</span><span style="color: #339933;">.</span><span style="color: #0000ff;">';'</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Instantiate the tableSync sending the table name I want to sync</span>
<span style="color: #000088;">$sync</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> tableSync<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tod_whois_raider'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
</div></pre><!--END_DEVFMTCODE--></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ivanvillareal.info/development/sync-mysql-tables-from-one-db-to-another-with-ph/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting up a new centos VPS server from console</title>
		<link>http://blog.ivanvillareal.info/linux/setting-up-a-new-centos-vps-server-from-console/</link>
		<comments>http://blog.ivanvillareal.info/linux/setting-up-a-new-centos-vps-server-from-console/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 00:36:30 +0000</pubDate>
		<dc:creator>Ivan Villareal</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[centos]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[vps]]></category>

		<guid isPermaLink="false">http://blog.ivanvillareal.info/?p=185</guid>
		<description><![CDATA[How I did an initial configuration of a Centos 5.4 VPS server to have it working with virtual hosts.]]></description>
			<content:encoded><![CDATA[<p>I just got a couple of vps servers, that I have to configure to run some apps, this are the tasks I did to have them ready for production use:</p>
<p>&nbsp;</p>
<p>This servers didn’t came with a control panel (better for me), so the first thing I did was to login:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow">ivan<span style="color: #000000; font-weight: bold;">@</span>mini:~$ <span style="color: #c20cb9; font-weight: bold;">ssh</span> root<span style="color: #000000; font-weight: bold;">@</span>23.45.12.56</div></pre><!--END_DEVFMTCODE--></p>
<p>After this I checked the OS, version and architecture</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># cat /etc/*release*</span>
CentOS release <span style="color: #000000;">5.4</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>Final<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># uname -a</span>
Linux V100205C4HB9V-<span style="color: #000000;">1</span> 2.6.18-028stab064.7 <span style="color: #666666; font-style: italic;">#1 SMP Wed Aug 26 13:11:07 MSD 2009 x86_64 x86_64 x86_64 GNU/Linux</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;">#</div></pre><!--END_DEVFMTCODE--></p>
<p>Ok, now that I know the OS I create a normal user to avoid using the root account.</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># useradd –Gwheel ivan</div></pre><!--END_DEVFMTCODE--></p>
<p>Then I change the server name to mygdon</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># sed -i 's/V100205C4HB9V-1/mygdon/g' /etc/sysconfig/network</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># sed -i 's/V100205C4HB9V-1/mygdon/g' /etc/hosts</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># echo HOST.DOMAIN.com &gt; /etc/hostname</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># hostname -F /etc/hostname</div></pre><!--END_DEVFMTCODE--></p>
<p>Unfortunately I was unable to persist the new hostname, because it is a VPS server, there are ways around this, but didn’t have the time to make the changes so I just moved on, and leave this for later</p>
<p>The next thing I did was update the OS, and add the rpmforge repo because I will need some apps from there</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># yum update</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># yum upgrade</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.x86_64.rpm</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># rpm -Uhv rpmforge-release-0.5.1-1.$dist.rf.$arch.rpm</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># yum update</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># yum upgrade</div></pre><!--END_DEVFMTCODE--></p>
<p>Then I installed some packages I often use:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># yum install htop screen vim-enhanced</div></pre><!--END_DEVFMTCODE--></p>
<p>The VPS already had a web stack installed I just did some configuration:</p>
<h2>Configuring Apache Virtual Hosts</h2>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># mkdir /var/www/vhosts/{site1, site2} –p</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># vi /etc/httpd/conf/httpd.conf</div></pre><!--END_DEVFMTCODE--></p>
<p>Here I Uncommented the following directive</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Apache configuration"><div class="devcodeoverflow"><span style="color: #00007f;">NameVirtualHost</span> *:<span style="color: #ff0000;">80</span></div></pre><!--END_DEVFMTCODE--></p>
<p>and Added a default vhost a new vhost</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Apache configuration"><div class="devcodeoverflow">&lt;<span style="color: #000000; font-weight:bold;">virtualhost</span> *:80&gt;
    <span style="color: #00007f;">DocumentRoot</span> /var/www/vhosts/default
    <span style="color: #00007f;">ServerName</span> mygdon.site1.net
    &lt;<span style="color: #000000; font-weight:bold;">directory</span> /var/www/vhosts/default&gt;
        <span style="color: #00007f;">Options</span> <span style="color: #0000ff;">Indexes</span> <span style="color: #0000ff;">FollowSymLinks</span>
        <span style="color: #00007f;">AllowOverride</span> <span style="color: #0000ff;">All</span>
        <span style="color: #00007f;">Order</span> <span style="color: #00007f;">allow</span>,<span style="color: #00007f;">deny</span>
        <span style="color: #00007f;">Allow</span> from <span style="color: #0000ff;">all</span>
    &lt;/<span style="color: #000000; font-weight:bold;">directory</span>&gt;
    <span style="color: #00007f;">ErrorLog</span> logs/mygdon.site1.net-error_log
    <span style="color: #00007f;">CustomLog</span> logs/mygdon.site1.net-access_log common
&lt;/<span style="color: #000000; font-weight:bold;">virtualhost</span>&gt;
&nbsp;
&lt;<span style="color: #000000; font-weight:bold;">virtualhost</span> *:80&gt;
    <span style="color: #00007f;">DocumentRoot</span> /var/www/vhosts/site2
    <span style="color: #00007f;">ServerName</span> appname.site2.net
    &lt;<span style="color: #000000; font-weight:bold;">directory</span> /var/www/vhosts/site2&gt;
        <span style="color: #00007f;">Options</span> <span style="color: #0000ff;">Indexes</span> <span style="color: #0000ff;">FollowSymLinks</span>
        <span style="color: #00007f;">AllowOverride</span> <span style="color: #0000ff;">All</span>
        <span style="color: #00007f;">Order</span> <span style="color: #00007f;">allow</span>,<span style="color: #00007f;">deny</span>
        <span style="color: #00007f;">Allow</span> from <span style="color: #0000ff;">all</span>
    &lt;/<span style="color: #000000; font-weight:bold;">directory</span>&gt;
    <span style="color: #00007f;">ErrorLog</span> logs/appname.site2.net-error_log
    <span style="color: #00007f;">CustomLog</span> logs/appname.site2.net-access_log common
&lt;/<span style="color: #000000; font-weight:bold;">virtualhost</span>&gt;</div></pre><!--END_DEVFMTCODE--></p>
<p>then restarted the apache server:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># service httpd restart</span>
Stopping httpd:                                            <span style="color: #7a0874; font-weight: bold;">&#91;</span>  OK  <span style="color: #7a0874; font-weight: bold;">&#93;</span>
Starting  httpd:                                            <span style="color: #7a0874; font-weight: bold;">&#91;</span>  OK  <span style="color: #7a0874; font-weight: bold;">&#93;</span></div></pre><!--END_DEVFMTCODE--></p>
<p>and&nbsp; I wrote a simple php file to test this out:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>V100205C4HB9V-<span style="color: #000000;">1</span> ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># echo '&amp;lt;? phpinfo(); ?&amp;gt;' &gt; /var/www/vhosts/site1/index.php</div></pre><!--END_DEVFMTCODE--></p>
<p>And because I haven’t configured a DNS server I just temporarily added the server to my hosts file (On my local machine):</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="PHP"><div class="devcodeoverflow">root<span style="color: #339933;">@</span>mini<span style="color: #339933;">:/</span>etc<span style="color: #666666; font-style: italic;"># echo 'subdomain.site1.net 54.65.74.23' &gt; /etc/hosts</div></pre><!--END_DEVFMTCODE--></p>
<p>and here is the result:</p>
<p><a href="http://blog.ivanvillareal.info/wp-content/uploads/2010/02/testingvhost.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="testing-vhost" src="http://blog.ivanvillareal.info/wp-content/uploads/2010/02/testingvhost_thumb.png" border="0" alt="testing-vhost" width="644" height="379" /></a>&nbsp;</p>
<h2>Configuring Mysql</h2>
<p>Once I had the virtual hosts configuration in place I added a mysql user and changed the default root password:</p>
<p>&nbsp;</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="MySQL"><div class="devcodeoverflow"><span style="color: #FF00FF;">&#91;</span>root@V100205C4HB9V<span style="color: #CC0099;">-</span><span style="color: #008080;">1</span> vhosts<span style="color: #FF00FF;">&#93;</span><span style="color: #808080; font-style: italic;"># mysql</span>
Welcome <span style="color: #990099; font-weight: bold;">to</span> the MySQL monitor.  Commands <span style="color: #009900;">end</span> <span style="color: #990099; font-weight: bold;">with</span> <span style="color: #000033;">;</span> <span style="color: #CC0099; font-weight: bold;">or</span> \g.
Your MySQL <span style="color: #FF9900; font-weight: bold;">connection</span> id <span style="color: #CC0099; font-weight: bold;">is</span> 5
Server <span style="color: #000099;">version</span>: 5.0.77 Source distribution
&nbsp;
<span style="color: #990099; font-weight: bold;">Type</span> <span style="color: #008000;">'help;'</span> <span style="color: #CC0099; font-weight: bold;">or</span> <span style="color: #008000;">'<span style="color: #004000; font-weight: bold;">\h</span>'</span> for <span style="color: #990099; font-weight: bold;">help</span>. <span style="color: #990099; font-weight: bold;">Type</span> <span style="color: #008000;">'<span style="color: #004000; font-weight: bold;">\c</span>'</span> <span style="color: #990099; font-weight: bold;">to</span> clear the <span style="color: #00CC00;">buffer</span>.
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">show</span> <span style="color: #990099; font-weight: bold;">databases</span><span style="color: #000033;">;</span>
<span style="color: #CC0099;">+--------------------+</span>
<span style="color: #CC0099;">|</span> <span style="color: #990099; font-weight: bold;">Database</span>           <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+--------------------+</span>
<span style="color: #CC0099;">|</span> information_schema <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">|</span> mysql              <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">|</span> test               <span style="color: #CC0099;">|</span>
<span style="color: #CC0099;">+--------------------+</span>
3 rows <span style="color: #990099; font-weight: bold;">in</span> <span style="color: #990099; font-weight: bold;">set</span> <span style="color: #FF00FF;">&#40;</span>0.00 sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">UPDATE</span> mysql.<span style="color: #000099;">user</span> <span style="color: #990099; font-weight: bold;">SET</span> <span style="color: #000099;">Password</span><span style="color: #CC0099;">=</span><span style="color: #000099;">PASSWORD</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'pass-here'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #000099;">user</span><span style="color: #CC0099;">=</span><span style="color: #008000;">'root'</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> 3 rows affected <span style="color: #FF00FF;">&#40;</span>0.02 sec<span style="color: #FF00FF;">&#41;</span>
Rows matched: 3  Changed: 3  <span style="color: #990099; font-weight: bold;">Warnings</span>: 0
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> FLUSH <span style="color: #990099; font-weight: bold;">PRIVILEGES</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> 0 rows affected <span style="color: #FF00FF;">&#40;</span>0.01 sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #000099;">USER</span> <span style="color: #008000;">'ivan'</span>@<span style="color: #008000;">'localhost'</span> IDENTIFIED BY <span style="color: #008000;">'pass-here'</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> 0 rows affected <span style="color: #FF00FF;">&#40;</span>0.01 sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">GRANT</span> <span style="color: #990099; font-weight: bold;">ALL</span> <span style="color: #990099; font-weight: bold;">PRIVILEGES</span> <span style="color: #990099; font-weight: bold;">ON</span> <span style="color: #CC0099;">*</span>.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">TO</span> <span style="color: #008000;">'ivan'</span>@<span style="color: #008000;">'localhost'</span> <span style="color: #990099; font-weight: bold;">WITH</span> <span style="color: #990099; font-weight: bold;">GRANT</span> <span style="color: #990099; font-weight: bold;">OPTION</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> 0 rows affected <span style="color: #FF00FF;">&#40;</span>0.00 sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #000099;">USER</span> <span style="color: #008000;">'ivan'</span>@<span style="color: #008000;">'<span style="color: #008080; font-weight: bold;">%</span>'</span> IDENTIFIED BY <span style="color: #008000;">'pass-here'</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> 0 rows affected <span style="color: #FF00FF;">&#40;</span>0.00 sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">GRANT</span> <span style="color: #990099; font-weight: bold;">ALL</span> <span style="color: #990099; font-weight: bold;">PRIVILEGES</span> <span style="color: #990099; font-weight: bold;">ON</span> <span style="color: #CC0099;">*</span>.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">TO</span> <span style="color: #008000;">'ivan'</span>@<span style="color: #008000;">'<span style="color: #008080; font-weight: bold;">%</span>'</span> <span style="color: #990099; font-weight: bold;">WITH</span> <span style="color: #990099; font-weight: bold;">GRANT</span> <span style="color: #990099; font-weight: bold;">OPTION</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> 0 rows affected <span style="color: #FF00FF;">&#40;</span>0.00 sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> FLUSH <span style="color: #990099; font-weight: bold;">PRIVILEGES</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span></div></pre><!--END_DEVFMTCODE--></p>
<p>&nbsp;</p>
<p>Now the server is&nbsp; ready for adding some virtual hosts, I just need to configure a DNS server and the mail server, but because the DNS is on another server and already working I just have to add a new zone, as for mail server, this server will be only used to send emails so I don’t need to dig in postfix configuration files for now.</p>
<p>I&#8217;ll write another post regarding the security, but for now this will work.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ivanvillareal.info/linux/setting-up-a-new-centos-vps-server-from-console/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Mysql5 and PHP5 on centos4</title>
		<link>http://blog.ivanvillareal.info/linux/mysql5-and-php5-on-centos4/</link>
		<comments>http://blog.ivanvillareal.info/linux/mysql5-and-php5-on-centos4/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 19:00:06 +0000</pubDate>
		<dc:creator>Ivan Villareal</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[centos]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[installation]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.ivanvillareal.info/?p=76</guid>
		<description><![CDATA[I have the need to install php5 and mysql 5 on a centos 4 server, so I found that the Centos Plus repository had this, and this is what I did to get these two working.
First check what is the version of the distro:
cat /etc/*release*
Then make an update:
yum update
After 496 package updates I&#8217;ve installed php5
yum [...]]]></description>
			<content:encoded><![CDATA[<p>I have the need to install php5 and mysql 5 on a centos 4 server, so I found that the Centos Plus repository had this, and this is what I did to get these two working.</p>
<p>First check what is the version of the distro:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow"><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/*</span>release<span style="color: #000000; font-weight: bold;">*</span></div></pre><!--END_DEVFMTCODE--></p>
<p>Then make an update:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow">yum update</div></pre><!--END_DEVFMTCODE--></p>
<p>After 496 package updates I&#8217;ve installed php5<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow">yum <span style="color: #660033;">--enablerepo</span>=centosplus <span style="color: #c20cb9; font-weight: bold;">install</span> php</div></pre><!--END_DEVFMTCODE--></p>
<p>This were the installed packages:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow">php-5.1.6-3.el4s1.10
php-cli-5.1.6-3.el4s1.10
php-common-5.1.6-3.el4s1.10
</div></pre><!--END_DEVFMTCODE--></p>
<p>So I&#8217;ve created a info.php file, and restarted the apache server:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&lt;?php phpinfo(); ?&gt;&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>html<span style="color: #000000; font-weight: bold;">/</span>info.php <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> service httpd restart</div></pre><!--END_DEVFMTCODE--></p>
<p>It was working correctly, so the next thing was to have mysql 5 in it, so I did:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow">yum <span style="color: #660033;">--enablerepo</span>=centosplus <span style="color: #c20cb9; font-weight: bold;">install</span> mysql-server</div></pre><!--END_DEVFMTCODE--></p>
<p>I got some problems here, first I got this:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow">Transaction Check Error: <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>my.cnf from <span style="color: #c20cb9; font-weight: bold;">install</span> of mysql-libs-5.0.68-1.el4_6 conflicts with <span style="color: #c20cb9; font-weight: bold;">file</span> from package mysql-4.1.22-2.el4
<span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>charsets<span style="color: #000000; font-weight: bold;">/</span>Index.xml from <span style="color: #c20cb9; font-weight: bold;">install</span> of mysql-libs-5.0.68-1.el4_6 conflicts with <span style="color: #c20cb9; font-weight: bold;">file</span> from package mysql-4.1.22-2.el4
<span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>charsets<span style="color: #000000; font-weight: bold;">/</span>README from <span style="color: #c20cb9; font-weight: bold;">install</span> of mysql-libs-5.0.68-1.el4_6 conflicts with <span style="color: #c20cb9; font-weight: bold;">file</span> from package mysql-4.1.22-2.el4
<span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>charsets<span style="color: #000000; font-weight: bold;">/</span>armscii8.xml from <span style="color: #c20cb9; font-weight: bold;">install</span> of mysql-libs-5.0.68-1.el4_6 conflicts with <span style="color: #c20cb9; font-weight: bold;">file</span> from package mysql-4.1.22-2.el4
<span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>charsets<span style="color: #000000; font-weight: bold;">/</span>ascii.xml from <span style="color: #c20cb9; font-weight: bold;">install</span> of mysql-libs-5.0.68-1.el4_6 conflicts with <span style="color: #c20cb9; font-weight: bold;">file</span> from package mysql-4.1.22-2.el4</div></pre><!--END_DEVFMTCODE--></p>
<p>So what I found was that yum was trying to install mysql.i386 but the architechture is x86_64, to fix this I had to specify the arch<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow">yum <span style="color: #660033;">--enablerepo</span>=centosplus <span style="color: #c20cb9; font-weight: bold;">install</span> mysql.x86_64 mysql-server</div></pre><!--END_DEVFMTCODE--></p>
<p>After this I was able to install it, but when I tried to start the server I received some errors about error messages not right, I&#8217;ve checked the installed packages and it was installed mysqlclient10, so I removed the error messages problem disappeared, however I still couldn&#8217;t startup the server, checking at the logs I found this:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow">InnoDB: Creating foreign key constraint system tables
InnoDB: Foreign key constraint system tables created
091012 11:01:18  InnoDB: Started; log sequence number <span style="color: #000000;">0</span> <span style="color: #000000;">0</span>                       091012 <span style="color: #000000;">11</span>:01:<span style="color: #000000;">18</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>ERROR<span style="color: #7a0874; font-weight: bold;">&#93;</span> Fatal error: Can<span style="color: #ff0000;">'t open and lock privilege tables: Table '</span>mysql.host<span style="color: #ff0000;">' doesn'</span>t exist                                                     091012 <span style="color: #000000;">11</span>:01:<span style="color: #000000;">18</span>  mysqld ended                                               </div></pre><!--END_DEVFMTCODE-->\</p>
<p>To fix this I cleared the data dir, that had som files, this was in &#8216;/var/lib/mysql&#8217; and after that I ran mysql_install_db:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>prestant <span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># cd /var/lib/mysql </span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>prestant mysql<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># rm -rf *</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>prestant mysql<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># mysql_install_db --user=mysql -ldata=/var/lib/mysql</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>prestant mysql<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># service mysqld start</div></pre><!--END_DEVFMTCODE--></p>
<p>After the server was up, I changed the root password and added a new user:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Bash"><div class="devcodeoverflow"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>prestant mysql<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># mysqladmin -u root password NEWPASSWORD</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>prestant mysql<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># mysql -pNEWPASSWORD</span>
&nbsp;
</div></pre><!--END_DEVFMTCODE--><br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="MySQL"><div class="devcodeoverflow">Welcome <span style="color: #990099; font-weight: bold;">to</span> the MySQL monitor.  Commands <span style="color: #009900;">end</span> <span style="color: #990099; font-weight: bold;">with</span> <span style="color: #000033;">;</span> <span style="color: #CC0099; font-weight: bold;">or</span> \g.
Your MySQL <span style="color: #FF9900; font-weight: bold;">connection</span> id <span style="color: #CC0099; font-weight: bold;">is</span> 5
Server <span style="color: #000099;">version</span>: 5.0.82sp1 Source distribution
&nbsp;
<span style="color: #990099; font-weight: bold;">Type</span> <span style="color: #008000;">'help;'</span> <span style="color: #CC0099; font-weight: bold;">or</span> <span style="color: #008000;">'<span style="color: #004000; font-weight: bold;">\h</span>'</span> for <span style="color: #990099; font-weight: bold;">help</span>. <span style="color: #990099; font-weight: bold;">Type</span> <span style="color: #008000;">'<span style="color: #004000; font-weight: bold;">\c</span>'</span> <span style="color: #990099; font-weight: bold;">to</span> clear the current input statement.
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #000099;">USER</span> <span style="color: #008000;">'ivan'</span>@<span style="color: #008000;">'<span style="color: #008080; font-weight: bold;">%</span>'</span> IDENTIFIED BY <span style="color: #008000;">'passhere'</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> 0 rows affected <span style="color: #FF00FF;">&#40;</span>0.00 sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">GRANT</span> <span style="color: #990099; font-weight: bold;">ALL</span> <span style="color: #990099; font-weight: bold;">PRIVILEGES</span> <span style="color: #990099; font-weight: bold;">ON</span> <span style="color: #CC0099;">*</span>.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">TO</span> <span style="color: #008000;">'ivan'</span>@<span style="color: #008000;">'<span style="color: #008080; font-weight: bold;">%</span>'</span> IDENTIFIED BY <span style="color: #008000;">'passhere'</span> <span style="color: #990099; font-weight: bold;">WITH</span> <span style="color: #990099; font-weight: bold;">GRANT</span> <span style="color: #990099; font-weight: bold;">OPTION</span> MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 <span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> 0 rows affected <span style="color: #FF00FF;">&#40;</span>0.00 sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #000099;">USER</span> <span style="color: #008000;">'ivan'</span>@<span style="color: #008000;">'localhost'</span> IDENTIFIED BY <span style="color: #008000;">'passhere'</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> 0 rows affected <span style="color: #FF00FF;">&#40;</span>0.00 sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> <span style="color: #990099; font-weight: bold;">GRANT</span> <span style="color: #990099; font-weight: bold;">ALL</span> <span style="color: #990099; font-weight: bold;">PRIVILEGES</span> <span style="color: #990099; font-weight: bold;">ON</span> <span style="color: #CC0099;">*</span>.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">TO</span> <span style="color: #008000;">'ivan'</span>@<span style="color: #008000;">'localhost'</span> <span style="color: #990099; font-weight: bold;">WITH</span> <span style="color: #990099; font-weight: bold;">GRANT</span> <span style="color: #990099; font-weight: bold;">OPTION</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> 0 rows affected <span style="color: #FF00FF;">&#40;</span>0.00 sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> flush <span style="color: #990099; font-weight: bold;">privileges</span><span style="color: #000033;">;</span>
Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.00</span> sec<span style="color: #FF00FF;">&#41;</span>
&nbsp;
mysql<span style="color: #CC0099;">&gt;</span> exit
Bye
&nbsp;
&nbsp;
</div></pre><!--END_DEVFMTCODE--></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ivanvillareal.info/linux/mysql5-and-php5-on-centos4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mysql unique keys and collations</title>
		<link>http://blog.ivanvillareal.info/development/mysql-unique-keys-and-collations/</link>
		<comments>http://blog.ivanvillareal.info/development/mysql-unique-keys-and-collations/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 01:10:59 +0000</pubDate>
		<dc:creator>Ivan Villareal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://blog.ivanvillareal.info/?p=65</guid>
		<description><![CDATA[I&#8217;m working on an application that stores unique values on a field, so I used the unique index for this column, everything was ok, until I&#8217;ve started inserting&#160;multi byte&#160;characters&#160;in it.
The charachters inserted fine, the problem was that I&#8217;m using INSERT &#8230; ON DUPLICATE KEY UPDATE&#160;syntax to avoid looking if the field exists, so after debugging [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on an application that stores unique values on a field, so I used the unique index for this column, everything was ok, until I&#8217;ve started inserting&nbsp;multi byte&nbsp;characters&nbsp;in it.</p>
<p>The charachters inserted fine, the problem was that I&#8217;m using <a href="http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html" target="_blank">INSERT &#8230; ON DUPLICATE KEY UPDATE</a>&nbsp;syntax to avoid looking if the field exists, so after debugging where the application was breaking I got this:</p>
<blockquote><p>Query failed: Cannot add or update a child row: a foreign key constraint fails (`trubaseek/mapKeywordName`, CONSTRAINT `fkDomainMapNameId` FOREIGN KEY (`NameId`) REFERENCES `names` (`nameId`) ON DELETE NO ACTION ON UPDATE NO ACTION)&lt;br&gt; SQL: INSERT INTO `mapKeywordName` (`keywordId`, `nameId`) VALUES (&#8217;1&#8242;, &#8216;326&#8242;);</p></blockquote>
<p>so I&#8217;v spent about an hour changing the encodings&nbsp;without&nbsp;success, but after some testing I discovered that <strong>hôteles</strong> was the same as <strong>hotelës, </strong>I did several testing from the linux console, the mysql browser, and from a php script, and all revealed the same.</p>
<p>Whe I did this</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="MySQL"><div class="devcodeoverflow"><span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #008000;">`names`</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`name`</span><span style="color: #000033;">,</span> <span style="color: #008000;">`price`</span><span style="color: #000033;">,</span> <span style="color: #008000;">`priceCurrency`</span><span style="color: #000033;">,</span> <span style="color: #008000;">`bids`</span><span style="color: #000033;">,</span> <span style="color: #008000;">`traffic`</span><span style="color: #000033;">,</span> <span style="color: #008000;">`lastUpdate`</span><span style="color: #FF00FF;">&#41;</span> 
<span style="color: #990099; font-weight: bold;">VALUES</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'hotelës'</span><span style="color: #000033;">,</span> <span style="color: #008000;">'0'</span><span style="color: #000033;">,</span> <span style="color: #008000;">'$US'</span><span style="color: #000033;">,</span> <span style="color: #008000;">'0'</span><span style="color: #000033;">,</span> <span style="color: #008000;">'0'</span><span style="color: #000033;">,</span> <span style="color: #000099;">NOW</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> 
<span style="color: #990099; font-weight: bold;">ON</span> <span style="color: #990099; font-weight: bold;">DUPLICATE KEY</span> <span style="color: #990099; font-weight: bold;">UPDATE</span> <span style="color: #008000;">`price`</span><span style="color: #CC0099;">=</span><span style="color: #008000;">'0'</span><span style="color: #000033;">,</span> <span style="color: #008000;">`priceCurrency`</span><span style="color: #CC0099;">=</span><span style="color: #008000;">'$US'</span><span style="color: #000033;">,</span> <span style="color: #008000;">`bids`</span><span style="color: #CC0099;">=</span><span style="color: #008000;">'0'</span><span style="color: #000033;">,</span> <span style="color: #008000;">`traffic`</span><span style="color: #CC0099;">=</span><span style="color: #008000;">'0'</span><span style="color: #000033;">,</span> <span style="color: #008000;">`lastUpdate`</span> <span style="color: #CC0099;">=</span> <span style="color: #000099;">NOW</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></div></pre><!--END_DEVFMTCODE--></p>
<p>I&#8217;ve got this:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="MySQL"><div class="devcodeoverflow">Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">2</span> rows affected <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">0.01</span> sec<span style="color: #FF00FF;">&#41;</span></div></pre><!--END_DEVFMTCODE--></p>
<p>Called from a php script <strong>mysql_insert_id(); </strong>returned me an Id like if it was inserted not updated, but when if I select the returned Id, an error were thrown, because it didn&#8217;t existed.</p>
<p>So after some research, I&#8217;ve found that adding a collation <dfn title="Unicode (multilingual), Binary"><strong>utf8_bin </strong>to that column fixed this issue.</dfn></p>
<p>I haven&#8217;t tested this enough but for now it appears to be working.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ivanvillareal.info/development/mysql-unique-keys-and-collations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- www.000webhost.com Analytics Code -->
<script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script>
<noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/count.php" alt="web hosting" /></a></noscript>
<!-- End Of Analytics Code -->
