10.1.5 Connection Character Sets and Collations
10.1.5 Connection Character Sets and Collations
Several character set and collation system variables relate to a client's interaction with the server. Some of these have been mentioned in earlier sections:
The server character set and collation are the values of the
character_set_serverandcollation_serversystem variables.The character set and collation of the default database are the values of the
character_set_databaseandcollation_databasesystem variables.
Additional 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.
A “connection” is what you make when you connect to the server. The client sends SQL statements, such as queries, over the connection to the server. The server sends responses, such as result sets or error messages, over the connection back to the client. This leads to several questions about character set and collation handling for client connections, each of which can be answered in terms of system variables:
What character set is the statement in when it leaves the client?
The server takes the
character_set_clientsystem variable to be the character set in which statements are sent by the client.What character set should the server translate a statement to after receiving it?
For this, the server uses the
character_set_connectionandcollation_connectionsystem variables. It converts statements sent by the client fromcharacter_set_clienttocharacter_set_connection(except for string literals that have an introducer such as_latin1or_utf8).collation_connectionis important for comparisons of literal strings. For comparisons of strings with column values,collation_connectiondoes not matter because columns have their own collation, which has a higher collation precedence.What character set should the server translate to before shipping result sets or error messages back to the client?
The
character_set_resultssystem variable indicates the character set in which the server returns query results to the client. This includes result data such as column values, and result metadata such as column names and error messages.
Clients can fine-tune the settings for these variables, or depend on the defaults (in which case, you can skip the rest of this section). If you do not use the defaults, you must change the character settings for each connection to the server.
Two statements affect the connection-related character set variables as a group:
SET NAMES 'charset_name' [COLLATE 'collation_name']SET NAMESindicates what character set the client will use to send SQL statements to the server. Thus,SET NAMES 'cp1251'tells the server, “future incoming messages from this client are in character setcp1251.” It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use aSELECTstatement.)A
SET NAMES 'statement is equivalent to these three statements:charset_name'SET character_set_client =
charset_name;
SET character_set_results =charset_name;
SET character_set_connection =charset_name;Setting
character_set_connectiontocharset_namealso implicitly setscollation_connectionto the default collation forcharset_name. It is unnecessary to set that collation explicitly. To specify a particular collation, use the optionalCOLLATEclause:SET NAMES '
charset_name' COLLATE 'collation_name'SET CHARACTER SETcharset_nameSET CHARACTER SETis similar toSET NAMESbut setscharacter_set_connectionandcollation_connectiontocharacter_set_databaseandcollation_database. ASET CHARACTER SETstatement is equivalent to these three statements:charset_nameSET character_set_client =
charset_name;
SET character_set_results =charset_name;
SET collation_connection = @@collation_database;Setting
collation_connectionalso implicitly setscharacter_set_connectionto the character set associated with the collation (equivalent to executingSET character_set_connection = @@character_set_database). It is unnecessary to setcharacter_set_connectionexplicitly.
ucs2, utf16, utf16le, and utf32 cannot be used as a client character set, which means that they do not work for SET NAMES or SET CHARACTER SET.
The MySQL client programs mysql, mysqladmin, mysqlcheck, mysqlimport, and mysqlshow determine the default character set to use as follows:
In the absence of other information, the programs use the compiled-in default character set, usually
latin1.The programs can autodetect which character set to use based on the operating system setting, such as the value of the
LANGorLC_ALLlocale environment variable on Unix systems or the code page setting on Windows systems. For systems on which the locale is available from the OS, the client uses it to set the default character set rather than using the compiled-in default. For example, settingLANGtoru_RU.KOI8-Rcauses thekoi8rcharacter set to be used. Thus, users can configure the locale in their environment for use by MySQL clients.The OS character set is mapped to the closest MySQL character set if there is no exact match. If the client does not support the matching character set, it uses the compiled-in default. For example,
ucs2is not supported as a connection character set.C applications can use character set autodetection based on the OS setting by invoking
mysql_options()as follows before connecting to the server:mysql_options(mysql,
MYSQL_SET_CHARSET_NAME,
MYSQL_AUTODETECT_CHARSET_NAME);The programs support a
--default-character-setoption, which enables users to specify the character set explicitly to override whatever default the client otherwise determines.
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.
With the mysql client, to use a character set different from the default, you could explicitly execute SET NAMES every time you start up. To accomplish the same result more easily, add the --default-character-set option setting to yourmysql command line or in your option file. For example, the following option file setting changes the three connection-related character set variables set to koi8r each time you invoke mysql:
[mysql]
default-character-set=koi8r
If you are using the mysql client with auto-reconnect enabled (which is not recommended), it is preferable to use the charset command rather than SET NAMES. For example:
mysql>charset utf8
Charset changed
The charset command issues a SET NAMES statement, and also changes the default character set that mysql uses when it reconnects after the connection has dropped.
Example: Suppose that column1 is defined as CHAR(5) CHARACTER SET latin2. If you do not say SET NAMES or SET CHARACTER SET, then for SELECT column1 FROM t, the server sends back all the values for column1 using the character set that the client specified when it connected. On the other hand, if you say SET NAMES 'latin1' or SET CHARACTER SET latin1 before issuing the SELECT statement, the server converts the latin2 values to latin1 just before sending results back. Conversion may be lossy if there are characters that are not in both character sets.
If you want the server to perform no conversion of result sets or error messages, set character_set_results to NULL or binary:
SET character_set_results = NULL;
To see the values of the character set and collation system variables that apply to your connection, use these statements:
SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';
You must also consider the environment within which your MySQL applications execute. See Section 10.1.6, “Configuring the Character Set and Collation for Applications”.
http://dev.mysql.com/doc/refman/5.6/en/charset-connection.html
10.1.5 Connection Character Sets and Collations的更多相关文章
- MySQL: Connection Character Sets and Collations
character_set_server collation_servercharacter_set_databasecollation_database character_set_clientch ...
- Firebird Character Sets and Collations
Firebird Character Sets and Collations Every CHAR or VARCHAR field can (or, better: must) have a cha ...
- 02:PostgreSQL Character Sets
在利用postGIS导入shapefile文件到postgresql数据库的时候,老是提示字符串的问题,或者是乱码,试了好几种都不行,于是度娘之.... 使用默认的UTF8,提示信息是:建议使用LAT ...
- docker登录报错Error response from daemon: Get https://192.168.30.10/v1/users/: dial tcp 192.168.30.10:443: connect: connection refused
背景描述: 登录docker报错: [root@localhost sysconfig]# docker login 192.168.30.10 Username (newcs06): newcs06 ...
- Character Sets: Migrating to utf8mb4 with pt_online_schema_change
David Berube | June 12, 2018 | Posted In: MySQL Modern applications often feature the use of data ...
- 10 Quality Free Flat Icon Sets for Your Designs
Subscribe It’s clear that flat design has gained great popularity in recent years. This is hardly su ...
- Character Sets, Collation, Unicode :: utf8_unicode_ci vs utf8_general_ci
w Hi, You can check and compare sort orders provided by these two collations here: http://www.collat ...
- mysql set names 命令和 mysql 字符编码问题
先看下面的执行结果: (root@localhost)[(none)]mysql>show variables like 'character%'; +--------------------- ...
- mysql set names 命令和 mysql字符编码问题
先看下面的执行结果: (root@localhost)[(none)]mysql>show variables like 'character%'; +--------------------- ...
随机推荐
- Android开发多线程断点续传下载器
使用多线程断点续传下载器在下载的时候多个线程并发可以占用服务器端更多资源,从而加快下载速度,在下载过程中记录每个线程已拷贝数据的数量,如果下载中断,比如无信号断线.电量不足等情况下,这就需要使用到断点 ...
- iOS 9之New System Fonts(San Francisco 字体)
金田 此次苹果发布的iOS 9系统测试版目前已经开放下载,新系统将弃用Helvetica字体,改用了San Francisco字体, 包括系统菜单.App名称等各个部分. 最初San Francisc ...
- Android Animations简介
一 .Animations简介 Animations提供了一系列的动画效果,这些效果可以应用于绝大多数的控件: 二.Animations的分类 第一类:TweenedAnimations,该类Anim ...
- struct2(四)编写Struct2 的Action
简介: 1.映射一个Action到一个类上面 2.把结果返回到view展示 3.编写Action对应的控制逻辑 1. Action Mapping <action name="he ...
- HDU 4274 Spy's Work (树 DFS)
给定N个点,每个点都有一个唯一的前驱结点(点1为大boss),每个点的实际权值是子节点的求和值.现在给出某些点的权值的估算(> , = , < ),问这些估算是否会有冲突,现在保证每个点的 ...
- c语言typedef运用与函数指针
#include <stdio.h> #include <stdlib.h> #define PINT int * typedef short* PSHORT; //typed ...
- Laravel-路由-控制器
(慕课网_轻松学会Laravel-基础篇_天秤vs永恒老师) 一.基础路由 二.多请求路由 三.参数路由 四.路由别名 生成url可以使用别名 五.路由群组 六.路由输出视图 七.控制器参数绑定
- 为MyEclipse加入自己定义凝视
非常多时候我们默认的MyEclipse的类凝视是这种,例如以下图 能够通过改动MyEclipse的凝视规则来改变,不但能够改动类的.还能够改动字段.方法等凝视规则,操作方法例如以下 1.针对方法的凝视 ...
- App上线流程全攻略(续)-iOS8之后的改动与所遇日常错误
随着iOS8的公布,iTunes Connect的界面也是发生了非常大的改变,App 上传到 Store上面的步骤也是发生了些改变.以下继续用图说话: /*********************** ...
- Hibernate自增列保存失败的问题
author: hiu 更正说明:今天(2014-07-07)才发现的问题,我把@Id设置在了实体类中的id中,@Id是主键,应该设置在实体类的keyjobno中,之前发的文章可能误导了大家,如今更正 ...