bawdo.comへようこそ
Created: 2007-02-14 17:44:48

Convert a MySQL DB from utf8 to UTF8

C'mon utf-8 :-)

I had a utf8 collated MySQL 4.1 db that contained both English and Japanese text. I dumped the data and then tried to migrate it into my new MySQL 5.0.26 installation. Of course being a bit lazy I did not take the time to check things out properly and was not surprised to find that all Japanese text had become mojibake. I set off on a mission to fix the problem.

I had two options. Stick with utf8 or move to utf8. I chose to move to utf8 as the front end of my website is all in utf8 so making the whole thing utf8 from front to back would make sense. Here is how I did it:

Dumped the original db with the following command:

mysqldump -u root -p --opt --default-character-set=utf8 --skip-set-charset  DBNAME > DBNAME.sql

 

Then using sed I changed all occurrences of the word utf8 to utf8:

sed  -e 's/utf8/utf8/g' -i ./DBNAME.sql

 

From here I then created the new database and then imported the dumpfile.

mysql -p -e "create database DBNAME"

mysql -p --default-character-set=utf8  DBNAME < DBNAME.sql

And that's it.

Regards, Bawdo 

 


Comments

Bawdo

When converting your backend DB from utf8 to UTF-8 you may also need to ensure the front end connects to the DB using UTF-8.
This should happen if you have the appropriate settings made in you php.ini and my.cnf configuration files. However, on one of my sites things were not working out as they should have.To fix things I issued the following query to the database after the initial mysql_connect:

SET NAMES \'utf8\'

Regards, Bawdo.
Comment Left: 2007-02-19 14:49:30

USA

rxIg31 szdvnemelzhi, [url=http://slklyryfxnjh.com/]slklyryfxnjh[/url], [link=http://yeboirxjtexu.com/]yeboirxjtexu[/link], http://rsvcndbmcler.com/
Comment Left: 2010-09-07 09:17:37

Post a comment

Sorry this is required to help avoid spam.
You can use the following tags in your comments: <a> , <b> , <i> & <br>. Also note you don't really need to use any <br> tags as carriage returns will be automatically translated into <br> tags.

NOTE all fields are required.

Wikipedia Affiliate Button

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 2.5 License