作者:多米音乐_35780104 | 来源:互联网 | 2023-09-18 10:24
一次建表时显示中文正常,建表后查询却是乱码。mysqlshowtables;+----------------+|Tables_in_test|+-------------
一次建表时显示中文正常,建表后查询却是乱码。
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| ????? |
| table1 |
+----------------+
2 rows in set (0.00 sec)
一般出现这种情况是因为客户端和服务端的编码类型不一致导致。
查看一些客户端和服务端的编码类型:
mysql> show variables like "%char%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
mysql>
从中可以看出服务端是utf8,而客户端是latin,临时设置一下客户端编码:
SET character_set_client=‘utf8‘;
SET character_set_cOnnection=‘utf8‘;
SET character_set_results=‘utf8‘;
再次查看一下表是否乱码:
mysql> show tables;
+-----------------+
| Tables_in_test |
+-----------------+
| 学生信息表 |
| table1 |
+-----------------+
2 rows in set (0.00 sec)
果然是编码不一致导致,解决:
1.如果是数据库编码不一致导致乱码,则需要修改数据库的编码类型:
show create database test;
alter database test character set utf8 COLLATE utf8_general_ci;
2.如果是客户端问题,则需编辑配置文件,添加如下一行:
[client]
default-character-set=utf8
本文出自 “小东哥” 博客,谢绝转载!
mysql查询时乱码解决办法