mysql set names 命令和 mysql 字符編碼問題
Reference: https://www.cnblogs.com/digdeep/p/5228199.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
The character set used by the server for storing identifiers. The value is always utf8
.
character_set_system 是系統元數據(字段名等)存儲時使用的編碼字符集,該字段和具體存儲的數據無關。總是固定不變的——utf8. 我們可以不去管它。
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
該變量設置的 server 級別的(mysqld級別的) 字符集。也就是說設置的是 一個 mysqld 的,所有字符最後存儲時,使用的編碼字符集。
默認值為 lantin1. 我們一般設置成:utf8、utf8mb4、gbk 等值。
一同設置的還有 server 級別的排序規則:
collation_server:
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 DATABASEdb_name
[[DEFAULT] CHARACTER SETcharset_name
] [[DEFAULT] COLLATEcollation_name
] ALTER DATABASEdb_name
[[DEFAULT] CHARACTER SETcharset_name
] [[DEFAULT] COLLATEcollation_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‘ 的作用是一樣的。
mysql set names 命令和 mysql 字符編碼問題