先看下面的執行結果:html
(root@localhost)[(none)]mysql>show variables like 'character%'; +--------------------------+-------------------------------------------------------------+ | Variable_name | Value | +--------------------------+-------------------------------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8mb4 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql-5.6.26-linux-glibc2.5-i686/share/charsets/ | +--------------------------+-------------------------------------------------------------+ 8 rows in set (0.01 sec) (root@localhost)[(none)]mysql>set names gbk; Query OK, 0 rows affected (0.00 sec) (root@localhost)[(none)]mysql>show variables like 'character%'; +--------------------------+-------------------------------------------------------------+ | Variable_name | Value | +--------------------------+-------------------------------------------------------------+ | character_set_client | gbk | | character_set_connection | gbk | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | gbk | | character_set_server | utf8mb4 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql-5.6.26-linux-glibc2.5-i686/share/charsets/ | +--------------------------+-------------------------------------------------------------+ 8 rows in set (0.01 sec) (root@localhost)[(none)]mysql>set names utf8mb4; Query OK, 0 rows affected (0.00 sec) (root@localhost)[(none)]mysql>show variables like 'character%'; +--------------------------+-------------------------------------------------------------+ | Variable_name | Value | +--------------------------+-------------------------------------------------------------+ | character_set_client | utf8mb4 | | character_set_connection | utf8mb4 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | utf8mb4 | | character_set_server | utf8mb4 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql-5.6.26-linux-glibc2.5-i686/share/charsets/ | +--------------------------+-------------------------------------------------------------+ 8 rows in set (0.00 sec)
1. character_set_system
mysql
The character set used by the server for storing identifiers. The value is always utf8
.linux
character_set_system 是系統元數據(字段名等)存儲時使用的編碼字符集,該字段和具體存儲的數據無關。老是固定不變的——utf8. 咱們能夠不去管它。sql
2. character_set_server 數據庫
Use charset_name
as the default server character set. See Section 10.5, 「Character Set Configuration」. If you use this option to specify a nondefault character set, you should also use --collation-server
to specify the collation.session
該變量設置的 server 級別的(mysqld級別的) 字符集。也就是說設置的是 一個 mysqld 的,全部字符最後存儲時,使用的編碼字符集。ide
默認值爲 lantin1. 咱們通常設置成:utf八、utf8mb四、gbk 等值。this
一同設置的還有 server 級別的排序規則:編碼
collation_server:spa
utf8mb4_bin, utf8mb4_general_ci, utf8_bin, utf8_general_ci
ci 表明: casesensitive ignore 排序時不考慮大小寫;而 _bin 結尾的排序時考慮大小寫。
3. character_set_database
Every database has a database character set and a database collation. The CREATE DATABASE
and ALTER DATABASE
statements have optional clauses for specifying the database character set and collation:
CREATE DATABASE [[DEFAULT] CHARACTER SET ] [[DEFAULT] COLLATE ] ALTER DATABASE [[DEFAULT] CHARACTER SET ] [[DEFAULT] COLLATE ]db_namecharset_namecollation_namedb_namecharset_namecollation_name
character_set_database 是單個數據庫級別的 字符集設置,該參數容許咱們在同一個 mysqd 下面的不一樣的 database 使用不一樣的字符集。
好比:
create database db1 character set utf8mb4 collate utf8mb4_bin;
這就設置了 數據庫 級別的字符集。若是 create database 語句沒有 character 和 collate 參數,那麼他們會默認使用:
character_set_server 和 character_collation 的值做爲 默認值。
一樣對應有數據庫級別的排序規則參數:
collation_database
4. character_set_client
The character set for statements that arrive from the client. The session value of this variable is set using the character set requested by the client when the client connects to the server. (Many clients support a --default-character-set
option to enable this character set to be specified explicitly. See also Section 10.1.4, 「Connection Character Sets and Collations」.)
也就是 mysql client 發送 給 mysqld 的語句使用的 編碼字符集。
可使用 --default-character-set 參數來顯示設置。
5. character_set_connection
The character set used for literals that do not have a character set introducer and for number-to-string conversion.
數字到字符轉換時的編碼字符集。
(用introducer指定文本字符串的字符集:
– 格式爲:[_charset] 'string' [COLLATE collation]
– 例如:
• SELECT _latin1 'string';
• SELECT _utf8 '你好' COLLATE utf8_general_ci;
– 由introducer修飾的文本字符串在請求過程當中不通過多餘的轉碼,直接轉換爲內部字符集處理。 )
實際中咱們通常沒有人去使用 introducer ,因此實際上是沒有 introducer,因此都會使用 character_set_connection來編碼的。
6. character_set_results
The character set used for returning query results such as result sets or error messages to the client.
mysqld 在返回 查詢 結果集 或者錯誤信息到 client 時,使用的編碼字符集。
7. set names 'xxx' 命令
能夠看到改變的是 character_set_client、character_set_connection、character_set_results
它們都是和 client 相關的。而 真正server端的編碼字符集,character_set_server 和 character_set_database ,set names 'xxx' 根本沒法修改。
set names 'xxx' 命令可使 character_set_client、character_set_connection、character_set_results 三者統一:
client (character_set_client) -----> character_set_connection -------> mysqld ------> client(character_set_results)
減小編碼轉換的須要。
8. character_set_server 和 character_set_database
兩者 的做用實際上是相同的,都是設置 字符最終存儲到磁盤時,使用的編碼字符集。只不過 兩者設置的級別不同而已。character_set_server 設置了 mysqld 級別的存儲編碼字符集,而character_set_database設置 mysqld 中單個 database 的存儲編碼字符集。並且character_set_database的默認值就是 character_set_server 的值。
存在三次編碼轉換過程:
1)mysql client 使用 character_set_client編碼的字符------> character_set_connection 編碼字符
------> mysqld :這裏須要從 character_set_connection 編碼格式二進制流解碼成 字符,而後使用 character_set_server/character_set_database 對字符進行再次編碼,生成二進制流,存儲時,就是存儲再次編碼的二進制流數據。
2)讀取數據時,會使用 character_set_server/character_set_database 對讀取到的二級制流進行 解碼成 字符,而後使用 character_set_results 對字符進行二次編碼,生成二進制流,發給 mysql client.
因此 使用 set names 'xxx' 命令,結合 character_set_server 參數,能夠將 整個過程的 字符集設置成相同的,就不會存在編碼轉換的過程。
9. default-character-set = charset_name 配置參數
Use charset_name
as the default character set for the client and connection(其實還有 character_set_results).
A common issue that can occur when the operating system uses utf8
or another multibyte character set is that output from the mysql client is formatted incorrectly, due to the fact that the MySQL client uses the latin1
character set by default. You can usually fix such issues by using this option to force the client to use the system character set instead.
See Section 10.5, 「Character Set Configuration」, for more information.
default-character-set 可以同時指定 client 端 和 connection 的字符,也就是:character_set_client 和 character_set_connection的值,實際上還設置了 character-set-results 的值。
因此 default-character-set 的做用和 set names 'xxx' 的做用是同樣的。