MySQL是一款常用的开源数据库软件,但是对于初次使用者好像并不是太友好,MySQL5.x的版本中默认字符集是latin1也就是我们所知道的ISO-8859-1字符集,这个字符集编码并没有包含汉字,所以我们在使用的时候汉字会出现乱码情况,通过修改数据库默认字符集即可解决。
进入MySQL命令行:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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.
1
2
3
4
5
6
|
[client]
default - character - set =utf8
[mysql]
default - character - set =utf8
[mysql]
character_set_server=utf8
|
然后重启MySQL
net stop mysql
net start mysql
再次查看字符集,都已经被修改为utf8字符集
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/chsoul/p/13649826.html