mysql 字符集全流程分析(一)

讨论环境

  1. windows10
  2. mysql5.7.24
  3. powershell

从客户端连接到 mysql 服务器角度

powershell 终端用 mysql 客户端连接 mysql 服务器

查看 powershell 使用的字符集

在这里插入图片描述

活动代码页: 936 # 请注意活动代码页这是 gbk
936
PS C:\Users\young> mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

由上述连接可知 mysql 客户端使用的字符集是 gb2312(也就是活动页是 936)。① 这就是本次这个 mysql 客户端使用 字符集

mysql 连接 mysqld 数据包分析

mysql login 流程(抓包查看)

在这里插入图片描述(数据包交互流程)
在这里插入图片描述

查看第 4 个 Server Greeting 数据包

在这里插入图片描述

可以看到 mysql 客户端和 mysql 服务器建立 TCP 连接后, mysql 服务器主动发起 Server Greeting,在这个数据包里,说明了 mysql 服务器使用的字符集。

如何查看 mysql 服务器使用的字符集
mysql> show variables like '%server';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| character_set_server | latin1            |  # mysql 服务器启动时使用的字符集
| collation_server     | latin1_swedish_ci |  # 使用的字符集采用的排序规则。
+----------------------+-------------------+
2 rows in set, 1 warning (0.00 sec)
查看第 6 个 Login Request 数据包

在这里插入图片描述

可以看到这个请求把 powershell 终端,也就是 mysql 客户端 使用的字符集告知了 mysql 服务器。这样服务器就可以根据这个字符集,设置字符集相关的变量了。

mysql 服务器哪些字符设置和 mysql 客户端使用的字符集有关?
mysql> show variables like 'character%';
+--------------------------+---------------------------------------------------------+
| Variable_name            | Value                                                   |
+--------------------------+---------------------------------------------------------+
| character_set_client     | gbk    (注: mysql 客户端使用的字符集)                                                 |
| character_set_connection | gbk    (注: mysql 服务器处理请求用到的字符集)                                            |
| character_set_database   | latin1                                                  |
| character_set_filesystem | binary                                                  |
| character_set_results    | gbk     (注: mysql 服务器响应使用的字符集)                                                |
| 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)

可以看到服务器根据 mysql 客户端使用的字符集 gbk 设置了 character_set_client 这个 Session 级别的变量。

#### 啥是 Session 级别的变量?
character_set_client 这个就是 session 级别的变量。因为不同的 mysql 终端使用的字符集可能并不一样。

举例说明
Active code page: 65001  # 使用 chcp 65001 将 powershell 使用的字符集调为 utf-8
PS C:\Users\young> mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like 'character%';
+--------------------------+---------------------------------------------------------+
| Variable_name            | Value                                                   |
+--------------------------+---------------------------------------------------------+
| character_set_client     | utf8    (1)                                                |
| character_set_connection | utf8    (2)                                              |
| character_set_database   | latin1                                                  |
| character_set_filesystem | binary                                                  |
| character_set_results    | utf8    (3)                                              |
| 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)

可以看到 powershell 调整是使用的字符集为 utf-8 后,相应地,character_set_client 这个变量也设置为了 utf8。这就是 Session 级别的变量。

References

  1. windows-code-page
  2. 彻底理解 mysql 字符乱码
  3. mysql-login-gb2312
  4. Code_page_936_(Microsoft_Windows) gbk 和 gb2312 这两个可以互换
  5. mysql-login-utf-8
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值