明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

Ubuntu下MySQL中文亂碼的問(wèn)題處理方法說(shuō)明

[摘要]對(duì)于MySQL中文亂碼問(wèn)題(我的是Ubuntu系統(tǒng)),在網(wǎng)上看了很多解決方案,期間也走了不少?gòu)澛罚韵胫鶕?jù)自己的解決之路寫一篇文章分享給有需要的朋友們,所以這篇文章主要介紹了如何解決Ubuntu下MySQL中文亂碼問(wèn)題的相關(guān)資料,需要的朋友可以參考借鑒。發(fā)現(xiàn)問(wèn)題最近一段時(shí)間學(xué)習(xí)Django,在...
對(duì)于MySQL中文亂碼問(wèn)題(我的是Ubuntu系統(tǒng)),在網(wǎng)上看了很多解決方案,期間也走了不少?gòu)澛,所以想著根?jù)自己的解決之路寫一篇文章分享給有需要的朋友們,所以這篇文章主要介紹了如何解決Ubuntu下MySQL中文亂碼問(wèn)題的相關(guān)資料,需要的朋友可以參考借鑒。

發(fā)現(xiàn)問(wèn)題

最近一段時(shí)間學(xué)習(xí)Django,在進(jìn)行與MySQL數(shù)據(jù)聯(lián)合使用的插入數(shù)據(jù)的時(shí)候遇到下邊的問(wèn)題:

/usr/local/lib/python2.7/dist-packages/Django-1.11.dev20170117002028-py2.7.egg
/django/db/backends/mysql/base.py:109: Warning: Incorrect string value: '\xE6\x88\x90\xE5\x8A\x9F...' 
for column 'json' at row 1
 return self.cursor.execute(query, args)
[07/Feb/2017 12:15:21] "GET /index/ HTTP/1.1" 200 250

中文無(wú)法插入MySQL數(shù)據(jù)庫(kù)~!~!

查看數(shù)據(jù)庫(kù)編碼

mysql> show create database bangjob;
+----------+--------------------------------------------------------------------+
  Database   Create Database     
+----------+--------------------------------------------------------------------+
  bangjob   CREATE DATABASE `bangjob` /*!40100 DEFAULT CHARACTER SET latin1 */  
+----------+--------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show variables like'%char%';
+--------------------------+----------------------------+
  Variable_name   Value   
+--------------------------+----------------------------+
  character_set_client   utf8   
  character_set_connection   utf8   
  character_set_database   latin1   
  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)

修改數(shù)據(jù)庫(kù)編碼

mysql> set character_set_database=utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> set character_set_server=utf8;
Query OK, 0 rows affected (0.00 sec)

查看修改后的結(jié)果

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

這時(shí)候繼續(xù)插入就沒有任何問(wèn)題了。

如果真的是這樣簡(jiǎn)單就好了,因?yàn)檫@樣的修改作者在重啟MySQL的后設(shè)置失效。!

繼續(xù)尋找其它方法

sudo gedit /etc/mysql/my.cnf

在my.cnf文件的對(duì)應(yīng)節(jié)點(diǎn)添加一下信息:

[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
[mysql]
default-character-set=utf8

然后重啟MySQL:

/etc/init.d/mysql start

如果能重啟那么再次查看數(shù)據(jù)庫(kù)編碼:

mysql> 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    utf8       
  character_set_system    utf8       
  character_sets_dir    /usr/share/mysql/charsets/  
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

如果真的時(shí)這樣就好了,事情的發(fā)生總是不像想象的那么簡(jiǎn)單:

在重啟MySQL服務(wù)的時(shí)候發(fā)現(xiàn)一直處于等待狀態(tài)(PS:猜測(cè)發(fā)生了死鎖什么的),這個(gè)時(shí)候執(zhí)行 :

mysql -u root -p

則會(huì)拋出異常

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

我就是想修改一下編碼而已,為什么這么苦->->->->->!!!

解決這個(gè)問(wèn)題的時(shí)候試過(guò)好多方法(重啟,恢復(fù))。。。。。

sudo /etc/init.d/mysql status

查看mysql的狀態(tài):mysql respawn/post-start, (post-start) process 55665

這些方式不能解決問(wèn)題,還是從日志開始吧…

找到日至文件 /var/log/mysql/error.log

Ubuntu下MySQL中文亂碼的問(wèn)題解決辦法介紹

繼續(xù)找ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)的解決辦法。

答案:

[ mysqld ] 下的 default-character-set=utf8' 改成character_set_server=utf8

好了,終于可以重啟MySQL了,并且重啟后設(shè)置的編碼依舊生效。

當(dāng)然之前創(chuàng)建的數(shù)據(jù)庫(kù)需要重新創(chuàng)建T_T

因?yàn)?show create database bangjob;展示之前創(chuàng)建的數(shù)據(jù)編碼依舊是latin1

總結(jié)

以上就是Ubuntu下MySQL中文亂碼的問(wèn)題解決辦法介紹的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!


學(xué)習(xí)教程快速掌握從入門到精通的SQL知識(shí)。