5
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQLダンプデータをujis → utf8

Last updated at Posted at 2013-02-05

ujisなダンプデータの変換に関してです。
nkfでドカンでも良かったんだけど、

$ nkf -E -w hoge.sql > hoge_utf8.sql

構文エラーになりやがったので、面倒だけど以下手順でutf8に変換しました。

変換用データベースを用意し、ujis環境にリストア

my.cnfを修正。

[ujis ~] $ sudo vi /etc/my.cnf
my.cnf
[mysql]
default-character-set=ujis <<<<<<<<<<<<<<< ここ

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
default-character-set=ujis <<<<<<<<<<<<<<< ここ

# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

MySQL再起動する。

[ujis ~] $ sudo /etc/init.d/mysqld restart

リストア。

#ダンプデータにCREATE文が無ければデータベースを作る。
[ujis ~] $ mysql -u root -e "CREATE DATABASE hoge;"
[ujis ~] $ mysql -u root [hoge] < hoge.sql

全テーブルの文字コードをUTF8に変換

[ujis ~] $ mysql -u root hoge -e "ALTER TABLE fuga CHARSET=utf8;"
[ujis ~] $ mysql -u root hoge -e "ALTER TABLE fugo CHARSET=utf8;"
・・・

ダンプ実行

[ujis ~] $ mysqldump -u root hoge --database > hoge_utf8.sql

ダンプデータのujis系をutf8系に置換

ujis → utf8ujis_bin → utf8_bin を実行。

#確認
[ujis ~] $ grep "character set ujis\|collate ujis" hoge_utf8.sql
#問題無さそうならsedで置換、問題があればVimでコツコツ置換。
[ujis ~] $ sed -i -e 's/\(character set \|collate \)ujis/\1utf8/' hoge_utf8.sql

utf8環境のMySQLにリストア

[utf8 ~] $ mysql -u root < hoge_utf8.sql
5
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?