MySQL查询结果乱码
现象:使用MySQL执行select命令后查到的结果如果含有汉字时会出现乱码,如下:
解决方法:
1、使用mysql命令登录MYSQL
mysql -hlocalhost -uroot -p
2、执行show variables like 'character%';获取字符集配置
show variables like 'character%';
3、修改/etc/mysql/my.cnf
vim /etc/mysql/my.cnf
[mysql]
default-character-set=utf8mb4
[mysqld]
character-set-server=utf8mb4
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
default-character-set=utf8mb4
[mysql.server]
default-character-set=utf8mb4
[client]
default-character-set=utf8mb4
4、使用命令service mysql restart重启MYSQL服务,然后使用show variables like 'character%';再看一次编码,发现已改变
service mysql restart
show variables like 'character%';
5、再重新执行select查询发现可以显示中文
6、查看字符集的命令
#查看MySQL数据库服务器和数据库字符集
show variables like 'character%';
#查看表中所有列的字符集
show full columns from tb_name;
#查看表的字符集
show table status from mysql_db like '%tb_name%'
#查看MySQL所支持的字符集
show charset;
7、修改字符集的命令
// 修改全局字符集
set character_set_connection=utf8;
set character_set_database=utf8;
set character_set_results=utf8;
set character_set_server=utf8;
set character_set_system=utf8;
set collation_connection=utf8;
set collation_database=utf8;
set collation_server=utf8;
// 修改表的字符集
alter table tb_name convert to character set utf8mb4;
// 修改字段字符集
alter table tb_name modify column tb_column varchar(30) character set utf8mb4 not null;
原文链接:https://blog.csdn.net/q283614346/article/details/84193650