11/23/2008

解决Ubuntu8.04中mysql中文乱码

昨天刚在笔记本上安装Ubuntu 8.04,按照步骤安装了apt的mysql数据库,然后将现有的开发数据库导入(基于UTF-8),发现浏览的时候出现乱码现象,所以解决办法:
1 确认Mysql的编码
通过客户端进入mysql,执行


mysql>show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

确认是编码问题
2 找到mysql的配置文件,修改/etc/mysql/my.cnf

sudo gedit /etc/mysql/my.cnf

在my.cnf文件中的[client]段和 [mysqld]段加上以下两行内容:

[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8

3 需要重启mysql服务

sudo /etc/init.d/mysql restart


4 查看一下现在mysql的编码

sudo mysql -u root -p

mysql>show variables like 'character%';
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+

5 编码正确,由于刚才导库的时候是基于错误编码的基础上作的,所以,会有编码的问题,因此,删除刚才导入的数据库,重新导入,打开页面,OK!

No comments: