mysql中文乱码解决方法:将mysql数据库编码统一utf8
1、 查看数据库编码:show variables like 'char%';
2、编辑/etc/my.cnf 文件
vim /etc/my.cnf
编辑之前的文件:
编辑后的文件内容:
[mysql]
default-character-set=utf8
#
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#
default-character-set = utf8
character_set_server = utf8
#lower_case_table_names=1 忽略大小写
#
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#
#
default-character-set = utf8
[mysql.server]
default-character-set = utf8
[client]
default-character-set = utf8
3、修改完成后,service mysqld restart重启mysql服务就生效。注意:[mysqld]字段与[mysql]字段是有区别的。
4、执行 show variables like 'character%';跟下面一样说明修改成功
5、如果上面的都修改了还乱码,那剩下问题就一定在connection连接层上。解决方法是在发送查询前执行一下下面这句(直接写在SQL文件的最前面):
SET NAMES 'utf8';
它相当于下面的三句指令:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;