Linux

MySQL4버전Latin1에서UTF8로 변환

아이티제어 2022. 3. 17. 10:07
  1. mysqldump 를 사용 하여 이전 데이터를 "latin1"으로 추출
  2. sed 를 사용 하여 덤프 파일에서 "latin1"을 "utf8"로 바꿉니다.
  3. 올바른 매개변수를 사용하여 새 데이터베이스 생성: 문자 집합 utf8 collate utf8_unicode_ci
  4. mysql --default-character-set=utf8 을 사용 하여 변환된 덤프를 새 데이터베이스로 파이프하십시오.
# Dump the old database as latin1, because ironically, mysqldump defaults to utf8.
mysqldump --default-character-set=latin1 db > db.dump

# If you need to convert a MySQL dump from one character set to another, use iconv.
iconv -f LATIN1 -t UTF-8 < db.dump > db.dump

# If you've been running mysqldump without parameters on a latin1 instance, you can convert the dump from UTF8 to latin1 to correct it.
iconv -f UTF-8 -t LATIN1 < db.dump > db.dump

# Rewrite the dump to say 'utf8' and 'utf8_unicode_ci' in all the right places.
sed -e 's/SET NAMES latin1/SET NAMES utf8/g' -i db.dump
sed -e 's/CHARSET=latin1/CHARSET=utf8 COLLATE=utf8_unicode_ci/g' -i db.dump

# Create a new database with the correct parameters.
create database db character set utf8 collate utf8_unicode_ci;
# Verify it.
show create database db;

# Pipe the converted database dump into MySQL.
mysql -h hostname --default-character-set=utf8 -u root -p db < db.dump