1.以root登陆,在终端输入命令 sudo gedit /etc/mysql/my.cnf在打开的文件中
找到[client]在下面加入
default-character-set=utf8
找到 [mysqld_safe]在下面加入
default-character-set=utf8
找到[mysqld]在下面加入,12.04以后
character-set-server=utf8
init_connect='SET NAMES utf8'
或者12.04以前
default-character-set=utf8
init_connect='SET NAMES utf8'
找到[mysql]在下面加入
default-character-set=utf8
2.重启mysql命令以便让你上面的修改生效
停用命令:sudo mysqladmin shutdown -u root -p
启用命令:sudo /etc/init.d/mysql restart
或者:sudo mysqld_safe -user=mysql &
3.查看现在mysql的编码
进入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/ |
+--------------------------+----------------------------+
4.原来建的库和表无法生效,还是乱码,不过新建的则可以正常显示中文!