Convert MySQL Tables and Columns to UTF-8


If you are like me and have never had experience with an internationalized website before, you probably setup your mysql database with the default latin character set and already have a ton of data in it. Luckily all the data in my app was latin and we had not ventured into Spanish or Japanese yet so my varchar and text fields were all in normal ascii latin characters that converted perfectly to UTF-8.

I found this bash script to work great:
mysql --database=dbname -B -N -e "SHOW TABLES" | awk '{print "ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;"}' | mysql --database=dbname &

I did some research and found that utf8_general_ci is a legacy version that is slightly faster, but utf8_unicode_ci is the more robust collation.

Here’s the script’s source:
http://www.commandlinefu.com/commands/view/1575/convert-all-mysql-tables-and-fields-to-utf8

Not only did it convert the table default to UTF-8, but it also converted my varchar and text fields to that charset and collation as well.

I tried the following mysql conf settings with my Percona database:

[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
skip-character-set-client-handshake

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s