《Mysql入门Ubuntu下MySQL中文乱码的问题解决》要点:
本文介绍了Mysql入门Ubuntu下MySQL中文乱码的问题解决,希望对您有用。如果有疑问,可以联系我们。
MYSQL实例发现问题
MYSQL实例最近一段时间学习Django,在进行与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实例中文无法插入MySQL数据库~!~!
MYSQL实例查看数据库编码
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实例
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实例修改数据库编码
MYSQL实例
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实例查看修改后的结果
MYSQL实例
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实例这时候继续插入就没有任何问题了.
MYSQL实例如果真的是这样简单就好了,因为这样的修改作者在重启MySQL的后设置失效!!!
MYSQL实例继续寻找其它办法
MYSQL实例
sudo gedit /etc/mysql/my.cnf
MYSQL实例在my.cnf文件的对应节点添加一下信息:
MYSQL实例
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
[mysql]
default-character-set=utf8
MYSQL实例然后重启MySQL:
MYSQL实例
/etc/init.d/mysql start
MYSQL实例如果能重启那么再次查看数据库编码:
MYSQL实例
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实例如果真的时这样就好了,事情的发生总是不像想象的那么简单:
MYSQL实例在重启MySQL服务的时候发现一直处于等待状态(PS:猜测发生了死锁什么的),这个时候执行 :
MYSQL实例
mysql -u root -p
MYSQL实例则会抛出异常:
MYSQL实例
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
MYSQL实例我就是想修改一下编码罢了,为什么这么苦->->->->->!!!
MYSQL实例解决这个问题的时候试过好多办法(重启,恢复).....
MYSQL实例
sudo /etc/init.d/mysql status
MYSQL实例查看mysql的状态:mysql respawn/post-start, (post-start) process 55665
MYSQL实例这些方式不能办理问题,还是从日志开始吧…
MYSQL实例找到日至文件 /var/log/mysql/error.log
MYSQL实例
MYSQL实例继续找ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
的解决方法.
MYSQL实例答案:
MYSQL实例[ mysqld ] 下的 default-character-set=utf8'
改成character_set_server=utf8
MYSQL实例好了,终于可以重启MySQL了,并且重启后设置的编码依旧生效.
MYSQL实例当然之前创建的数据库需要重新创建T_T
MYSQL实例因为 show create database bangjob;
展示之前创建的数据编码依旧是latin1
MYSQL实例总结
MYSQL实例以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的赞助,如果有疑问大家可以留言交流.
维易PHP培训学院每天发布《Mysql入门Ubuntu下MySQL中文乱码的问题解决》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。