Tuesday, June 2, 2009

Convert MySQL charset from Latin1 to UTF-8 easily

Latest versions of MySQL finally supports multiple charsets… This is great when you have to store date from different sources and manage multilingual application (especially the ones with glyphs and strange characters).

When migrating an application you may want to convert from Latin1 (ISO 88xx) charset to the most comprehensive Unicode UTF-8.

  • First of all made a dump of your database table.
  • Use iconv as follows:
    iconv -c -f latin1 -t utf-8 filename.sql > filename-utf8.sql
  • Backup the old table and truncate it.
  • Ensure every field has collation in utf8
  • Replace “latin1″ with “utf8″ in filename-utf8.sql
  • Reimport data and have fun ;-)

No comments: