Solve the Problem of MySQL Chinese garbled characters in Ubuntu, using tumysql
Problems Found
Recently I learned about Django and encountered the following problems when I inserted data in conjunction with MySQL DATA:
/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
Unable to insert MySQL database in Chinese ~!~!
View database code
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)
Modify database code
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)
View the modified result
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)
At this time, there will be no problem if you continue to insert.
If this is the case, it would be easy, because the modification settings failed after the author restarted MySQL !!!
Continue searching for other methods
sudo gedit /etc/mysql/my.cnf
Add the following information to the corresponding node of the my. cnf file:
[client]default-character-set=utf8[mysqld]default-character-set=utf8[mysql]default-character-set=utf8
Then restart MySQL:
/etc/init.d/mysql start
If you can restart the instance, check the database code again:
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)
If this is the case, things are always not as simple as you think:
When I restart the MySQL service, I find that I am always waiting (PS: I guess there is a deadlock or something). At this time, I will execute:
mysql -u root -p
An exception is thrown:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I just want to modify the encoding. Why is it so bitter?-> !!!
I tried many methods to solve this problem (restart and restore ).....
sudo /etc/init.d/mysql status
View the mysql status:mysql respawn/post-start, (post-start) process 55665
These methods cannot solve the problem. Let's start with the log...
Locate the/var/log/mysql/error. log File
Continue searchingERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
.
Answer:
Under [mysqld]default-character-set=utf8'
Changecharacter_set_server=utf8
Now, you can restart MySQL, And the configured encoding will still take effect after the restart.
Of course, the previously created database needs to be re-created T_T
Becauseshow create database bangjob;
Demonstrate that the previously created data encoding is still latin1
Summary
The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.