MySQL Bulk Update Collation and Character Set for Entire Database

This is post is now quite old and the the information it contains may be out of date or innacurate.

If you find any errors or have any suggestions to update the information please let us know or create a pull request on GitHub

If you need to change the character set and collation for all columns and tables on an entire database then check out this little PHP script.

It’s currently Zend Framework based, though only for the database adapter - you could easily modify this to suit your own database connection.

/* collationChange
 * Edmonds Commerce
 * 0844 357 0201
 * Description:
 * Bulk Change the Collation and Character Set for an Entire Database
 * Inspiration Came from Here:

//get Db connection etc
$db = Zend_Registry::get('dbAdapter');

$character_set = 'utf8';
$collation = 'utf8_general_ci';

$stmt = $db->query("SELECT distinct CONCAT( 'alter table ', TABLE_SCHEMA, '.', TABLE_NAME, '  CONVERT TO CHARACTER SET $character_set COLLATE $collation;' ) as query, TABLE_NAME as t FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '$dbName';");
    echo "<h3>Done {$r['t']}</h3>";

Tags: mysqlphpbulkcollationcharacter setchange