<?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 &#187; Script to copy data from MySQL to postgreSQL &#8211; Ivan Villareal</title>
	<atom:link href="http://ivanvillareal.com/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://ivanvillareal.com</link>
	<description>IT stuff and more...</description>
	<lastBuildDate>Tue, 01 Nov 2011 23:00:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Script to copy data from MySQL to postgreSQL</title>
		<link>http://ivanvillareal.com/development/script-to-copy-data-from-mysql-to-postgres/</link>
		<comments>http://ivanvillareal.com/development/script-to-copy-data-from-mysql-to-postgres/#comments</comments>
		<pubDate>Sat, 02 Apr 2011 12:28:32 +0000</pubDate>
		<dc:creator>Ivan Villareal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[migration]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://ivanvillareal.com/?p=190</guid>
		<description><![CDATA[This is a perl script I&#8217;ve used to move data from mysql 5.1 to postgres 9, this script doesn&#8217;t create a schema, it only grabs the data from mysql and [...]]]></description>
			<content:encoded><![CDATA[<p>This is a perl script I&#8217;ve used to move data from mysql 5.1 to postgres 9, this script doesn&#8217;t create a schema, it only grabs the data from mysql and try to put it on the same table in postgres.</p>
<p>The only requirement is DBI for mysql and postgres.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># this script moves all the data from mysql to postgres</span>
<span style="color: #666666; font-style: italic;"># modules required are:</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># http://search.cpan.org/~timb/DBI-1.616/DBI.pm</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># please change the dsn, srcUser/srcPass and dstUser/Pass variables.</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$srcUser</span>   <span style="color: #339933;">=</span> <span style="color: #ff0000;">'ivan'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$srcPass</span>   <span style="color: #339933;">=</span> <span style="color: #ff0000;">'123'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$srcDbName</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'datatocopy'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$srcDbHost</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'mini'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dstUser</span>  <span style="color: #339933;">=</span> <span style="color: #ff0000;">'ivan'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dstPass</span>  <span style="color: #339933;">=</span> <span style="color: #ff0000;">'456'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dstDbName</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'migrateddata'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dstDbHost</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'192.168.1.145'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">### do not modify ##</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$srcDb</span>  <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;dbi:mysql:database=$srcDbName;host=$srcDbHost&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$srcUser</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$srcPass</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dstDb</span>  <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;dbi:Pg:database=$dstDbName;host=$dstDbHost&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$dstUser</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$dstPass</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">####main########</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Starting to move data from $srcDbHost to $dstDbHost<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$srcDb</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">do</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SET NAMES <span style="color: #000099; font-weight: bold;">\'</span>UTF8<span style="color: #000099; font-weight: bold;">\'</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #0000ff;">$dstDb</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">do</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SET CLIENT_ENCODING=<span style="color: #000099; font-weight: bold;">\'</span>UTF8<span style="color: #000099; font-weight: bold;">\'</span>&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$srcTables</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$srcDb</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">selectall_arrayref</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SHOW TABLES&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dstTables</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dstDb</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">selectall_hashref</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT * FROM pg_tables WHERE NOT tablename ~<span style="color: #000099; font-weight: bold;">\'</span>^(pg_|sql_)<span style="color: #000099; font-weight: bold;">\'</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;tablename&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$seqlist</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dstDb</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">selectall_arrayref</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT RELNAME FROM pg_class WHERE relkind=<span style="color: #000099; font-weight: bold;">\'</span>S<span style="color: #000099; font-weight: bold;">\'</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$seqmap</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$s</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@$seqlist</span><span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$seqname</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$seqname</span><span style="color: #339933;">=~/^</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>_<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>z<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span>_<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>z<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$)</span><span style="color: #339933;">/</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>   
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$item</span><span style="color: #339933;">;</span>
        <span style="color: #0000ff;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>seq<span style="color: #009900;">&#125;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$seqname</span><span style="color: #339933;">;</span>
        <span style="color: #0000ff;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>field<span style="color: #009900;">&#125;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$2</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$table</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$1</span><span style="color: #339933;">;</span> 
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">exists</span> <span style="color: #0000ff;">$dstTables</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$table</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>   
            <span style="color: #000066;">push</span> <span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$seqmap</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$table</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$item</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: #000066;">print</span> <span style="color: #000000; font-weight: bold;">STDERR</span> <span style="color: #ff0000;">&quot;WARN: cannot find table for sequence $seqname<span style="color: #000099; font-weight: bold;">\n</span>&quot;</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: #0000ff;">$dstDb</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>AutoCommit<span style="color: #009900;">&#125;</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$totalRowCount</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$t</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@$srcTables</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$table</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$t</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">exists</span> <span style="color: #0000ff;">$dstTables</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$table</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">print</span> <span style="color: #000000; font-weight: bold;">STDERR</span> <span style="color: #ff0000;">&quot;WARN: table $table does not exists in dest db Pg:$dstDbName<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">next</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
	<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Clearing data from table $table in $dstDbHost<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    clear_table<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dstDb</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$table</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">next</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$table</span><span style="color: #339933;">=~/^</span>cache<span style="color: #339933;">/;</span>
    <span style="color: #b1b100;">next</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$table</span><span style="color: #339933;">=~/^</span>locales<span style="color: #339933;">/;</span>
	<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Copying data from mysql table $table in $srcDbHost to postgres table $table in $dstDbHost<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cnt</span> <span style="color: #339933;">=</span> copy_table<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$srcDb</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$dstDb</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$table</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">unless</span> <span style="color: #0000ff;">$table</span><span style="color: #339933;">=~/^</span>cache<span style="color: #339933;">/;</span>
	<span style="color: #0000ff;">$totalRowCount</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$totalRowCount</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">$cnt</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;$cnt rows copied ($table)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    init_seq<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dstDb</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$table</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$seqmap</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$table</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># if (exists $seqmap-&gt;{$table});</span>
  <span style="color: #009900;">&#125;</span>  
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Commiting changes <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>     
<span style="color: #0000ff;">$dstDb</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">commit</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;$totalRowCount rows were copied.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">exit</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> init_seq  <span style="color: #009900;">&#123;</span>  
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$db</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$table</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$list</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">return</span> <span style="color: #b1b100;">unless</span> <span style="color: #0000ff;">$list</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$list</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ref</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">selectall_arrayref</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT MAX($item-&gt;{field}) FROM $table&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$val</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$ref</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #0000ff;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">do</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT SETVAL(<span style="color: #000099; font-weight: bold;">\'</span>$item-&gt;{seq}<span style="color: #000099; font-weight: bold;">\'</span>,$val)&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span><span style="color: #339933;">;</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;">sub</span> clear_table <span style="color: #009900;">&#123;</span>  
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dbh</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$table</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">return</span> <span style="color: #b1b100;">unless</span> <span style="color: #0000ff;">$table</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">do</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;DELETE FROM $table&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>  
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> copy_table <span style="color: #009900;">&#123;</span> 
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$srcDb</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$dstDb</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$table</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">die</span> <span style="color: #b1b100;">unless</span> <span style="color: #0000ff;">$table</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$slf</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$srcDb</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;select * from $table limit 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$slf</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">return</span> <span style="color: #cc66cc;">0</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$rows</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$rec1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$slf</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetchrow_hashref</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$slf</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">finish</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@fieldnames</span> <span style="color: #339933;">=</span> <span style="color: #000066;">sort</span> <span style="color: #000066;">keys</span> <span style="color: #0000ff;">%$rec1</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@qm</span> <span style="color: #339933;">=</span>  <span style="color: #000066;">map</span> <span style="color: #009900;">&#123;</span> <span style="color: #ff0000;">'?'</span><span style="color: #009900;">&#125;</span> <span style="color: #0000ff;">@fieldnames</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ins</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dstDb</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;INSERT INTO $table (&quot;</span><span style="color: #339933;">.</span><span style="color: #000066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;,&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">@fieldnames</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;) VALUES(&quot;</span><span style="color: #339933;">.</span><span style="color: #000066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;,&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">@qm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sel</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$srcDb</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SELECT * FROM $table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$sel</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cnt</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$sel</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetchrow_hashref</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$ins</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">map</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span> <span style="color: #0000ff;">@fieldnames</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span><span style="color: #339933;">;</span>
        <span style="color: #0000ff;">$cnt</span><span style="color: #339933;">++;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #0000ff;">$ins</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">finish</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$sel</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">finish</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">return</span> <span style="color: #0000ff;">$cnt</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ivanvillareal.com/development/script-to-copy-data-from-mysql-to-postgres/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mysql5 and PHP5 on centos4</title>
		<link>http://ivanvillareal.com/linux/mysql5-and-php5-on-centos4/</link>
		<comments>http://ivanvillareal.com/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://ivanvillareal.com/?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 [...]]]></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:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><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></pre></div></div>

<p>Then make an update:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">yum update</pre></div></div>

<p>After 496 package updates I&#8217;ve installed php5</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">yum <span style="color: #660033;">--enablerepo</span>=centosplus <span style="color: #c20cb9; font-weight: bold;">install</span> php</pre></div></div>

<p>This were the installed packages:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">php-5.1.6-<span style="color: #000000;">3</span>.el4s1.10
php-cli-5.1.6-<span style="color: #000000;">3</span>.el4s1.10
php-common-5.1.6-<span style="color: #000000;">3</span>.el4s1.10</pre></div></div>

<p>So I&#8217;ve created a info.php file, and restarted the apache server:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><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</pre></div></div>

<p>It was working correctly, so the next thing was to have mysql 5 in it, so I did:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">yum <span style="color: #660033;">--enablerepo</span>=centosplus <span style="color: #c20cb9; font-weight: bold;">install</span> mysql-server</pre></div></div>

<p>I got some problems here, first I got this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">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-<span style="color: #000000;">1</span>.el4_6 conflicts with <span style="color: #c20cb9; font-weight: bold;">file</span> from package mysql-4.1.22-<span style="color: #000000;">2</span>.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-<span style="color: #000000;">1</span>.el4_6 conflicts with <span style="color: #c20cb9; font-weight: bold;">file</span> from package mysql-4.1.22-<span style="color: #000000;">2</span>.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-<span style="color: #000000;">1</span>.el4_6 conflicts with <span style="color: #c20cb9; font-weight: bold;">file</span> from package mysql-4.1.22-<span style="color: #000000;">2</span>.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-<span style="color: #000000;">1</span>.el4_6 conflicts with <span style="color: #c20cb9; font-weight: bold;">file</span> from package mysql-4.1.22-<span style="color: #000000;">2</span>.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-<span style="color: #000000;">1</span>.el4_6 conflicts with <span style="color: #c20cb9; font-weight: bold;">file</span> from package mysql-4.1.22-<span style="color: #000000;">2</span>.el4</pre></div></div>

<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</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">yum <span style="color: #660033;">--enablerepo</span>=centosplus <span style="color: #c20cb9; font-weight: bold;">install</span> mysql.x86_64 mysql-server</pre></div></div>

<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:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">InnoDB: Creating foreign key constraint system tables
InnoDB: Foreign key constraint system tables created
091012 <span style="color: #000000;">11</span>:01:<span style="color: #000000;">18</span>  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</pre></div></div>

<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:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><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</span></pre></div></div>

<p>After the server was up, I changed the root password and added a new user:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><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></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;">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> <span style="color: #008080;">5</span>
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> <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> <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 <span style="color: #008080;">0</span> MAX_CONNECTIONS_PER_HOUR <span style="color: #008080;">0</span> MAX_UPDATES_PER_HOUR <span style="color: #008080;">0</span> MAX_USER_CONNECTIONS <span style="color: #008080;">0</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> <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> <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> <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> <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> 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</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ivanvillareal.com/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://ivanvillareal.com/development/mysql-unique-keys-and-collations/</link>
		<comments>http://ivanvillareal.com/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://ivanvillareal.com/?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. [...]]]></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>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">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)&amp;lt;br&amp;gt; 
SQL: INSERT INTO `mapKeywordName` (`keywordId`, `nameId`) VALUES ('1', '326');</pre></div></div>

<p>so I&#8217;ve 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>When I did this</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><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></pre></div></div>

<p>I&#8217;ve got this:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;">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></pre></div></div>

<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 <strong>utf8_bin </strong>to that column fixed this issue.</p>
<p>I haven&#8217;t tested this enough but for now it appears to be working.</p>
]]></content:encoded>
			<wfw:commentRss>http://ivanvillareal.com/development/mysql-unique-keys-and-collations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

