Home  >  Article  >  Database  >  Introduction to the solution to the problem of Chinese garbled characters in MySQL under Ubuntu

Introduction to the solution to the problem of Chinese garbled characters in MySQL under Ubuntu

黄舟
黄舟Original
2017-03-29 13:43:521181browse

ForMySQL中文garbled problem(mine is Ubuntu System), I read a lot of solutions on the Internet, and made many detours during the process, so I thought about writing an article based on my own solution to share with friends in need, so this article mainly introduces how to solve Ubuntu Download the relevant information on the MySQL Chinese garbled code problem. Friends who need it can refer to it. When using

to insert data

in conjunction with MySQL data, I encountered the following problem:

/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
Chinese cannot be inserted into the MySQL database~!

View database encoding

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 the database encoding

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 results

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)

If you continue to insert at this time, there will be no problem No problem.

It would be great if it was so simple, because the author's settings will become invalid after restarting MySQL!

Continue to look for others! Method

sudo gedit /etc/mysql/my.cnf

Add some 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 it can be restarted, then 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)

It would be great if this was the case. Things are not always as simple as imagined:

Found out when restarting the MySQL service It has been in a waiting state

(PS: Guess a deadlock or something has occurred). At this time, if you execute:

mysql -u root -p

, an exception will be thrown:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I just want to change the encoding, why is it so hard->->->->->!!!I tried it when I solved this problem Many methods (restart, recovery). . .

sudo /etc/init.d/mysql status

Check the status of mysql: mysql respawn/post-start) process 55665 These methods cannot solve the problem, let’s start with the log...

Find the file /var/log/mysql/error.log

Continue to search

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Solution Method.
Answer:

default-character-set=utf8'

under

[ mysqld ] is changed to

character_set_server=utf8

Okay, you can finally restart MySQL, and the encoding set after the restart will still take effect.

Of course the previously created database needs to be re-created T_T

because

show create database bangjob;shows that the previously created data encoding is still latin1Summarize

The above is the detailed content of Introduction to the solution to the problem of Chinese garbled characters in MySQL under Ubuntu. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn