第一步先在终端输入一下指令查看数据库的字符串编码格式:
- show variables like'character_set_%';
- 查看数据库编码格式是不是以下形式
- 注意:
- 不能在终端直接用指令SET character_set_server = utf8修改,这样改再次连接数据库的时候将会恢复成默认值。
![1f716f5f1c4aa3b974b623b5065a46bd.png](https://i-blog.csdnimg.cn/blog_migrate/60bdfc42ace3b6e6ff79d2eb1d962b57.jpeg)
图例1
如果是接着看第二步,如果不是跳转到第三步;
第二步输入以下指令查看列表的编码格式
- show create table accounts ;
![78639266efd00d1edea6cd514b507238.png](https://i-blog.csdnimg.cn/blog_migrate/4dc789cce95807cd09b23cc767916251.jpeg)
图例2
注意 name 的编码仍然为 latin1,虽然此表的编码是utf8。因为编码不一致导致中文无法显示因此,需要修改该列的编码格式:输入alter table accounts change name name varchar (7) character set utf8;
![6641bc1de33bbfc193c35857d3690ca4.png](https://i-blog.csdnimg.cn/blog_migrate/331be2996a7950cf6866eea69383dae2.jpeg)
图例3
在查询语句之前先调用下set names utf8;这样大多中文乱码的问题就解决了。
第三步:在mysql的路径下,创建一个etc文件夹,添加、修改my.cnf文件
- 关闭mysql服务
![ba70bdb3e8a19045ac777f989d7232fa.png](https://i-blog.csdnimg.cn/blog_migrate/692141068882224a14bb45670f548175.jpeg)
- 创建etc文件夹,修改my.cnf文件
- [mysql]部分添加character-set-server=utf8
- 在[client]部分添加default-character-set=utf8,若没有client部分就自己写一个,然后保存退出。
# For advice on how to change settings please see# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html[client]default-character-set=utf8[mysqld]character-set-server=utf8collation-server=utf8_general_ci# Remove leading # and set to the amount of RAM for the most important data# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.# innodb_buffer_pool_size = 128M# Remove leading # to turn on a very important data integrity option: logging# changes to the binary log between backups.# log_bin# These are commonly set, remove the # and set as required.# basedir = .....# datadir = .....# port = .....# server_id = .....# socket = .....# Remove leading # to set options mainly useful for reporting servers.# The server defaults are faster for transactions and fast SELECTs.# Adjust sizes as needed, experiment to find the optimal values.# join_buffer_size = 128M# sort_buffer_size = 2M# read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
然后再返回到第一步,逐步对比。完成第一步和第二部配置后,打开mysql服务,打开终端测试,中文乱码问题解决。