幽灵资源网 Design By www.bzswh.com

MySQL是一款常用的开源数据库软件,但是对于初次使用者好像并不是太友好,MySQL5.x的版本中默认字符集是latin1也就是我们所知道的ISO-8859-1字符集,这个字符集编码并没有包含汉字,所以我们在使用的时候汉字会出现乱码情况,通过修改数据库默认字符集即可解决。

进入MySQL命令行:

mysql> show variables like '%colla%';
+----------------------+-------------------+
| Variable_name    | Value       |
+----------------------+-------------------+
| collation_connection | gbk_chinese_ci  |
| collation_database  | latin1_swedish_ci |
| collation_server   | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set, 1 warning (0.00 sec)

mysql> show variables like '%char%';
+--------------------------+---------------------------------------------------------+
| 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    | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)

可以看到现在默认的字符集是latin1

找到MySQL的配置文件,windows中的配置文件是my.ini,我的电脑位于C:\ProgramData\MySQL\MySQL Server 5.7,Linux中需要修改的是my.conf文件,具体路径根据你的实际安装位置,分别修改以下节点中的配置:

我使用的5.7中[client]节点的配置需要新增,其余两个节点[mysql]、[mysql]需要打开注释并修改为utf8.

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysql]
character_set_server=utf8

然后重启MySQL

net stop mysql
net start mysql

再次查看字符集,都已经被修改为utf8字符集

mysql> show variables like '%colla%';
+----------------------+-----------------+
| Variable_name    | Value      |
+----------------------+-----------------+
| collation_connection | gbk_chinese_ci |
| collation_database  | utf8_general_ci |
| collation_server   | utf8_general_ci |
+----------------------+-----------------+
3 rows in set, 1 warning (0.00 sec)

mysql> show variables like '%char%';
+--------------------------+---------------------------------------------------------+
| Variable_name      | Value                          |
+--------------------------+---------------------------------------------------------+
| character_set_client   | gbk                           |
| character_set_connection | gbk                           |
| character_set_database  | utf8                          |
| character_set_filesystem | binary                         |
| character_set_results  | gbk                           |
| 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)

为保证编码一致性还可以在创建数据库或者表的时候指定字符集,并在连接串后面指定连接参数:

?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
MySQL,乱码

幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com