Migrating MySQL latin1 to utf8 – In House Version

This entry is part 4 of 4 in the series Migrat­ing MySQL latin1 to utf8

Use this method if at all pos­si­ble as it will attempt to recover non-English latin1 char­ac­ters (accents, umlauts) in your exist­ing data.

  • Con­firm your data­base is cur­rently encoded in latin1.
  • Make a fresh backup (ide­ally using mysql­hot­copy se notes below)
  • Tem­porar­ily dis­able your cron­job so you don’t have any­thing try­ing to access the database.
  • The steps below require the ‘iconv’ util­ity to ensure proper UTF8 encod­ing. Type ‘iconv –ver­sion’ at the shell to check if it’s installed. If not, install it from your platform’s pack­age man­ager (e.g. apt-get, yum, rpm).

Dump the data­base schema (use your own user + pass­word + data­base):

mysqldump -Q -d -u root -p
--default-character-set=latin1 --skip-set-charset
old_database | sed 's/latin1/utf8/gi' > dump.schema.sql

Dump the data­base data (use your own user + pass­word + data­base):

mysqldump -Q --insert-ignore -t -u root -p
--default-character-set=latin1 --skip-set-charset
old_database | iconv -c -f utf8 -t utf8 > dump.data.sql

Cre­ate a new UTF-8 data­base:

CREATE DATABASE newdb_utf8 CHARACTER SET utf8 COLLATE utf8_general_ci;

Import into the new data­base:

mysql -u root -p --default-character-set=utf8
newdb_utf8 < dump.schema.sql;
mysql -u root -p --default-character-set=utf8
newdb_utf8 < dump.data.sql;

Notes:

Some help­full hints

mysql­hot­copy:
Ref­er­ence Here
Usage: (to local filesys­tem)

mysqlhotcopy -u backups -p`cat ~backups/.db.shadow`
--addtodest --noindices old_database ~backups/dbs/

Usage: (to SCP)

mysqlhotcopy -u backups -p`cat ~backups/.db.shadow`
--addtodest --noindices
--method='scp -c arcfour -C -2' old_database backups@remotehost:~backups/dbs/

Series Nav­i­ga­tionMigrat­ing MySQL latin1 to utf8 – The process
Leave a Comment

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes