關(guān)于MySQL如何修改character_set_client的編碼問題

問題引入:

    我們經(jīng)常會遇到一些向MySQL數(shù)據(jù)庫中插入中文,但是select出來的時候,卻發(fā)現(xiàn)是亂碼的情況。如我們向表a出入這樣一段記錄:i

insert into a values('你好helloworld你好','helloworld');
可能當(dāng)你訪問它的時候,會發(fā)現(xiàn)他的結(jié)果變成如下圖所示:


image

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

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

    要解決字符集的問題,首先要知道現(xiàn)在的系統(tǒng)、數(shù)據(jù)庫、表、客戶端等使用什么樣的字符集,以及系統(tǒng)支持什么字符集等,下面介紹一些獲取相關(guān)信息的語句:

1.查看數(shù)據(jù)庫支持的所有字符集
show character set;或者show char set;

Image(42)

2.查看當(dāng)前狀態(tài),里面當(dāng)然包括字符集的設(shè)置:
status或者/s
Image(43)

其中Db characterset對應(yīng)的是數(shù)據(jù)庫目錄下的db.opt文件內(nèi)容:
Image(44)

3.查看系統(tǒng)字符集設(shè)置,包括所有的字符集設(shè)置:
show variables like '%char%';
得出如何所示結(jié)果:
Image(40)

其中的含義如下:
Image(41)

關(guān)于connection相關(guān)的字符集的官方文檔:
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三個字符集什么時候用到。從實際上可以看到,當(dāng)客戶端連接服務(wù)器的時候,它會將自己想要的字符集名稱發(fā)給mysql服務(wù)器,然后服務(wù)器就會使用這個字符集去設(shè)置character_set_connection、character_set_client、character_set_results這三個值。如cmd是用gbk,而mysql workbench是用utf8.
CMD:


image

MySql WorkBench:


image

4.查看數(shù)據(jù)表中字符集設(shè)置:
show full columns from tablename;

Image(45)

show create table tablename/G;
Image(46)

5.查看數(shù)據(jù)庫編碼:
show create database dbname;

Image(47)

創(chuàng)建時指定字符集:

    知道了怎么查找字符集的相關(guān)信息之后,我們就要懂得怎么在創(chuàng)建指定對象的時候,為該對象匹配相應(yīng)的字符集。

1.服務(wù)器級:
在安裝MySQL時可以設(shè)置服務(wù)器的默認編碼格式,也可對my.ini做修改,修改[mysqld]里面的character_set_server=utf8,則可設(shè)置character_set_server的值。
2.數(shù)據(jù)庫級:
CREATE DATABASE db_name DEFAULT CHARACTER SET utf8;

Image(48)

注意,如果不指定默認的字符集,則系統(tǒng)會根據(jù)character_set_database的值進行設(shè)置,如:
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的值來設(shè)置,如圖所示:
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)

修改字符集命令

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

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']
相當(dāng)于
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;
轉(zhuǎn)自: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 插入數(shù)據(jù)時,中文亂碼問題的解決: http://www.cnblogs.com/sunzn/archive/2013/03/14/2960248.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容