Solve the problem of Docker installing MySQL Chinese garbled

Problem description: After installing mysql using docker, Chinese garbled characters appear as follows;

The solution is as follows:

1. Start mysql:

docker start mysql

2. Enter the container:

docker exec -it mysql /bin/bash

3. Log in to mysql:

mysql -u root -p

4. View the default character set of the database:

SHOW VARIABLES LIKE 'character_set_%';

As you can see, the default is latin1, and connection is the encoding specified when we connect through the client.

The problem of garbled external access data lies in the connection layer.

5. Modify the character set to utf-8 format:

SET NAMES'utf8mb4'; is equivalent to the following three instructions:

SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;

6. Modify the configuration file:

① Enter the configuration file directory and install the necessary software as follows:

② Modify my.cnf

  • [mysqld] Add 2 lines under the label

        default-character-set = utf8mb4

        character_set_server = utf8mb4

  • [mysql] Add a line under the label

        default-character-set = utf8mb4

  • [mysql.server] Add a line under the label

        default-character-set = utf8mb4

  • [mysqld_safe] Add a line under the label

        default-character-set = utf8mb4

  • Add a line under the [client] tag

        default-character-set = utf8mb4

7. Check whether the configuration is successful

As you can see, the character set has been successfully modified. Next, see if the Chinese garbled data in the data table is resolved, as follows:

Successfully resolved!

 

If there is a problem outside the article, you can privately write to the author.

Guess you like

Origin blog.csdn.net/m0_38023584/article/details/105584623