Home > Database > Mysql Tutorial > body text

How to solve the problem of Chinese garbled characters in MySQL

coldplay.xixi
Release: 2020-07-06 16:20:27
forward
2735 people have browsed it

How to solve the problem of Chinese garbled characters in MySQL

##1. MySQL will appear The reason for garbled Chinese charactersWhen we use the MySQL database, we often encounter the problem of garbled characters. See the following code.

    mysql> create table test(id int,name varchar(10));
Query OK, 0 rows affected (0.01 sec)

    mysql> insert into test values(1,'宋蔚然');
    ERROR 1366 (HY000): Incorrect string value: '\xE5\xAE\x8B\xE8\x94\x9A...' for column 'name' at row 1
    mysql>
Copy after login
Copy after login

Related learning recommendations:

mysql video tutorial

Obviously, when inserting Chinese, an error is reported. What is the reason?

    mysql> show variables like '%CHARACTER%';
    +--------------------------+----------------------------+
    | 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/ |
    +--------------------------+----------------------------+
Copy after login
Copy after login

character_set_client The character encoding used by the client

character_set_connection The encoding used by the database link
character_set_database The character encoding used by the database
It turns out that the character encoding is not unified with the encoding of the server and the database. of.

2. The solution to Chinese garbled characters appearing in MySQL
Method 1: Set names

    mysql> set names latin1;
    
    mysql> set names latin1;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select * from test;
    Empty set (0.00 sec)
    
    mysql> insert into test values(1,'宋蔚然');
    Query OK, 1 row affected (0.01 sec)
    
    mysql> select * from test;
    +------+-----------+
    | id   | name      |
    +------+-----------+
    |    1 | 宋蔚然    |
    +------+-----------+
    1 row in set (0.00 sec)
Copy after login
Copy after login

Come again Take a look at the character set settings

     mysql> show variables like '%CHARACTER%';
    +--------------------------+----------------------------+
    | Variable_name            | Value                      |
    +--------------------------+----------------------------+
    | character_set_client     | latin1                     |
    | character_set_connection | latin1                     |
    | character_set_database   | latin1                     |
    | character_set_filesystem | binary                     |
    | character_set_results    | latin1                     |
    | character_set_server     | latin1                     |
    | character_set_system     | utf8                       |
    | character_sets_dir       | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+
Copy after login
Copy after login

If the character encoding is unified, there will be no garbled characters.

Solving the problem of garbled characters is actually to unify the character encoding of the client with the encoding of the server and database. The server and database encodings here are latin1, and all set names latin1 can temporarily solve the garbled problem.

Method 2: Modify the database configuration file character set to UTF8UTF8 supports many language systems, so it is strongly recommended to set the character encoding to UTF8 in production. Open the database configuration file and add the following content under [client], [mysql], and [mysqld] respectively.

    #vi /mysql/data/3306/my.cnf
    
    [client]
    default-character-set=utf8
    
    [mysql]
    default-character-set=utf8
    
    [mysqld]
    default-storage-engine=INNODB
    character-set-server=utf8
    collation-server=utf8_general_ci
Copy after login
Copy after login

Restart the database

   [root@test ~]# systemctl restart mysqld
Copy after login
Copy after login

rebuilding the creation library and table

    mysql> create database test;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> use test;
    Database changed
    mysql> create table test(id int,name varchar(10));
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> insert into test values(1,'宋蔚然');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from test;
    +------+-----------+
    | id   | name      |
    +------+-----------+
    |    1 | 宋蔚然    |
    +------+-----------+
    1 row in set (0.00 sec)
Copy after login
Copy after login

again to look at the settings of the character set

    mysql> show variables like '%CHARACTER%';
    +--------------------------+----------------------------+
    | 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/ |
    +--------------------------+----------------------------+
Copy after login
Copy after login

#1. Reasons why Chinese garbled characters appear in MySQL
When we use the MySQL database, we often encounter garbled characters. Question, look at the code below.

    mysql> create table test(id int,name varchar(10));
Query OK, 0 rows affected (0.01 sec)

    mysql> insert into test values(1,'宋蔚然');
    ERROR 1366 (HY000): Incorrect string value: '\xE5\xAE\x8B\xE8\x94\x9A...' for column 'name' at row 1
    mysql>
Copy after login
Copy after login

Obviously, an error is reported when inserting Chinese characters. What is the reason?

    mysql> show variables like '%CHARACTER%';
    +--------------------------+----------------------------+
    | 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/ |
    +--------------------------+----------------------------+
Copy after login
Copy after login

character_set_client The character encoding used by the client
character_set_connection The encoding used by the database link
character_set_database The character encoding used by the database
It turns out that the character encoding is not unified with the encoding of the server and the database. of.

2. The solution to Chinese garbled characters appearing in MySQL
Method 1: Set names

    mysql> set names latin1;
    
    mysql> set names latin1;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select * from test;
    Empty set (0.00 sec)
    
    mysql> insert into test values(1,'宋蔚然');
    Query OK, 1 row affected (0.01 sec)
    
    mysql> select * from test;
    +------+-----------+
    | id   | name      |
    +------+-----------+
    |    1 | 宋蔚然    |
    +------+-----------+
    1 row in set (0.00 sec)
Copy after login
Copy after login

Come again Take a look at the character set settings

     mysql> show variables like '%CHARACTER%';
    +--------------------------+----------------------------+
    | Variable_name            | Value                      |
    +--------------------------+----------------------------+
    | character_set_client     | latin1                     |
    | character_set_connection | latin1                     |
    | character_set_database   | latin1                     |
    | character_set_filesystem | binary                     |
    | character_set_results    | latin1                     |
    | character_set_server     | latin1                     |
    | character_set_system     | utf8                       |
    | character_sets_dir       | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+
Copy after login
Copy after login

If the character encoding is unified, there will be no garbled characters.
Solving the problem of garbled characters is actually to unify the character encoding of the client with the encoding of the server and database. The server and database encodings here are latin1, and setting all names latin1 can temporarily solve the garbled problem.

Method 2: Modify the database configuration file character set to UTF8
UTF8 supports many language systems, so it is strongly recommended to set the character encoding to UTF8 in production. Open the database configuration file and add the following content under [client], [mysql], and [mysqld] respectively.

    #vi /mysql/data/3306/my.cnf
    
    [client]
    default-character-set=utf8
    
    [mysql]
    default-character-set=utf8
    
    [mysqld]
    default-storage-engine=INNODB
    character-set-server=utf8
    collation-server=utf8_general_ci
Copy after login
Copy after login

Restart the database

   [root@test ~]# systemctl restart mysqld
Copy after login
Copy after login

Rewrite the created library and table

    mysql> create database test;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> use test;
    Database changed
    mysql> create table test(id int,name varchar(10));
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> insert into test values(1,'宋蔚然');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from test;
    +------+-----------+
    | id   | name      |
    +------+-----------+
    |    1 | 宋蔚然    |
    +------+-----------+
    1 row in set (0.00 sec)
Copy after login
Copy after login

Let’s take a look at the character set settings

    mysql> show variables like '%CHARACTER%';
    +--------------------------+----------------------------+
    | 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/ |
    +--------------------------+----------------------------+
Copy after login
Copy after login

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

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template