MySQL: CHARSET from latin1 to utf8 > MySQL Tip

본문 바로가기
 

MySQL: CHARSET from latin1 to utf8

페이지 정보

작성자 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 20,994회 작성일 08-01-01 18:47

본문

Wednesday, March 29, 2006

MySQL: CHARSET from latin1 to utf8

A website im supporting needs to have multilingual characters. The default character set for MySQL is latin1. This unfortunately will not support Chinese nor other wierd multibyte characters.

It will quietly support them, but returns gibberish and will cause frustration all round.

After digging around, the best character set to use is UTF8.
To set the default charset for the server, the my.cfg/my.ini file has to be modified:

default-character-set=utf8


Unfortunately, once a database and their tables have been defined as latin1, they remain as latin1 unless you run this for each database:

alter database mydatabase charset=utf8;


and for each table:
alter table mytable charset=utf8;


and for each varchar/char type column:
alter table mytable alter column mycol charset=utf8;


and repeat ad infinitum....

This is rather tedious and boring, so there should be a better way. And that is to dump out the sql, change the charset and dump it back in. Here is the script.


===== latin1ToUTF8.sh

echo Script to convert MySQL latin1 charsets to utf8.
echo Usage: $0 dbname
echo 060329 yky Created.

echo Dumping out $1 database
mysqldump --add-drop-table $1 > db.sql

mydate=`date +%y%m%d`
echo Making a backup
mkdir bak &> /dev/null
cp db.sql bak/$1.$mydate.sql

echo String replacing latin1 with utf8
cat db.sql | replace CHARSET=latin1 CHARSET=utf8 > db2.sql

echo Pumping back $1 into database
mysql $1 < db2.sql

echo Changing db charset to utf8
mysql $1 -e "alter database $1 charset=utf8;"

echo $1 Done!


======

There must be a better way ?!

yk.

5 lewsers:

Simos said...
It's better to use "iconv" to convert the existing database to UTF-8. It's possible that the database already contains bad characters, so these have to be removed.
Once you have a sane UTF-8 database, you can import.

iconv -f utf-8 -t utf-8 < olddatabase.sql

To check if the database is sane.
Yoon Kit said...
Here are more resources:

http://blog.firebright.com/2006/09/12/mysql-charset-latin1-to-utf8-and-back-again-seeking-xen-with-character-encoding/

where he rates my post a mere 4 of 10.

And rightly so, because this one is the ultimate resource:

http://www.oreillynet.com/onlamp/blog/2006/01/turning_mysql_data_in_latin1_t.html


yk.
Alf said...
Here's a better way...it worked for me in MySQL 5, not sure about older versions:

alter table TABLE_NAME convert to character set utf8;
EoD said...
Try:
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
(http://dev.mysql.com/doc/refman/5.0/en/alter-table.html)
eod said...
Oh, didn't mention alf.

Btw:
Converting from latin1 to utf8 caused some trouble with length of primary key (max 1000 byte) (since utf8 needs more space?)

댓글목록

등록된 댓글이 없습니다.

Total 243건 3 페이지
MySQL Tip 목록
번호 제목 글쓴이 조회 날짜
207 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 24749 10-29
206 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 34845 10-29
205 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 22304 10-29
204 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 13204 10-28
203 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 19668 10-27
202 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 21785 10-22
201 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 13136 09-12
200 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 14249 09-11
199 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 22471 07-08
198 no_profile 차동박 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 20954 07-08
197 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 19277 04-14
196 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 12569 04-14
195 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 12588 04-01
194 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 13281 01-05
열람중 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 20995 01-01
192 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 12619 12-18
191 no_profile 차동박 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 20220 11-06
190 no_profile human 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 28327 09-11
게시물 검색