在使用mysql5.7时,会发现通过web端向数据库中写入中文后会出现乱码,但是在数据库中直接操作SQL语句插入数据后中文就显示正常,这个问题怎么解决呢?此处不对MySQL的数据编码过程和原理进行讲解,如果有兴趣的同学可以自己百度。
下面我们就直接使用如下操作解决:
一、打开mysql控制台,输入命令show variables like 'character%';
显示如下:
1
2
3
4
5
6
7
8
9
10
11
|
+ --------------------------+-------------------------------------------+ | Variable_name | Value | + --------------------------+-------------------------------------------+ | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | utf8 | | character_set_results | latin1 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | C:\MySQL\MySQL Server 5.0\share\charsets\ | + --------------------------+-------------------------------------------+ |
二、修改my.ini文件
1
2
3
4
5
6
7
8
|
[client] default - character - set =utf8 [mysqld] default -storage-engine=INNODB character - set -server=utf8 collation-server=utf8_general_ci |
三、重启后使用第一步的命令查看,如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
+ --------------------------+----------------------------+ | Variable_name | Value | + --------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | + --------------------------+----------------------------+ |
还有character_set_database和character_set_server的默认字符集还是latin1。
四、输入命令:set names utf8;
重启后,使用第一步命令查看:
1
2
3
4
5
6
7
8
9
10
11
12
|
+ --------------------------+------------------------------------------------+ | 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 | C:\wamp64\bin\mysql\mysql5.7.9\share\charsets\ | + --------------------------+------------------------------------------------+ |
除了character_set_filesystem是binary外,其他都是utf8了,
这样在web端也设置成utf8编码就可以正常输入数据库了。
以上所述是小编给大家介绍的解决Mysql5.7中文乱码的问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/wu_fei_li/article/details/62423056