Pages

Saturday, 24 November 2012

MySQL 5中文编码快速设置

这里的中文编码设置实际上就是设置UTF-8编码,这样所有的语言都能正常在MySQL存储和读取。本文是针对Linux平台的,Windows平台可以直接运行配置向导,然后选择UTF-8编码即可。
在Linux平台,MySQL 5的配置文件位于/etc/my.cnf或者/etc/mysql/my.cnf,打开该文件,在[client]后添加:
default-character-set=utf8
在[mysqld]后添加:
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
default-character-set=utf8 # 注:MySQL 5.5不支持该选项,应注释掉
然后重启MySQL,进入命令行模式查看:
mysql> use mydatabase;
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| 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/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
如果显示的编码均为utf8则大功告成!