云服务器MySQL数据库乱码如何解决?
一、我们需要将默认的字符集更改为utf-8来解决数据库导入数据的乱码问题:我们需要先查看数据库默认的字符集是什么:
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;三、导入数据库的方法Windows系统
source d:/table.sql(*.sql文件所处的盘位置)linux系统
source /home/hhh/桌面/tables.sql;
页:
[1]