對於MySQL中文亂碼問題(我的是Ubuntu系統),在網路上看了很多解決方案,期間也走了不少彎路,所以想著根據自己的解決之路寫一篇文章分享給有需要的朋友們,所以這篇文章主要介紹瞭如何解決Ubuntu下MySQL中文亂碼問題的相關資料,需要的朋友可以參考借鑒。與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> 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)
sudo gedit /etc/mysql/my.cnf
在my.cnf檔案的對應節點加入資訊:[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
[mysql]
default-character-set=utf8
/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)
我就是想修改一下編碼而已,為什麼這麼苦->->->->->!!!
解決這個問題的時候試過好多方法(重啟,恢復)。 這些方式無法解決問題,還是從日誌開始吧…
找到日至檔案/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'
改成
好了,終於可以重新啟動MySQL了,而且重新啟動後設定的編碼依舊生效。
show create database bangjob;展示之前建立的資料編碼依舊是latin1
總結
以上是Ubuntu下MySQL中文亂碼的問題解決方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!