發(fā)現(xiàn)問題
最近一段時(shí)間學(xué)習(xí)Django,在進(jìn)行與MySQL數(shù)據(jù)聯(lián)合使用的插入數(shù)據(jù)的時(shí)候遇到下邊的問題:
/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數(shù)據(jù)庫~!~!
查看數(shù)據(jù)庫編碼
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ù)庫編碼
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ù)插入就沒有任何問題了。
如果真的是這樣簡單就好了,因?yàn)檫@樣的修改作者在重啟MySQL的后設(shè)置失效!?。?/p>
繼續(xù)尋找其它方法
sudo gedit /etc/mysql/my.cnf
在my.cnf文件的對應(yīng)節(jié)點(diǎn)添加一下信息:
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
[mysql]
default-character-set=utf8
然后重啟MySQL:
如果能重啟那么再次查看數(shù)據(jù)庫編碼:
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ā)生總是不像想象的那么簡單:
在重啟MySQL服務(wù)的時(shí)候發(fā)現(xiàn)一直處于等待狀態(tài)(PS:猜測發(fā)生了死鎖什么的),這個(gè)時(shí)候執(zhí)行 :
則會拋出異常:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
我就是想修改一下編碼而已,為什么這么苦->->->->->!!!
解決這個(gè)問題的時(shí)候試過好多方法(重啟,恢復(fù))。。。。。
sudo /etc/init.d/mysql status
查看mysql的狀態(tài):mysql respawn/post-start, (post-start) process 55665
這些方式不能解決問題,還是從日志開始吧…
找到日至文件 /var/log/mysql/error.log
繼續(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ù)庫需要重新創(chuàng)建T_T
因?yàn)?show create database bangjob;
展示之前創(chuàng)建的數(shù)據(jù)編碼依舊是latin1
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
您可能感興趣的文章:- mysql中插入表數(shù)據(jù)中文亂碼問題的解決方法
- 解決Linux下Tomcat向MySQL插入數(shù)據(jù)中文亂碼問題
- JDBC連接mysql處理中文時(shí)亂碼解決辦法詳解
- 詳解mysql數(shù)據(jù)庫中文亂碼問題
- 在CMD中操作mysql數(shù)據(jù)庫出現(xiàn)中文亂碼解決方案
- 關(guān)于servlet向mysql添加數(shù)據(jù)時(shí)中文亂碼問題的解決
- 解決Mysql5.7中文亂碼的問題
- JSP MySQL插入數(shù)據(jù)時(shí)出現(xiàn)中文亂碼問題的解決方法
- Mac Mysql數(shù)據(jù)庫中文亂碼問題解決
- 解壓版MYSQL中文亂碼問題解決方案