Ubuntu下MySQL中文乱码解决

yhljxy 2013-06-09

安装mysql如下:

# apt-get install mysql-server

修改/etc/mysql/my.cnf
sudo gedit /etc/mysql/my.cnf

在my.cnf文件中的[client]段和 [mysqld]段加上以下两行内容:
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8

需要重启mysql服务
停用:
sudo mysqladmin shutdown -u root -p
然后输入mysql的root用户的密码
启用:
sudo mysqld_safe -user=mysql &

或者

/etc/init.d/mysql restart

查看一下现在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/ |
+--------------------------+----------------------------+

现在不管在mysql shell下还是JDBC都可以正常显示中文了。但之前的数据还是不行,所以在这之前得先备份数据,修改完编码后再导入。

相关推荐