Convert non-UTF-8 MySQL database to UTF-8
目的
Database upgrades may fail due to a wrong character set usage. This article explains how to convert a non-UTF-8 MySQL database into the recommended UTF-8 database.
データベースの変更を行う場合は必ず事前にバックアップを取得してください。可能な場合は、まずステージング サーバーで SQL コマンドの変更、挿入、更新、または削除を行うようにします。
ソリューション
Let's suppose the database is using
latin1
character set andlatin1_general_cs
collation:CREATE DATABASE dbname CHARACTER SET latin1 COLLATE latin1_general_cs;
- Make sure that Fisheye / Crucible is not running;
- Connect to the database being used by Fisheye / Crucible;
Create a database dump:
mysqldump --opt -u root -p<secret> dbname > dbname.sql
- Open the resulting
dbname.sql
file and replace all occurrences oflatin1_general_cs
byutf8_bin
and all occurrences oflatin1
byutf8
, through find / replace, then save changes. Restore
dbname.sql
. There are two possibilities here:Restore into a new database name, if you want to keep the original database untouched. This consists in:
Creating a new database, this time using the correct character set and collation:
CREATE DATABASE newdbname CHARACTER SET utf8 COLLATE utf8_bin;
Restoring the dump:
mysql -u root -p<secret> newdbname < dbname.sql
- Restore into the existing database name, if you're sure that the manual changes in
dbname.sql
through find / replace are correct. This consists in:Deleting the current database
dbname
:DROP DATABASE dbname;
Creating it again, this time using the correct character set and collation:
CREATE DATABASE dbname CHARACTER SET utf8 COLLATE utf8_bin;
Restoring the dump:
mysql -u root -p<secret> dbname < dbname.sql
Restart MySQL service
Start Fisheye / Crucible