修改mysql的配置文件,使數(shù)據(jù)庫(kù)與服務(wù)器操作系統(tǒng)的字符集設(shè)置一致。
vi /etc/my.cnf 設(shè)置(如果沒(méi)有發(fā)現(xiàn)這個(gè)文件,就新建1個(gè))
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
default-character-set=utf8 (增加的關(guān)鍵一句,使得數(shù)據(jù)庫(kù)缺省以u(píng)tf8存儲(chǔ))
當(dāng)然,修改后,要重啟數(shù)據(jù)庫(kù)。
再次用mysql -u root -p命令進(jìn)入數(shù)據(jù)庫(kù)系統(tǒng),用SHOW VARIABLES LIKE 'character_set_%';命令查看到如下內(nèi)容:
+--------------------------+-----------------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-----------------------------------------------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.1.20-beta-linux-i686-glibc23/share/mysql/charsets/ |
+--------------------------+-----------------------------------------------------------------------+
發(fā)現(xiàn)關(guān)鍵項(xiàng)目已經(jīng)用了utf8,但這樣還不夠,還要保證客戶(hù)端也是用utf8的字符集來(lái)操作的。
登錄的時(shí)候,要用以下命令:mysql --default-character-set=utf8 -u root -p
再次用SHOW VARIABLES LIKE 'character_set_%';命令查看,結(jié)果變成了:
+--------------------------+-----------------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-----------------------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.1.20-beta-linux-i686-glibc23/share/mysql/charsets/ |
+--------------------------+-----------------------------------------------------------------------+
這樣才能保證客戶(hù)端所發(fā)命令都是基于utf8格式的,比如說(shuō)建立數(shù)據(jù)庫(kù)和表,默認(rèn)就會(huì)以u(píng)tf8編碼,而無(wú)須再次指定。
需要說(shuō)明一下,如果在修改編碼之前已經(jīng)建好數(shù)據(jù)庫(kù),需要?jiǎng)h掉原來(lái),重新建立數(shù)據(jù)庫(kù)。