關於MYSQL存儲中文問題

最近在學習MYSQL時遇到了不能存儲中文的問題,在網上找了不少相似的方法,最後都失敗了(失敗緣由:修改完my.cnf文件後mysql server從新啓動失敗),瀕臨崩潰的時候重裝了下mysql(Ubuntu重裝mysql)而後用如下方法走了一遍就解決了。mysql

 

形成MYSQL存儲中文亂碼的緣由是編碼格式不一致致使亂碼,如何去查看編碼是否一致呢?sql

首先進入mysql服務器:數據庫

~$ mysql -u root -p******
//「*」號處輸入密碼,與p之間沒有空格;

 查看當前數據庫相關編碼集:服務器

show variables like '%char%';       (注意最後有分號!)       
+------------------------------+---------------------------------+
| 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         | latin1                          |
| character_set_system         | utf8                            |
| character_sets_dir           | /usr/share/mysql/charsets/      |
+------------------------------+---------------------------------+
8 rows in set (0.00 sec)

 如何去修改爲一致的編碼格式呢?學習

 Ctrl+C退出mysql服務器。編碼

 用vi或者gedit打開MYSQL配置文件my.cnf,並添加內容:spa

~$ sudo vi /etc/mysql/my.cnf              //用vi打開

~$ sudo gedit /etc/mysql/my.cnf         //用gedit打開

在[client]標籤下,增長客戶端的字符編碼
[client]
default-character-set=utf8

在[mysqld]標籤下,增長服務器端的字符編碼
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

 修改完後,從新啓動MYSQL服務器:rest

~$ sudo /etc/init.d/mysql restart

 再次進入mysql服務器,查看編碼集,出現以下結果表示You succeed:code

+--------------------------+----------------------------+
| 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/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

轉載自本人ITeye連接:http://xiaozhuang0706.iteye.com/blog/2258342server

相關文章
相關標籤/搜索