關於MySQL如何修改character_set_client的編碼問題

  • 問題引入:

        我們經常會遇到一些向MySQL數據庫中插入中文,但是select出來的時候,卻發現是亂碼的情況。如我們向表a出入這樣一段記錄:i

insert into a values('你好helloworld你好','helloworld');

可能當你訪問它的時候,會發現他的結果變成如下圖所示:

image

那怎麼樣才能解決這種問題呢?通過下文對MySQL中字符集的一些操作,你將會得到答案!

 

  • 查看庫、表字符集命令: 

        要解決字符集的問題,首先要知道現在的系統、數據庫、表、客戶端等使用什麼樣的字符集,以及系統支持什麼字符集等,下面介紹一些獲取相關信息的語句:

1.查看數據庫支持的所有字符集

show character set;或者show char set;

Image(42)

2.查看當前狀態,裏面當然包括字符集的設置:

status或者/s

Image(43)

其中Db characterset對應的是數據庫目錄下的db.opt文件內容:

Image(44)

3.查看系統字符集設置,包括所有的字符集設置:

show variables like '%char%';

得出如何所示結果:

Image(40)

其中的含義如下:

Image(41)

關於connection相關的字符集的官方文檔:

  • What character set is the statement in when it leaves the client?

The server takes the character_set_client system variable to be the character set in which statements are sent by the client.

 

  • What character set should the server translate a statement to after receiving it?

For this, the server uses the character_set_connection and collation_connection system variables. It converts statements sent by the client from character_set_client to character_set_connection (except for string literals that have an introducer such as _latin1 or _utf8). collation_connection is important for comparisons of literal strings. For comparisons of strings with column values, collation_connection does not matter because columns have their own collation, which has a higher collation precedence.

 

  • What character set should the server translate to before shipping result sets or error messages back to the client?
The character_set_results system variable indicates the character set in which the server returns query results to the client. This includes result data such as column values, and result metadata such as column names and error messages.

     從上文中可以看出character_set_connection、character_set_client、character_set_results三個字符集什麼時候用到。從實際上可以看到,當客戶端連接服務器的時候,它會將自己想要的字符集名稱發給mysql服務器,然後服務器就會使用這個字符集去設置character_set_connection、character_set_client、character_set_results這三個值。如cmd是用gbk,而mysql workbench是用utf8.

CMD:

image

MySql WorkBench:

image

 

4.查看數據表中字符集設置:

show full columns from tablename;

Image(45)

show create table tablename/G;

Image(46)

 

5.查看數據庫編碼:

show create database dbname;

Image(47)

 

  • 創建時指定字符集:

        知道了怎麼查找字符集的相關信息之後,我們就要懂得怎麼在創建指定對象的時候,爲該對象匹配相應的字符集。

1.服務器級:

在安裝MySQL時可以設置服務器的默認編碼格式,也可對my.ini做修改,修改[mysqld]裏面的character_set_server=utf8,則可設置character_set_server的值。

2.數據庫級:

CREATE DATABASE db_name DEFAULT CHARACTER SET utf8;

Image(48)

注意,如果不指定默認的字符集,則系統會根據character_set_database的值進行設置,如:

Image(49)

3.表級:

CREATE TABLE  `db_name`.`tb_name` (id VARCHAR(20) NOT NULL,name VARCHAR(20) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

從下圖可看出,定義表的默認字符集爲utf8,即使character_set_database爲gbk,但是表的列都未utf8

Image(50)

但要注意,如果沒有定義表的默認字符集,則他會按照character_set_database的值來設置,如圖所示:

Image(51)

4.列級:

CREATE TABLE  `db_name`.`tb_name` (   id varchar(20) NOT NULL,   name varchar(20) CHARACTER SET utf8 );

從下圖可以看到,整個表的默認字符集爲gbk,所以沒有指定字符集的列都用默認的字符集,而指定了字符集的列name,則使用指定的字符集utf8。

Image(52)

 

  • 修改字符集命令

        如果已經是創建好的對象,那又應該如何處理呢。我們就應該對指定對象就行修改字符集的操作。

1.修改character_set_connection、character_set_client、character_set_results三值:

對於某一個連接來說,可以使用:

SET NAMES 'charset_name' [COLLATE 'collation_name']

image

命令

SET NAMES 'charset_name' [COLLATE 'collation_name']

相當於

SET character_set_client = charset_name; SET character_set_results = charset_name; SET character_set_connection = charset_name;

另外、還可以修改配置文件,對[mysql]下增加default-character-set=utf8,配置成你想要的字符集。(個人嘗試在my.ini裏面配置過,沒有成效,不知道是不是被使用的客戶端想要的字符集給覆蓋掉了呢?)

2.修改character_set_database字段:

ALTER DATABASE db_name     [[DEFAULT] CHARACTER SET charset_name]     [[DEFAULT] COLLATE collation_name]

image

3.修改character_set_server字段:

最簡單的方法是直接改my.ini配置文件裏面[mysqld]的字段,增加character-set-server=gbk,然後重啓mysqld,則可改爲你想要的字符集。

4.修改表的字符集:

ALTER TABLE tbl_name     [[DEFAULT] CHARACTER SET charset_name]     [COLLATE collation_name]

5.修改列的字符集:

col_name {CHAR | VARCHAR | TEXT} (col_length)     [CHARACTER SET charset_name]     [COLLATE collation_name]

例如:

ALTER TABLE t1 MODIFY     col1 VARCHAR(5)       CHARACTER SET latin1       COLLATE latin1_swedish_ci;

  轉自:http://zhidao.baidu.com/link?url=E5kHt1YHeMOFxI6p_5myGoGmHw0wwVi2KEOomo2tzKrHn9KihKaH0Cb5eGb8Bz015hKlP1oBYqOa-Ka2MV1K4SnR0aWTBnRrqEJiWqKMale

  • 參考資料:

MySQL的Character Set Support:  http://dev.mysql.com/doc/refman/5.6/en/charset.html

mysql常用查看庫,表字符集命令:  http://bjlfp.blog.163.com/blog/static/773684612012298455765/

MySQL 插入數據時,中文亂碼問題的解決: http://www.cnblogs.com/sunzn/archive/2013/03/14/2960248.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章