Changing the collation of all tables in a mysql database

If you're reading this, you're probably wanting to change the collation of your mysql database to utf8_general_ci in order to support non-English characters.

There seems to be lots of methods out there for doing this, probably none of which are as quick and simple as my script below :cool:

This script changes the collation of the mysql database, every table and every column that has an existing collation.

Cut and paste the script below into a file called (say) change_collation.php on your server. Set the connection properties at the top of the script, then call the script via your browser.

*** TAKE A BACKUP BEFORE RUNNING THIS SCRIPT ***

If you don't take a complete backup before running this script then you are extremely foolish and don't come crying to me when all your data is gone. This script worked fine for me but changing collations might have all sorts of odd effects that I can't predict on your system. Almost certainly everything will be fine, especially if you have a backup !!


<?php

// Configuration Section
$server = 'localhost'; //probably localhost but change if required
$username = 'your_username';
$password = 'your_password';
$database = 'your_database';
$new_charset = 'utf8'; // change to the required character set - you're probably changing to utf8 ?
$new_collation = 'utf8_general_ci'; // change to the required collation - you're probably changing to utf8_general_ci ?

// Connect to database
$db = mysql_connect($server, $username, $password); if(!$db) die("Cannot connect to database server -".mysql_error());
$select_db = mysql_select_db($database); if (!$select_db) die("could not select $database: ".mysql_error());

// change database collation
mysql_query("ALTER DATABASE $database DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");

// Loop through all tables changing collation
$result=mysql_query('show tables');
while($tables = mysql_fetch_array($result)) {
$table = $tables[0];
mysql_query("ALTER TABLE $table DEFAULT CHARACTER SET $new_charset COLLATE $new_collation");

// loop through each column changing collation
$columns = mysql_query("SHOW FULL COLUMNS FROM $table where collation is not null");
while($cols = mysql_fetch_array($columns)) {
$column = $cols[0];
$type = $cols[1];
mysql_query("ALTER TABLE $table MODIFY $column $type CHARACTER SET $new_charset COLLATE $new_collation");
}

print "changed collation of $table to $new_collation<br/>";
}
print '<br/>The collation of your database has been successfully changed!<br/>';
?>

Categories:

Rate this page: 
No votes yet
Simon's picture

Comments

Firstly as you know I am an Oracle peep not MySql, so I am confused by terminology - you use Collation where I would expect to use Character Set.

But that aside, are you sure this works on the data? i'm sure it is possible to have data in the format of US7ASCII (7bit) in a WE8ISOxx (whatever the mysql equivalent is) 8bit database structure, as this US7ASCII 7bit is a subset of said WE8ISOxxx 8bit.
Admittedly this example going from a 7 to an 8 may not give you any difficulties, as there will be no Euro signs or umlauts for instance and they are correlated, but if you had a 7bit character set (that was a subset of a different 8bit character set) then you changed to the above mentioned (unrelated) 8bit character set - you may well have unexpected (and wrong) character translations when that data is accessed. Just thinking out loud.

A safer way surely would be to export all the data using a bulk unload utility, change the character set of the database, then re-import the data so it gets properly converted by the inbuilt "from character set" - "to character set" routines.

Is there not an export and import data utility for MySQL ?

wow, great!

Works fine and save lots of time of mine. I am using MySQL 5. Thanks!

Hey there,
quick note
1. the alter database doesn't use variables
2. if anyone is using this for magento be aware that catalogrule_group_website has a different collation

thanks for the script

Wow, thank you VERY MUCH!!! It works like a charm!!

Works perfectly! Thank You.

Awesome. Worked like a charm. Thank you very much indeed!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Insert Google Map macro.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Insert Google Map macro.
  • You may use [view:name=display=args] tags to display views.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
To prevent automated spam submissions leave this field empty.