lost password?

home
•  xaraya
•  rails
•  django
•  webdev
•  xamp +
•  musings

rss
Tag this page
   

ยป Blogs that link here
last modified: Aug 02, 2006
(first posted: Jun 29, 2006)
(7538 Reads)
keywords: mysql
Permalink

Compatibility between mySQL 4 and 5

I regularly move my projects between various servers, which often have different versions of software tools. I encountered a problem moving data from MySQL 5 to MySQL 4. Here's the fix.

In general, I use mysqldump to export databases, and mysql to import them.

MySQL 4 to 5

No problems here.

1. Export the database on the system with MySQL 4:

mysqldump -p --opt dbname >dbname.sql

 

2. Copy the database to the other sytem, for example,

scp dbname.sql me@domain.com:/home/me

 

3. To import the database on the system with MySQL 5:

mysqladmin -p drop dbname

mysqladmin -p create dbname

mysql -p -D dbname <dbname.sql

 

MySQL 5 to 4

The issue here is 4 cannot directly import files dumped by 5. You must use the --compatible=mysql40 flag. However, that does not export the "autoincrement" option on tables that are autoincremented, so re-importing the data will fail.

I've assembled a script from various googled sources (unfortunately I didnt add those links in my comments). I called it "mysql-aifix", and it takes the dbname and creates a file called "dbname-fix.sql". After importing the database, also import this fix sql to ensure your autoincrement tables are autoincrementing.

1. Export the database on the system with MySQL 5:

mysqldump -p --opt --compatible=mysql40 dbname >dbname.sql

mysql-aifix dbname

 

2. Copy the database to the other sytem, for example,

scp dbname.sql me@domain.com:/home/me

scp dbname-fix.sql me@domain.com:/home/me

 

3. To import the database on the system with MySQL 4:

mysqladmin -p drop dbname

mysqladmin -p create dbname

mysql -p -D dbname <dbname.sql

mysql -p -D dbname <dbname-fix.sql

 

The msql-aifix Script

Paste this code into a file named mysql-aifix, and chmod +x it.

 

#!/usr/bin/php
<?php
$hostname = "localhost";
$username = "root";
$password = "secret";
if ($argc < 2) {
echo "Usage: mysql-aifix [dbname] \n";
echo "Adds autoincrement to mysqldump for database dbname \n";
echo "Outputs file dbname-fix.sql \n";
exit;
}

$dbName = $argv[1];
mysql_connect($hostname,$username,$password) or die("Can't create connection");
$res1 = mysql_query("SHOW TABLES FROM $dbName");
if (empty($res1)) die("Can't find database '" . $dbName . "'");
$i=0;
$fname = $dbName . "-fix.sql";
$fp = fopen ($fname, "w");
while($row1 = mysql_fetch_array($res1)){
$res2 = mysql_db_query($dbName, "SHOW CREATE TABLE $row1[0]");
while($row2 = mysql_fetch_array($res2)){
preg_match("/\s*(\W\w+\W)(.*auto_increment)/", $row2[1], $matches);
if($matches){
$i++;
$str = "ALTER TABLE ".$row2[0]." CHANGE ".$matches[1]."
".$matches[1]."".$matches[2].";\n";
fwrite($fp, $str);
}
}
mysql_free_result($res2);
}
fclose($fp);
mysql_free_result($res1);
mysql_close();
echo "Entries created: ".$i . " in file " . $fname;
?>

 

Compatibility between mySQL 4 and 5

Posted by: Blaisorblade on February 23, 2007 09:19 PM
Remember to edit the file and set the correct password (optionally hostname and username) near the beginning of mysql-aifix. And thanks for this!

#

Compatibility between mySQL 4 and 5

Posted by: Elton on May 14, 2007 08:29 AM
Just thought I would let anyone else looking here know that as of MySQL 5.0.19 the mysql-aifix script is no longer required. :)

#

Compatibility between mySQL 4 and 5

Posted by: nirvan, guwahati assam on July 03, 2007 04:22 AM
this is what i was looking for.

#

Compatibility between mySQL 4 and 5

Posted by: ben on September 03, 2007 03:51 PM
THANK YOU!!!! this is perfect! you are a godsend.

#

Compatibility between mySQL 4 and 5

Posted by: David on October 28, 2007 03:23 AM
Te felicito, estuve buscando algo asi durante muchas horas hasta que al fin lo encontre, necesitaba pasar una base mysql5 a mysql4 y no lo lograba hacer correctamente.

#

Compatibility between mySQL 4 and 5

Posted by: linoj on October 28, 2007 11:01 AM
David's message, through Google Translate: I congratulate you, I was looking for something for so many hours until finally I found it, I needed to spend a mysql5 to mysql4 and not managed properly.

#

Compatibility between mySQL 4 and 5

Posted by: over on December 18, 2007 07:49 PM
Thanks a lot, as David, I've spent a lot of time trying to solve this issue until discover your script. Again, THANKS A LOT! ... y felices fiestas!

#

Re: Compatibility between mySQL 4 and 5

Posted by: Emanuele on March 04, 2008 08:51 AM
really... thank you! I find it really usefull.

#

Re: Compatibility between mySQL 4 and 5

Posted by: Keith on November 03, 2008 03:21 PM
If mysql-aifix script is no longer required, how can we get the auto-increment to dump with mysqldump?

#

Post a new comment

How many days in a week?

Name :