Solve the problem of Chinese garbled code in Mysql5.7

  • 2021-07-22 11:44:50
  • OfStack

When using mysql5.7, it will be found that garbled codes will appear after writing Chinese to the database through web, but Chinese will display normally after directly operating SQL statements to insert data in the database. How to solve this problem? The data coding process and principle of MySQL will not be explained here. If you are interested, you can Baidu yourself.

Let's directly use the following operations to solve:

1. Open the mysql console and enter the command show variables like 'character%';

Shows as follows:


   +--------------------------+-------------------------------------------+
| Variable_name                        | Value                                                                                     |
+--------------------------+-------------------------------------------+
| character_set_client         | latin1                                                                                |
| character_set_connection | latin1                                                                                |
| character_set_database    | utf8                                                                                     |
| character_set_results    | latin1                                                                                |
| character_set_server         | utf8                                                                                     |
| character_set_system         | utf8                                                                                     |
| character_sets_dir              | C:\MySQL\MySQL Server 5.0\share\charsets\ |
+--------------------------+-------------------------------------------+

2. Modify the my. ini file


   [client]

        default-character-set=utf8

        [mysqld]
        default-storage-engine=INNODB
        character-set-server=utf8
        collation-server=utf8_general_ci

3. After restarting, use the command in Step 1 to view it as follows:


    +--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+

Also, the default character set for character_set_database and character_set_server is latin1.

4. Enter command: set names utf8;

After restarting, use the Step 1 command to view:


+--------------------------+------------------------------------------------+
| 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 | C:\wamp64\bin\mysql\mysql5.7.9\share\charsets\ |
+--------------------------+------------------------------------------------+

Except that character_set_filesystem is binary, everything else is utf8.

In this way, the web terminal is also set to utf8 coding, which can be normally input into the database.


Related articles: