最近一段时间学习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> 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
[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