Ubuntu下MySQL中文亂碼的問題解決。本站提示廣大學習愛好者:(Ubuntu下MySQL中文亂碼的問題解決)文章只能為提供參考,不一定能成為您想要的結果。以下是Ubuntu下MySQL中文亂碼的問題解決正文
發現問題
最近一段時間學習Django,在進行與MySQL數據聯合使用的插入數據的時候遇到下邊的問題:
/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
中文無法插入MySQL數據庫~!~!
查看數據庫編碼
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)
修改數據庫編碼
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)
查看修改後的結果
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)
這時候繼續插入就沒有任何問題了。
如果真的是這樣簡單就好了,因為這樣的修改作者在重啟MySQL的後設置失效!!!
繼續尋找其它方法
sudo gedit /etc/mysql/my.cnf
在my.cnf文件的對應節點添加一下信息:
[client] default-character-set=utf8 [mysqld] default-character-set=utf8 [mysql] default-character-set=utf8
然後重啟MySQL:
/etc/init.d/mysql start
如果能重啟那麼再次查看數據庫編碼:
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)
如果真的時這樣就好了,事情的發生總是不像想象的那麼簡單:
在重啟MySQL服務的時候發現一直處於等待狀態(PS:猜測發生了死鎖什麼的),這個時候執行 :
mysql -u root -p
則會拋出異常:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
我就是想修改一下編碼而已,為什麼這麼苦->->->->->!!!
解決這個問題的時候試過好多方法(重啟,恢復)。。。。。
sudo /etc/init.d/mysql status
查看mysql的狀態:mysql respawn/post-start, (post-start) process 55665
這些方式不能解決問題,還是從日志開始吧…
找到日至文件 /var/log/mysql/error.log
繼續找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了,並且重啟後設置的編碼依舊生效。
當然之前創建的數據庫需要重新創建T_T
因為 show create database bangjob;
展示之前創建的數據編碼依舊是latin1
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。