在使用mysql5.7时,会发现通过web端向数据库中写入中文后会出现乱码,但是在数据库中直接操作SQL语句插入数据后中文就显示正常,这个问题怎么解决呢?此处不对MySQL的数据编码过程和原理进行讲解,如果有兴趣的同学可以自己百度。
下面我们就直接使用如下操作解决:
一、打开mysql控制台,输入命令show variables like 'character%';
显示如下:
+--------------------------+-------------------------------------------+| 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文件
[client] default-character-set=utf8 [mysqld] default-storage-engine=INNODB character-set-server=utf8 collation-server=utf8_general_ci |
三、重启后使用第一步的命令查看,如下:
+--------------------------+----------------------------+| 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;
重启后,使用第一步命令查看:
+--------------------------+------------------------------------------------+| 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/ |+--------------------------+------------------------------------------------+ |