Ubuntu修改mysql預設編碼

來源:互聯網
上載者:User

修改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編碼,而無須再次指定。

需要說明一下,如果在修改編碼之前已經建好資料庫,需要刪掉原來,重建立立資料庫。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.