MySql:charset和collation的設(shè)置
charset 和 collation 有多個(gè)級(jí)別的設(shè)置:服務(wù)器級(jí)、數(shù)據(jù)庫級(jí)、表級(jí)、列級(jí)和連接級(jí)
1.服務(wù)器級(jí)
查看設(shè)置:show global variables like 'character_set_server'; 和 show global variables like 'collation_server';
修改設(shè)置:在OPTION FILE (/etc/mysql/my.cnf)里設(shè)置:
[mysqld]
character_set_server=utf8
collation_server=utf8_general_ci
2. 數(shù)據(jù)庫級(jí)
查看設(shè)置:select * from information_schema.schemata where schema_name = 'cookbook';
設(shè)置:
1.若沒有顯式設(shè)置,則自動(dòng)使用服務(wù)器級(jí)的配置
2.顯式設(shè)置:在創(chuàng)建庫時(shí)指定
create database playUtf8 DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
3.表級(jí)
查看設(shè)置:show create table course;
設(shè)置:
1.若沒有顯式設(shè)置,則自動(dòng)使用數(shù)據(jù)庫級(jí)的配置
2.顯式設(shè)置:在創(chuàng)建表時(shí)指定
create table utf ( id int ) default charset=utf8 default collate=utf8_bin;
4.列級(jí)
查看設(shè)置:show create table course;
設(shè)置:
1.若沒有顯式設(shè)置,則自動(dòng)使用表級(jí)的配置
2.顯式設(shè)置:
CREATE TABLE Table1(column1 VARCHAR(5) CHARACTER SET latin1 COLLATE latin1_german1_ci);
5.連接級(jí)別
查看設(shè)置:
show variables like 'character_set_client'; # 服務(wù)端使用這個(gè)編碼來理解客戶端發(fā)來的statements
show variables like 'character_set_connection' ; # 我還不知道什么意思,等看了mysql源碼再說
show variables like 'character_set_results'; # 服務(wù)端使用這個(gè)編碼回送結(jié)果集和錯(cuò)誤信息
設(shè)置:
客戶端在連接時(shí)可以指定這些參數(shù);同時(shí),服務(wù)端也提供了一個(gè)Global范圍的值,客戶端未指定這些參數(shù)時(shí),服務(wù)端就使用這個(gè)Global值。這個(gè)global值怎么設(shè)置的? 我查遍了很多文檔,似乎還沒看到設(shè)置的辦法 (有人說通過my.cnf,或者在啟動(dòng)mysqld時(shí)指定命令行參數(shù),其實(shí)都是錯(cuò)的)
附:connector/j傳輸SQL時(shí)用什么編碼?
答案: "The character encoding between client and server is automatically detected upon connection. The encoding used by the driver is specified on the server using the character_set_server system variable for server versions 4.1.0 and newer."
也就是說,是在連接時(shí)查詢服務(wù)器端的character_set_server值,再確定連接將使用的編碼。
不過,官方文檔還說,"要想覆蓋客戶端上的自動(dòng)檢測(cè)編碼功能,可在用于連接到服務(wù)器的URL中使用“characterEncoding”屬性。"
bitsCN.com聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com