今天在本地搭建环境之后,app端写入后台发现中文无法正确写入,全部显示为???,摸索了一会,设置过tomcat环境也设置过数据库字符集,结果还是不行,后来咨询大佬们,尝试如下:
先在数据库中查询字符编码:
show VARIABLES like 'char%';
结果如下:
+--------------------------+----------------------------------------+
| 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 | latin1 |
| character_set_system | utf8 |
| character_sets_dir | J:\mysql-5.7.16-winx64\share\charsets\ |
+--------------------------+----------------------------------------+
发现character_set_server
这是个叛徒!原因是MySQL的默认编码是拉丁,所以打开mysql的安装目录,找到my.ini,在[mysqld]最后下面加上
character_set_server=utf8
init_connect='SET NAMES utf8'
之后重启MySQL。
再查看一下编码,结果变为:
+--------------------------+----------------------------------------+
| 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 | J:\mysql-5.7.16-winx64\share\charsets\ |
敌军被干掉了,测试也一切正常了~