linux下mysql中文亂碼(中文問號)解決辦法

今天發現我mysql插入中文時老出現????號了,但保存英文是沒有問題的,下面我來給各位同窗介紹一下linux下mysql中文亂碼解決方法。

linux下則須要修改/etc/my.cnf,在[mysqld]下加入default-character-set = utf8,[client]下加入default-character-set = utf8,在[mysql]字段里加入default-character-set=utf8html

實現:
1.查看原編碼mysql

 代碼以下  
mysql> show variables like 'character%'; //執行編碼顯示,能夠看到默認是latin1
+--------------------------+----------------------------+ 
| Variable_name | Value | 
+--------------------------+----------------------------+ 
| character_set_client | latin1 | 
| character_set_connection | latin1 | 
| character_set_database | latin1 | 
| character_set_filesystem | binary | 
| character_set_results | latin1 | 
| character_set_server | latin1 | 
| character_set_system | utf8 | 
| character_sets_dir | /usr/share/mysql/charsets/ | 
+--------------------------+----------------------------+  

2.修改/etc/my.cnf,分別在[client],[mysqld],[mysql]字段裏添加default-character-set=utf8。注意[mysqld]字段與[mysql]字段是有區別的。這點在網上沒人反饋過。linux

# vim /etc/my.cnf  //添加default-character-set=utf8sql

 代碼以下  

[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8數據庫

[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
character-set-server=utf8vim

[mysql]
no-auto-rehash服務器

default-character-set=utf8修改完成後,service mysql restart重啓mysql服務器,使用SHOW VARIABLES LIKE ‘character%’;查看,發現數據庫編碼全已改爲utf8socket

 代碼以下  
mysql> show variables like 'character%';    
+--------------------------+----------------------------------------+
| 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/share/mysql/charsets/ |
+--------------------------+----------------------------------------+ 

3.若是上面的都修改了還亂碼,那剩下問題就必定在connection鏈接層上。解決方法是在發送查詢前執行一下下面這句(直接寫在SQL文件的最前面):編碼

 代碼以下  

SET NAMES ‘utf8′;url

它至關於如下三指令:

 代碼以下  
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;

附錄:
朋友的一個站點轉到我這邊。導入mysql時,mysql仍是latin的編碼,所以,雖然按上面的步驟,在/etc/my.cnf文件中[client],[mysqld],[mysql]字段裏添加default-character-set=utf8,但站點部分中文是亂碼。在將mysql編輯改成utf8再從新導入數據庫後,就正常了。 估計是mysql是latin編碼是,導入數據時,由於mysql沒法識別,直接將數據保存成了???這種亂碼的形式了

相關文章
相關標籤/搜索