一、mysql的多字元集支援
1、設定mysqld服務同時支援多個字元集
將mysqld服務的預設字元集改為utf8,並同時支援latin1、gbk、gb2312、big5、ascii字元集。可以為不同的資料庫、表使用不同的字元集編碼。
Step1:./configure步驟時需增加字元集支援選項,例如:
shell> ./configure --with-charset=utf8 --with-collation=utf8_bin --with-extra-charsets=big5,ascii,gb2312,gbk,utf8,latin1
//預設的字元集預設為latin1。
//其中,utf8對應可用的collation值為:utf8_bin、utf8_general_ci、utf8_unicode_ci
//extra支援也可以使用 "--with-extra-charsets=all"。
Step2:在make步驟之前,修改config.h標頭檔,尋找utf8、gbk、gb2312等定義字元集變數的行,確認有如下內容(如果沒有則需要手動添加),例如:
#define HAVE_CHARSET_ascii 1
#define HAVE_CHARSET_big5 1
#define HAVE_CHARSET_gb2312 1
#define HAVE_CHARSET_gbk 1
#define HAVE_CHARSET_latin1 1
#define HAVE_CHARSET_utf8 1
//一開始沒有修改config.h,費了好多功夫,"mysql>"環境中使用非預設字元集時總報"Error 1115"錯誤:
mysqld> set names gbk;
ERROR 1115 (42000): Unknown character set: 'gbk'
//後來發現在./configure完成後,config.h檔案中並沒有自動開啟gbk、gb2312等變數定義,不得不手動修改添加。
2、驗證mysqld服務的多字元集支援
1) 查看當前mysql資料庫支援的字元集
mysql> show character set;
2) 查看對應字元集可用的校對規則
mysql> show collation like 'gb%';
mysql> show collation like 'utf8%';
3) 查看當前mysql伺服器使用的預設字元集狀態
mysql> status
……
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
4) 在建立資料庫時指定使用的預設字元集:
mysql> create database testdb default character set utf8;
mysql> show create database testdb; //驗證資料庫建立資訊
5) 在建立資料表時指定使用的預設字元集:
mysql> create table testdb.tb1 ( id int(10) unsigned NOT NULL AUTO_INCREMENT,name varchar(15) NOT NULL default '',PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARACTER SET gb2312;
mysql> show create table testdb.tb1; //驗證資料表建立資訊
6) 在mysql>用戶端環境中設定使用的預設字元集
mysql> set NAMES utf8;