0

0

MySQL数据库编码概要_MySQL

php中文网

php中文网

发布时间:2016-06-01 13:26:42

|

1566人浏览过

|

来源于php中文网

原创

bitsCN.com

大家在使用数据库的时候,总会出现各种各样的编码问题,看了mysql官方文档后,记录下一些mysql的编码体系知识,如mysql有那几层使用编码的地方,mysql客户端和服务端交互时哪些环节涉及到的编码,和如何指定编码。

基本概念:

PHP简约自动发卡平台个人版
PHP简约自动发卡平台个人版

PHP自动发卡平台个人版是采用php+mysql进行开发的自动发卡支付平台。服务器环境:PHP5.2以上版本mysql5.1 或以上版本安装说明:安装 http://你的域名/install.php 进行安装,后台路径http://你的域名/admin 后台账号:admin 后台密码:yc88.net需要修改用户名,可以进入数据库进行修改faka_users把admin改成其他支持改成中文

下载
mysql数据库编码层次:系统层,server层,database层,table层,column层,还有client,connection和result三种和客户端通讯相关的场景;A character set is a set of symbols and encodings. A collation is a set of rules for comparing characters in a character set;To connect to MySQL from Java you have to use the JDBC driver from MySQL. The MySQL JDBC driver is called MySQL Connector/J.MySQL 4.1以下对unicode支持不好jdbc3.0.16及以上才支持使用数据库本身编码,否则使用ISO8859-1在mysql控制台下输入show variables like 'character_set_%'; 查看当前编码相关系统变量,后面会解析其中几项
     mysql> SHOW VARIABLES LIKE 'character%';+--------------------------+---------------------------------+| Variable_name            | Value                           |+--------------------------+---------------------------------+| character_set_client     | latin1                          || character_set_connection | latin1                          || character_set_database   | latin1                          || character_set_filesystem | binary                          || character_set_results    | latin1                          || character_set_server     | latin1                          || character_set_system     | utf8                            || character_sets_dir       | D:"mysql-5.0.37"share"charsets" |


mysql的多层字符编码支持

1.server层

作用
整个数据库服务器默认编码。
配置点
通过系统变量character_set_server参数指定server层编码
可以在mysqld执行时加入该参数
或者在编辑mysql时候,设置该参数

2.database层

作用
数据库级别默认编码。
配置点
通过系统变量character_set_database参数指定database层编码
建表时候指定编码

3.table层

同理,table层的编码设置仅影响当前表的所有未指定编码的列的编码。但这个指定是mysql独有的,而且只能通过sql在建表或修改表时指定, 在标准sql中,没有可指定表编码的sql语法

4.column层

作用
设置列的编码。
配置点
建表或修改列时设置。这是标准sql语法。

mysql server与client交互时编码如何转换

1.客户端发送语句

character set and collation system variables are involved in handling traffic for the connection between a client and the server. Every client has connection-related character set and collation system variables.
The server takes the character_set_client system variable to be the character set in which statements are sent by the client.
在客户端和服务端通讯时,会涉及到另外几个编码设置相关的系统变量的,每个客户端都有属于自己的编码链接相关编码。
服务端使用系统变量character_set_client来处理客户端发来的语句。

2.服务端处理语句

The server uses the character_set_connection and collation_connection system variables. It converts statements sent by the client from character_set_client to character_set_connection (except for string literals that have an introducer such as _latin1 or _utf8)
服务端会把客户端发来的语句(以character_set_client 编码)转换为character_set_connection编码。

A character string literal may have an optional character set introducer and COLLATE clause [_charset_name]'string' [COLLATE collation_name]
如:SELECT _latin1'string' COLLATE latin1_danish_ci;
在缺少编码指定是,默认会使用character_set_connection指定的编码。
The character set used for literals that do not have a character set introducer and for number-to-string conversion.
没有前导编码修饰(introducer)的文本和数字到字符的转换会应用character_set_connection编码。

For comparisons of strings with column values, collation_connection does not matter because columns have their own collation, which has a higher collation precedence.
表的列字段与客户端传来的语句进行比较时,会把客户端语句转成列对应编码再进行比较,这是因为列字段拥有更高优先级。

3.服务端返回内容

The character_set_results system variable indicates the character set in which the server returns query results to the client
系统变量character_set_results用来把数据以该编码方式返回给客户端。

下面用一张图来大致描述下上面的内容(个人理解所画)
/

服务端如何自动判断并设置编码?

The character encoding between client and server is automatically detected upon connection. You specify the encoding on the server using the character_set_server for server versions 4.1.0 and newer, and character_set system variable for server versions older than 4.1.0. The driver automatically uses the encoding specified by the server.
如果客户端连接时没有提供编码(连接串无characterEncoding),则服务端会使用character_set_server变量来作为客户端编码(4.1.0后)。

For example, to use 4-byte UTF-8 character sets with Connector/J, configure the MySQL server with character_set_server=utf8mb4, and leave characterEncoding out of the Connector/J connection string. Connector/J will then autodetect the UTF-8 setting.

客户端如何制定编码?
To override the automatically detected encoding on the client side, use the characterEncoding property in the URL used to connect to the server.

When a client connects to the server, it sends the name of the character set that it wants to use. The server uses the name to set the character_set_client, character_set_results, and character_set_connection system variables. In effect, the server performs a SET NAMES operation using the character set name.
客户端与服务端建立链接时,会发送客户端所希望使用的编码集。服务端会用这个编码集去初始化三个系统变量character_set_client, character_set_results, and character_set_connection。如执行了语句 SET NAMES XXX一般:
SET NAMES xx可以指定connection编码为xx:character_set_connection,character_set_results,character_set_client 系统变量可修改;

SET NAMES 'charset_name' 这句SQL等同与执行下面3个语句:
SET character_set_client = charset_name;SET character_set_results = charset_name;SET character_set_connection = charset_name;

A SET CHARACTER SET charset_name 等同于执行下面3个语句:
SET character_set_client = charset_name;SET character_set_results = charset_name;SET collation_connection = @@collation_database;

参考

http://dev.mysql.com/doc/refman/5.5/en/charset.html

bitsCN.com

相关专题

更多
C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

9

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

25

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

18

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

19

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

10

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

11

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

7

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

30

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 7.6万人学习

Django 教程
Django 教程

共28课时 | 3.4万人学习

Excel 教程
Excel 教程

共162课时 | 13万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号