mysql set names 命令和 mysql 字元編碼問題
閱讀更多
轉自:https://www.cnblogs.com/digdeep/p/5228199.html
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 to specify the collation.
該變數設定的 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 DATABASE db_name
[[DEFAULT] CHARACTER SET charset_name]
[[DEFAULT] COLLATE collation_name]
ALTER DATABASE db_name
[[DEFAULT] CHARACTER SET charset_name]
[[DEFAULT] COLLATE collation_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' 的作用是一樣的。