修改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
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...