大家在使用数据库的时候,总会出现各种各样的编码问题,看了MySQL官方文档后,记录下一些MySQL的编码体系知识,如MySQL有那几层使用编码的地方,MySQL客户端和服务端交互时哪些环节涉及到的编码,和如何指定编码。
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" |
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.
表的列字段与客户端传来的语句进行比较时,会把客户端语句转成列对应编码再进行比较,这是因为列字段拥有更高优先级。
下面用一张图来大致描述下上面的内容(个人理解所画)
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 character_set_client = charset_name;SET character_set_results = charset_name;SET character_set_connection = charset_name;
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