我们需要先查看数据库默认的字符集是什么:
show variables like 'character_set_%'
查看所有的默认字符集
mysql> show variables like 'character_set_%';+--------------------------+---------------------------------------------------------+| Variable_name | Value |+--------------------------+---------------------------------------------------------+| character_set_client | utf8 || character_set_connection | utf8 || character_set_database | latin1 || character_set_filesystem | binary || character_set_results | utf8 || character_set_server | latin1 || character_set_system | utf8 || character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ |+--------------------------+---------------------------------------------------------+8 rows in set, 1 warning (0.01 sec)
其中character_set_database和character_set_server的字符集mysql默认为latin1.
我们需要将其更改为utf-8的字符需要用到下面两行代码:
set character_set_database=utf8;set character_set_server=utf8;
然后我们做字符集查询
show variables like 'character_set_%'
得到:
mysql> show variables like 'character_set_%';+--------------------------+---------------------------------------------------------+| 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 | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ |+--------------------------+---------------------------------------------------------+8 rows in set, 1 warning (0.00 sec)
之后创建的所有的数据库的字符集都默认为utf-8的字符集 .这个时候我们再去做字符集的插入时就可以看到汉字被成功插入不会再出现乱码问题.
alter database 数据库名 charset utf8;alter table 数据表名 convert to character set utf8;
source d:/table.sql(*.sql文件所处的盘位置)
source /home/hhh/桌面/tables.sql;
Copyright © 2013-2021 8a.hk All Rights Reserved. 八艾云 版权所有 中山市八艾云计算有限公司 粤ICP备14095776号