继续昨天的问题,数据库配置好后,查询表发现中文乱码了,看了网上的几个方法也没解决。
感觉是导入的sql文件问题,那么逆向的思考,用命令创建个数据库,添加些数据,然后导出来看看什么情况。
向表内插入数据的时候发现报错:error 1366 (hy000): incorrect string value: '\xe6\xb5\x8b\xe8\xaf\x95' for column 'bookname' at row 1
这就奇怪了,查看表结构:show create table book;
看到了一个不想看到的编码格式latin1,果断改掉,通过命令:alter table book default character set utf8;
改完看到一个神清气爽的结果,表的编码改过来了,但还存在一个“什么鬼?”,字段有个乱码latin1 :
果断再改,通过命令:alter table book change bookname bookname varchar(32) character set utf8;
改完后也不看了,直接插数据试试:
insert into book (id,bookname,size,price) values (1,"测试",2,3) ;
插入成功,查看数据是不是还中文乱码,发现并不乱码了:
那么开始进行下一步,导出sql文件,进入到mysql的bin目录下,开始通过命令导出,这个过程需要输入密码
e:\mysql-5.7.28-winx64\bin>mysqldump -u root -p test > test.sql enter password: *******
导出的文件,表的编码格式utf8,与之前导入文件的比较并看不出什么问题。
那么,就是之前导入文件的过程,创建数据库的过程除了问题,查看之前导入的mydb.sql 的mydb数据库结构,果然不对:
mysql> use mydb; database changed mysql> show variables like 'character_set_database'; ------------------------ -------- | variable_name | value | ------------------------ -------- | character_set_database | latin1 | ------------------------ -------- 1 row in set, 1 warning (0.00 sec)
遂修改之:alert database mydb character set utf8;
然后再查看表的编码,发现有鬼:
mysql> show create table sp_user_cart; -------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | table | create table | -------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | sp_user_cart | create table `sp_user_cart` ( `cart_id` int(11) unsigned not null auto_increment comment '涓婚敭', `user_id` int(11) unsigned not null comment '瀛﹀憳id', `cart_info` text character set utf8mb4 collate utf8mb4_unicode_ci comment '璐?墿杞﹁?鎯呬俊鎭?紝浜岀淮鏁扮粍搴忓垪鍖栦俊鎭', `created_at` timestamp null default null, `updated_at` timestamp null default null, `delete_time` timestamp null default null, primary key (`cart_id`) ) engine=innodb auto_increment=22 default charset=utf8
utf8mb4_unicode_ci 这个东西是怎么出来的...
然后改了编码,查询,还是不对,想起来之前创建数据库,可能是忘记设置编码,导入的东西本来就乱码了,也或者是my.ini配置不对,从头开始用正确流程试试吧。
先查看数据库的编码设置:show variables like 'character%';
发现很混乱:
mysql> show variables like 'character%'; -------------------------- ---------------------------------------- | variable_name | value | -------------------------- ---------------------------------------- | character_set_client | gbk | | character_set_connection | gbk | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | gbk | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | d:\mysql-5.7.29-winx64\share\charsets\ | -------------------------- ----------------------------------------
开始改,通过一系列的setset character_set_client = utf8;
得到结果
-------------------------- ---------------------------------------- | variable_name | value | -------------------------- ---------------------------------------- | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | utf8 | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | d:\mysql-5.7.29-winx64\share\charsets\ | -------------------------- ----------------------------------------
创建数据库并制定编码:create database test2 character set utf8 collate utf8_general_ci;
使用:use test2
在导入数据库文件之前,制定编码set names utf8;
导入:source f:xxxx\xxxx\mydb.sql;
不算漫长的等待之后,查询,不乱码了
mysql> select * from sp_role; --------- --------------- --------------------------------------------------------------------------------------------- ------------------------------------------------------ ----------------------------------------------------------------- ------------------------ -------------------- | role_id | role_name | ps_ids | ps_ca | role_desc | --------- --------------- --------------------------------------------------------------------------------------------- ------------------------------------------------------ ----------------------------------------------------------------- ------------------------ -------------------- | 30 | 主管 | 101,0,104,116,115,142,143,144,121,122,123,149,102,107,109,103,111,129,130,134,135,138,139,14 0,141,112,147,125,110,131,132,133,136,137,145,146,148 | goods-index,goods-tianjia,category-index,order-showlist,brand-in dex | 技术负责人 | | 31 | 测试角色 | 101,0,104,105,116,117,115,142,143,144,121,122,123,149,103,111,129,134,138,112,147 | goods-showlist,goods-tianjia,category-showlist,order-showlist,or der-dayin,order-tianjia | 测试角色描述 | | 34 | 测试角色2 | 0,105,116,142,143,122 | null | 测试描述12 | | 39 | 大发送到 | 101,0,104,105,116 | null | 阿斯蒂芬 | | 40 | test | 102,0,107,109,154,155,145,146,148 | null
实战项目可以继续进行,遂生法喜。