1. 程式人生 > >Ubuntu修改mysql預設編碼

Ubuntu修改mysql預設編碼

修改mysql的配置檔案,使資料庫與伺服器作業系統的字符集設定一致。
    vi /etc/my.cnf 設定(如果沒有發現這個檔案,就新建1個)
    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    default-character-set=utf8  (增加的關鍵一句,使得資料庫預設以utf8儲存)
    當然,修改後,要重啟資料庫。
    
    再次用mysql -u root -p命令進入資料庫系統,用SHOW VARIABLES LIKE 'character_set_%';命令檢視到如下內容:
+--------------------------+-----------------------------------------------------------------------+
| Variable_name            | Value                                                                 |
+--------------------------+-----------------------------------------------------------------------+
| character_set_client     | latin1                                                                | 
| character_set_connection | latin1                                                                | 
| character_set_database   | utf8
                                                                  | 
| character_set_filesystem | binary                                                                | 
| character_set_results    | latin1                                                                | 
| character_set_server     | utf8
                                                                  | 
| character_set_system     | utf8                                                                  | 
| character_sets_dir       | /usr/local/mysql-5.1.20-beta-linux-i686-glibc23/share/mysql/charsets/ | 
+--------------------------+-----------------------------------------------------------------------+
    發現關鍵專案已經用了utf8,但這樣還不夠,還要保證客戶端也是用utf8的字符集來操作的

    登入的時候,要用以下命令:mysql --default-character-set=utf8 -u root -p
   
    再次用SHOW VARIABLES LIKE 'character_set_%';命令檢視,結果變成了:
+--------------------------+-----------------------------------------------------------------------+
| Variable_name            | Value                                                                 |
+--------------------------+-----------------------------------------------------------------------+
| character_set_client     | utf8                                                                  | 
| character_set_connection | utf8                                                                  | 
| character_set_database   | utf8                                                                  | 
| character_set_filesystem | binary                                                                | 
| character_set_results    | utf8                                                                  | 
| character_set_server     | utf8                                                                  | 
| character_set_system     | utf8                                                                  | 
| character_sets_dir       | /usr/local/mysql-5.1.20-beta-linux-i686-glibc23/share/mysql/charsets/ | 
+--------------------------+-----------------------------------------------------------------------+
    這樣才能保證客戶端所發命令都是基於utf8格式的,比如說建立資料庫和表,預設就會以utf8編碼,而無須再次指定。