mysql 中文乱码解决

Posted by wxianfeng Tue, 19 Oct 2010 21:11:00 GMT

环境:centos 5.5 + mysql 5.0

最近给我的blog生成了pdf文档,发现上了服务器后生成的pdf中文都是乱码,本地都是好的,后来发现我进入mysql 的console中select出来的中文都是乱码,解决之:

查看字符编码:

mysql> SHOW VARIABLES LIKE 'charac%';
+--------------------------+----------------------------+
| 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)

这个是正确的设置,原来查看出来的是latin1,即mysql自带的编码集,发现rails中只要你database.yml中设置了encoding:utf8就可以了,哪怕mysql的character_set_*是latin1,那么你存入数据库中的中文也不是乱码,so,rails中设置encoding非常之重要

那么如何解决mysql console中乱码,修改配置文件:

>vim /etc/my.cnf

[mysqld]
default-character-set = utf8

[client]
default-character-set = utf8

mysqld 和 clcient 区域中配置上面的两句即可

然后重启mysql

如果出现 unknown variable 'default-character-set=utf8' ,可能是 mysql版本是 5.5 以上, 在 mysqld 部分改为:

character_set_server=utf8

This entry was posted on Tue, 19 Oct 2010 21:11:00 GMT and Posted in . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.

Tags


Trackbacks

Use the following link to trackback from your own site:
http://wxianfeng.com/trackbacks?article_id=83

Comments

Leave a comment