Table of Contents
Windows客户端
数据传输流程
Linux客户端
乱码陷阱模拟
乱码分析
乱码“修复”
Home Database Mysql Tutorial MySQL的字符编码体系(二)数据传输编码_MySQL

MySQL的字符编码体系(二)数据传输编码_MySQL

May 31, 2016 am 08:46 AM

MySQL的字符编码体系可以分成两部分:一部分是关于数据库服务器本身存储数据表时如何管理字符数据的编码;另一部分是关于客户端与数据库服务器传输数据如何编码。上一篇 MySQL的字符编码体系(一)——数据存储编码 讨论了数据存储编码,本篇讨论数据传输编码。

MySQL的客户端可以分为两种:一种就是用C语言写的官方客户端——MySQL命令程序;一种就是平常程序员使用JDBC等connector API写成的客户端。这里只讨论第一种。

Windows客户端

MySQL命令程序在Windows和Linux系统中关于字符编码处理的部分并不等效,下图是Windows系统的客户端字符编码转换逻辑:

其中的三个character变量存在于服务器上,而charset_info存在于客户端。

当客户端启动连接到服务器时,客户端将根据配置参数设置charset_info为指定编码,同时通知服务器让服务器把三个character变量设置为相同编码。

数据传输流程

  1. 客户端从控制台标准输入读取一行命令文本,其编码为操作系统编码;
  2. 客户端将命令从系统编码转码为客户端charset_info变量设定的编码;
  3. 客户端将命令文本发送给服务器;
  4. 服务器把收到的文本解码为character_set_client编码,这个编码通常与客户端charset_info一致;
  5. 服务器把命令文本转码为character_set_connection;
  6. 服务器执行命令,产生结果;
  7. 将结果转码为character_set_results发送给客户端;
  8. 客户端把收到的结果解码为charset_info编码,这个编码通常与character_set_results一致;
  9. 客户端将结果转码为操作系统编码,输出到控制台标准输出。

由于在Windows平台上MySQL程序在读取控制台时使用了Unicode Console Read API,所以程序从控制台获取的原始字符串实际上是UTF16编码,所以这里的“操作系统编码”并不是Windows通常的GBK,而应该看做UTF16。

Linux客户端

下图是Linux系统中的MySQL客户端程序字符编码转换逻辑:

它与Windows版的不同之处就在于,它并不把来自终端标准输入的操作系统编码字符串强制转换为charset_info编码,也不会把输出到终端的charset_info编码结果字符串强制转换为操作系统编码。也就是说,Linux平台的MySQL程序这时候会会忽略charset_info变量。当然,这样一来Linux客户端的数据传输流程就比Windows客户端对应地少几步。

乱码陷阱模拟

根据Linux平台MySQL程序的这一特点,很容易产生这样一个可能的陷阱:在Linux系统中通过MySQL客户端向数据库插入中文数据后,查询结果没有乱码,但从配置正确的Windows平台MySQL客户端查询同一个表得到的却是乱码。

可以这样模拟上述的情况:

创建一个表,其中只包含一个GBK字符串字段和UTF8字符串字段。Linux中启动MySQL连接到数据库服务器,将服务器的三个character变量从默认的UTF8修改为GBK。向数据库插入中文数据,立即select,结果无异常:

但是使用Windows的MySQL客户端查询时,结果却是乱码:

乱码分析

结合前面的数据传输流程,就能知道问题出在什么地方:

  1. 客户端从终端读取了一行utf8编码(Linux默认)的命令文本,忽略charset_info变量,直接把文本发送给服务器;
  2. 服务器因为事先的命令charset gbk把三个character变量都设置为了GBK,所以服务器认为收到的文本是GBK编码;
  3. 接下来服务器会不经过任何转码将文本字符串直接存入数据表中,因为数据表第一个字段也是GBK。

到这里为止,数据表中存了一个UTF8字符串,而服务器却当它是GBK,在同一个Linux客户端查询时:

  1. 表中的字符串不经过任何转码直接发给客户端,因为character_set_results也是GBK;
  2. 客户端收到查询结果后因为忽略charset_info而直接不经过转码输出到终端标准输出;
  3. 终端得到的数据实际上是UTF8编码的,所以正常输出。

在Windows客户端查询时:

  1. 表中的字符串(UTF8)不经过任何转码直接发给客户端,因为character_set_results也是GBK;
  2. 客户端收到查询结果后认为是charset_info编码(此时为GBK);
  3. 客户端把查询结果从charset_info转码为UTF16,然后调用Unicode Console Write API输出,看到乱码。

乱码“修复”

如果Windows客户端也想看到正确的结果,那就要故意错误地配置:

  1. 执行命令charset utf8,这会将charset_info和三个服务器character都设置为UTF8;
  2. 执行命令set names gbk,这只会将三个服务器character设置为GBK;
  3. 现在select,结果看上去不再乱码了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Explain the role of InnoDB redo logs and undo logs. Explain the role of InnoDB redo logs and undo logs. Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

How does MySQL index cardinality affect query performance? How does MySQL index cardinality affect query performance? Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: From Small Businesses to Large Enterprises MySQL: From Small Businesses to Large Enterprises Apr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

See all articles