修改MySQL資料庫字元集

来源:https://www.cnblogs.com/aaron8219/archive/2018/07/13/9305546.html
-Advertisement-
Play Games

占座 ...


  Preface       I've demonstrated how to change character set in Oracle database in my previous blog.Now,I'm gonna do the similar operation in MySQL database,Let's see the difference of details.   Example
  Create a test table.
1 root@localhost:mysql3306.sock [zlm]>create table charset(
2     -> id int,
3     -> name varchar(10)
4     -> ) engine=innodb charset=utf8;
5 Query OK, 0 rows affected (0.01 sec)

 

Check the character set.

 1 root@localhost:mysql3306.sock [zlm]>\s
 2 --------------
 3 mysql  Ver 14.14 Distrib 5.7.21, for linux-glibc2.12 (x86_64) using  EditLine wrapper
 4 
 5 Connection id:        2
 6 Current database:    zlm
 7 Current user:        root@localhost
 8 SSL:            Not in use
 9 Current pager:        stdout
10 Using outfile:        ''
11 Using delimiter:    ;
12 Server version:        5.7.21-log MySQL Community Server (GPL)
13 Protocol version:    10
14 Connection:        Localhost via UNIX socket
15 Server characterset:    utf8
16 Db     characterset:    utf8
17 Client characterset:    utf8
18 Conn.  characterset:    utf8
19 UNIX socket:        /tmp/mysql3306.sock
20 Uptime:            29 min 38 sec

 

Insert a record contains Chinese characters into test table.

 1 root@localhost:mysql3306.sock [zlm]>insert into charset values(1,'黎明');
 2 Query OK, 1 row affected (0.00 sec)
 3 
 4 root@localhost:mysql3306.sock [zlm]>select * from charset;
 5 +------+--------+
 6 | id   | name   |
 7 +------+--------+
 8 |    1 | 黎明   |
 9 +------+--------+
10 1 row in set (0.00 sec)

 

Change the character from utf8 to to gbk.

 1 root@localhost:mysql3306.sock [zlm]>set @@global.character_set_database=gbk;
 2 Query OK, 0 rows affected, 1 warning (0.00 sec)
 3 
 4 root@localhost:mysql3306.sock [zlm]>set @@global.character_set_server=gbk;
 5 Query OK, 0 rows affected (0.00 sec)
 6 
 7 root@localhost:mysql3306.sock [zlm]>show global variables like 'character%';
 8 +--------------------------+----------------------------------------------------------------+
 9 | Variable_name            | Value                                                          |
10 +--------------------------+----------------------------------------------------------------+
11 | character_set_client     | utf8                                                           |
12 | character_set_connection | utf8                                                           |
13 | character_set_database   | gbk                                                            |
14 | character_set_filesystem | binary                                                         |
15 | character_set_results    | utf8                                                           |
16 | character_set_server     | gbk                                                            |
17 | character_set_system     | utf8                                                           |
18 | character_sets_dir       | /usr/local/mysql-5.7.21-linux-glibc2.12-x86_64/share/charsets/ |
19 +--------------------------+----------------------------------------------------------------+
20 8 rows in set (0.00 sec)
21 
22 root@localhost:mysql3306.sock [zlm]>show variables like 'character%';
23 +--------------------------+----------------------------------------------------------------+
24 | Variable_name            | Value                                                          |
25 +--------------------------+----------------------------------------------------------------+
26 | character_set_client     | utf8                                                           |
27 | character_set_connection | utf8                                                           |
28 | character_set_database   | utf8                                                           |
29 | character_set_filesystem | binary                                                         |
30 | character_set_results    | utf8                                                           |
31 | character_set_server     | utf8                                                           |
32 | character_set_system     | utf8                                                           |
33 | character_sets_dir       | /usr/local/mysql-5.7.21-linux-glibc2.12-x86_64/share/charsets/ |
34 +--------------------------+----------------------------------------------------------------+
35 8 rows in set (0.00 sec)

 

Start a new mysql client and check the data in test table.

 1 [root@zlm1 13:51:24 ~]
 2 #mysql
 3 Welcome to the MySQL monitor.  Commands end with ; or \g.
 4 Your MySQL connection id is 6
 5 Server version: 5.7.21-log MySQL Community Server (GPL)
 6 
 7 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 8 
 9 Oracle is a registered trademark of Oracle Corporation and/or its
10 affiliates. Other names may be trademarks of their respective
11 owners.
12 
13 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
14 
15 root@localhost:mysql3306.sock [(none)]>show variables like 'character%';
16 +--------------------------+----------------------------------------------------------------+
17 | Variable_name            | Value                                                          |
18 +--------------------------+----------------------------------------------------------------+
19 | character_set_client     | utf8                                                           |
20 | character_set_connection | utf8                                                           |
21 | character_set_database   | gbk                                                            |
22 | character_set_filesystem | binary                                                         |
23 | character_set_results    | utf8                                                           |
24 | character_set_server     | gbk                                                            |
25 | character_set_system     | utf8                                                           |
26 | character_sets_dir       | /usr/local/mysql-5.7.21-linux-glibc2.12-x86_64/share/charsets/ |
27 +--------------------------+----------------------------------------------------------------+
28 8 rows in set (0.00 sec)
29 
30 root@localhost:mysql3306.sock [(none)]>select * from charset;
31 ERROR 1046 (3D000): No database selected
32 root@localhost:mysql3306.sock [(none)]>use zlm //After execute "use database",the character set of database will turn into utf8 again.
33 Reading table information for completion of table and column names
34 You can turn off this feature to get a quicker startup with -A
35 
36 Database changed
37 root@localhost:mysql3306.sock [zlm]>select * from charset;
38 +------+--------+
39 | id   | name   |
40 +------+--------+
41 |    1 | 黎明   |
42 +------+--------+
43 1 row in set (0.00 sec)
44 
45 root@localhost:mysql3306.sock [zlm]>select length('黎明') from dual;
46 +------------------+
47 | length('黎明')   |
48 +------------------+
49 |                6 | //The length of one Chinese character occupys three bytes.It depends on the character set of table.
50 +------------------+
51 1 row in set (0.00 sec)
52 
53 //The data still shows correct result after change the database and server character set to gbk.
54 
55 root@localhost:mysql3306.sock [zlm]>\s
56 --------------
57 mysql  Ver 14.14 Distrib 5.7.21, for linux-glibc2.12 (x86_64) using  EditLine wrapper
58 
59 Connection id:        6
60 Current database:    zlm
61 Current user:        root@localhost
62 SSL:            Not in use
63 Current pager:        stdout
64 Using outfile:        ''
65 Using delimiter:    ;
66 Server version:        5.7.21-log MySQL Community Server (GPL)
67 Protocol version:    10
68 Connection:        Localhost via UNIX socket
69 Server characterset:    gbk
70 Db     characterset:    utf8 //The character set of database turns back to utf8.Therefore,no messy code appears.
71 Client characterset:    utf8
72 Conn.  characterset:    utf8
73 UNIX socket:        /tmp/mysql3306.sock
74 Uptime:            37 min 4 sec
75 
76 Threads: 2  Questions: 116  Slow queries: 0  Opens: 120  Flush tables: 1  Open tables: 113  Queries per second avg: 0.052
77 --------------
78 
79 root@localhost:mysql3306.sock [zlm]>show variables like 'character%';
80 +--------------------------+----------------------------------------------------------------+
81 | Variable_name            | Value                                                          |
82 +--------------------------+----------------------------------------------------------------+
83 | character_set_client     | utf8                                                           |
84 | character_set_connection | utf8                                                           |
85 | character_set_database   | utf8                                                           |
86 | character_set_filesystem | binary                                                         |
87 | character_set_results    | utf8                                                           |
88 | character_set_server     | gbk                                                            |
89 | character_set_system     | utf8                                                           |
90 | character_sets_dir       | /usr/local/mysql-5.7.21-linux-glibc2.12-x86_64/share/charsets/ |
91 +--------------------------+----------------------------------------------------------------+
92 8 rows in set (0.01 sec)

 

Set the character set again in curren session to gbk.

 1 root@localhost:mysql3306.sock [zlm]>set character_set_database=gbk;
 2 Query OK, 0 rows affected, 1 warning (0.00 sec)
 3 
 4 root@localhost:mysql3306.sock [zlm]>show variables like 'character%';
 5 +--------------------------+----------------------------------------------------------------+
 6 | Variable_name            | Value                                                          |
 7 +--------------------------+----------------------------------------------------------------+
 8 | character_set_client     | utf8                                                           |
 9 | character_set_connection | utf8                                                           |
10 | character_set_database   | gbk                                                            |
11 | character_set_filesystem | binary                                                         |
12 | character_set_results    | utf8                                                           |
13 | character_set_server     | gbk                                                            |
14 | character_set_system     | utf8                                                           |
15 | character_sets_dir       | /usr/local/mysql-5.7.21-linux-glibc2.12-x86_64/share/charsets/ |
16 +--------------------------+----------------------------------------------------------------+
17 8 rows in set (0.00 sec)
18 
19 root@localhost:mysql3306.sock [zlm]>select * from charset;
20 +------+--------+
21 | id   | name   |
22 +------+--------+
23 |    1 | 黎明   |
24 +------+--------+
25 1 row in set (0.00 sec)
26 
27 //Change the character set of client tool(mine is Xshell) to gbk.
28 
29 root@localhost:mysql3306.sock [zlm]>select * from charset;
30 +------+--------+
31 | id   | name   |
32 +------+--------+
33 |    1 | 榛庢槑   | //After changing the character set of client tool,the messy code occurs.
34 +------+--------+
35 1 row in set (0.00 sec)

 

 Change the character set of client tool back to utf8 and insert another record into test table.

 1 root@localhost:mysql3306.sock [zlm]>select * from charset;
 2 +------+--------+
 3 | id   | name   |
 4 +------+--------+
 5 |    1 | 黎明   |
 6 +------+--------+
 7 1 row in set (0.00 sec)
 8 
 9 root@localhost:mysql3306.sock [zlm]>insert into charset values(2,'上海');
10 Query OK, 1 row affected (0.00 sec)
11 
12 root@localhost:mysql3306.sock [zlm]>select * from charset;
13 +------+--------+
14 | id   | name   |
15 +------+--------+
16 |    1 | 黎明   |
17 |    2 | 上海   |
18 +------+--------+
19 2 rows in set (0.00 sec)
20 
21 //The changing of character set from utf8 to gbk does not influence the result of Chinese characters.

 

Change the character set of database & server to utf8 again.Then,change the character set of client & connection to gbk. 

 1 root@localhost:mysql3306.sock [zlm]>set character_set_database=utf8;
 2 Query OK, 0 rows affected, 1 warning (0.01 sec)
 3 
 4 root@localhost:mysql3306.sock [zlm]>set character_set_server=utf8;
 5 Query OK, 0 rows affected (0.00 sec)
 6 
 7 root@localhost:mysql3306.sock [zlm]>set names gbk;
 8 Query OK, 0 rows affected (0.00 sec)
 9 
10 root@localhost:mysql3306.sock [zlm]>\s
11 --------------
12 mysql  Ver 14.14 Distrib 5.7.21, for linux-glibc2.12 (x86_64) using  EditLine wrapper
13 
14 Connection id:        8
15 Current database:    zlm
16 Current user:        root@localhost
17 SSL:            Not in use
18 Current pager:        stdout
19 Using outfile:        ''
20 Using delimiter:    ;
21 Server version:        5.7.21-log MySQL Community Server (GPL)
22 Protocol version:    10
23 Connection:        Localhost via UNIX socket
24 Server characterset:    utf8
25 Db     characterset:    utf8
26 Client characterset:    gbk
27 Conn.  characterset:    gbk
28 UNIX socket:        /tmp/mysql3306.sock
29 Uptime:            1 hour 1 min 33 sec
30 
31 Threads: 1  Questions: 144  Slow queries: 0  Opens: 123  Flush tables: 1  Open tables: 116  Queries per second avg: 0.038
32 --------------
33 
34 root@localhost:mysql3306.sock [zlm]>root@localhost:mysql3306.sock [zlm]>select * from charset;
35 +------+------+
36 | id   | name |
37 +------+------+
38 |    1 | hķ     |
39 |    2 | ʏº£    |
40 +------+------+
41 2 rows in set (0.00 sec)
42 
43 //The messy code occured after I've changed the character of my client tool to utf8.

 

 Insert the third record with Chinese characters.

1 root@localhost:mysql3306.sock [zlm]>insert into charset values(3,'中國');
2 ERROR 1366 (HY000): Incorrect string value: '\xAD\xE5\x9B\xBD' for column 'name' at row 1
3 
4 //It doesn't permit your insertion operation now 'cause they'll be messy code again.

 

Summary
  • Cheracter set in MySQL does not make a large influence even though it has so many variables which may confuse us.
  • We can specify character set in a single table or even a column of the table which oracle cannot support.
  • In order to avoid messy code,make sure to keep character set of connection is bigger or equal with the one of our client tool.
  • It's reccomended to use utf8 even utf8mb4 as the character set of MySQL database because it can support almost all the languages
  • Notice that the character set of database may change after you execute "use xxx" to choose a target database.

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • HDFS HA Namenode HA 詳解 hadoop2.x 之後,Clouera 提出了 QJM/Qurom Journal Manager,這是一個基於 Paxos 演算法(分散式一致性演算法)實現的 HDFS HA 方案,它給出了一種較好的解決思路和方案,QJM 主要優勢如下: 不需要配置額外 ...
  • NoSQL:一類新出現的資料庫(not only sql) 泛指非關係型的資料庫 不支持SQL語法 存儲結構跟傳統關係型資料庫中的那種關係表完全不同,nosql中存儲的數據都是KV形式 NoSQL的世界中沒有一種通用的語言,每種nosql資料庫都有自己的api和語法,以及擅長的業務場景 NoSQL中 ...
  • 一、增加MariaDB源 cd /etc/yum.repos.d vi MariaDB.repo # MariaDB 10.1 CentOS repository list - created 2017-04-20 03:29 UTC # http://downloads.mariadb.org/m ...
  • 一、為什麼要做Galera集群非同步複製 Galera集群解決了資料庫高可用的問題,但是存在局限性,例如耗時的事務處理可能會導致集群性能急劇下降,甚至出現阻塞現象。而不幸的是,類似報表等業務需求就需要做數據大批量的數據查詢操作,為了不影響Galera的集群效率,需要做數據非同步複製,產生一個從庫來適配耗 ...
  • 安裝Elasticsearch Elasticsearch下載地址:https://www.elastic.co/cn/downloads/elasticsearch 也可以直接使用wget下載到某目錄下, 本文所有下載的包都放在 /home/tools 中, 解壓後移到 /home/apps目錄下 ...
  • 性能更好的新伺服器申請下來了,我們決定在2台新伺服器上使用mysql5.7,並且使用主從同步、讀寫分離架構,很不幸這個任務落到了我的頭上。讀寫分離是在業務代碼中實現的,在此不做詳述,介紹一下我搭建MySQL主從的過程。 環境介紹: Master 10.20.66.150 Slave 10.20.66 ...
  • 1、cd到mysql安裝目錄bin目錄: 2、輸入id、用戶名和密碼: 3、查看資料庫實例: 4、創建一個實例: 5、刪除一個實例: 6、創建一個表: 7、刪除一個表: 8、表結構: 9、修改表: 你想在一個庫裡面建表的時候 首先你要記得use 使用當前的庫 use庫名 創建表: create ta ...
  • 簡介 在資料庫中,我們除了存儲數據外,還存儲了大量的元數據。它們主要的作用就是描述資料庫怎麼建立、配置、以及各種對象的屬性等。本篇簡單介紹如何使用和查詢元數據,如何更有效的管理SQLServer 資料庫。 對一些有經驗的資料庫開發和管理人員而言,元數據是非常有價值的。下麵我會介紹一下簡單的原理,然後 ...
一周排行
    -Advertisement-
    Play Games
  • GoF之工廠模式 @目錄GoF之工廠模式每博一文案1. 簡單說明“23種設計模式”1.2 介紹工廠模式的三種形態1.3 簡單工廠模式(靜態工廠模式)1.3.1 簡單工廠模式的優缺點:1.4 工廠方法模式1.4.1 工廠方法模式的優缺點:1.5 抽象工廠模式1.6 抽象工廠模式的優缺點:2. 總結:3 ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 本章將和大家分享ES的數據同步方案和ES集群相關知識。廢話不多說,下麵我們直接進入主題。 一、ES數據同步 1、數據同步問題 Elasticsearch中的酒店數據來自於mysql資料庫,因此mysql數據發生改變時,Elasticsearch也必須跟著改變,這個就是Elasticsearch與my ...
  • 引言 在我們之前的文章中介紹過使用Bogus生成模擬測試數據,今天來講解一下功能更加強大自動生成測試數據的工具的庫"AutoFixture"。 什麼是AutoFixture? AutoFixture 是一個針對 .NET 的開源庫,旨在最大程度地減少單元測試中的“安排(Arrange)”階段,以提高 ...
  • 經過前面幾個部分學習,相信學過的同學已經能夠掌握 .NET Emit 這種中間語言,並能使得它來編寫一些應用,以提高程式的性能。隨著 IL 指令篇的結束,本系列也已經接近尾聲,在這接近結束的最後,會提供幾個可供直接使用的示例,以供大伙分析或使用在項目中。 ...
  • 當從不同來源導入Excel數據時,可能存在重覆的記錄。為了確保數據的準確性,通常需要刪除這些重覆的行。手動查找並刪除可能會非常耗費時間,而通過編程腳本則可以實現在短時間內處理大量數據。本文將提供一個使用C# 快速查找並刪除Excel重覆項的免費解決方案。 以下是實現步驟: 1. 首先安裝免費.NET ...
  • C++ 異常處理 C++ 異常處理機制允許程式在運行時處理錯誤或意外情況。它提供了捕獲和處理錯誤的一種結構化方式,使程式更加健壯和可靠。 異常處理的基本概念: 異常: 程式在運行時發生的錯誤或意外情況。 拋出異常: 使用 throw 關鍵字將異常傳遞給調用堆棧。 捕獲異常: 使用 try-catch ...
  • 優秀且經驗豐富的Java開發人員的特征之一是對API的廣泛瞭解,包括JDK和第三方庫。 我花了很多時間來學習API,尤其是在閱讀了Effective Java 3rd Edition之後 ,Joshua Bloch建議在Java 3rd Edition中使用現有的API進行開發,而不是為常見的東西編 ...
  • 框架 · 使用laravel框架,原因:tp的框架路由和orm沒有laravel好用 · 使用強制路由,方便介面多時,分多版本,分文件夾等操作 介面 · 介面開發註意欄位類型,欄位是int,查詢成功失敗都要返回int(對接java等強類型語言方便) · 查詢介面用GET、其他用POST 代碼 · 所 ...
  • 正文 下午找企業的人去鎮上做貸後。 車上聽同事跟那個司機對罵,火星子都快出來了。司機跟那同事更熟一些,連我在內一共就三個人,同事那一手指桑罵槐給我都聽愣了。司機也是老社會人了,馬上聽出來了,為那個無辜的企業經辦人辯護,實際上是為自己辯護。 “這個事情你不能怪企業。”“但他們總不能讓銀行的人全權負責, ...