Ubuntu的MySQL中文乱码问题
作者:网络转载 发布时间:[ 2017/2/9 10:16:21 ] 推荐标签:数据库 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: 'xE6x88x90xE5x8Ax9F...' 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)。
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11