docker mysql Chinese garbled code
When using a Docker container to deploy a MySQL database, sometimes garbled Chinese characters may appear. This is because the default character set of MySQL is Latin1, and Chinese characters need to be encoded in UTF-8. This article will introduce how to solve the Chinese garbled problem of docker mysql.
1. Check the current MySQL character set
First, we need to check the current MySQL character set. You can use the following command after logging in to MySQL:
mysql> show variables like '%char%';
Among them, the character set related variables are:
- character_set_client: client character set, which is the client that connects to MySQL. The character set used;
- character_set_connection: connection character set, that is, the character set between the server and the client;
- character_set_database: database character set, the newly created database will be based on this character set Create;
- character_set_results: result character set, the character set used in the results returned by the SELECT query;
- character_set_server: server character set, the character set used by the MySQL server itself.
2. Modify the MySQL character set to UTF-8
After checking that the current MySQL character set is Latin1, we need to modify it to UTF-8. This can be achieved in the following two ways:
- Modify the MySQL configuration file
In the MySQL configuration file (usually /etc/my.cnf or /etc/mysql/ my.cnf), add the following two lines:
[client] default-character-set=utf8 [mysqld] character-set-server=utf8
Among them, [client] is used to set the client character set to UTF-8, and [mysqld] is used to set the server character set to UTF-8. After the modification is completed, restart the MySQL service:
sudo service mysql restart
- Modify the character set after connecting to MySQL
If you cannot modify the MySQL configuration file, you can manually modify the character set after connecting to MySQL. You can connect to MySQL through the following command:
mysql --default-character-set=utf8 -u用户名 -p密码 数据库名
After the connection is successful, execute the following commands in sequence to modify the character set:
SET character_set_client = utf8; SET character_set_connection = utf8; SET character_set_database = utf8; SET character_set_results = utf8; SET character_set_server = utf8;
After the modification is completed, exit MySQL and log in again to take effect.
3. Use the UTF-8 character set in the Docker container
Since MySQL in the Docker container runs based on the image, we need to set the UTF-8 character set in the image. The MySQL image can be built through the following Dockerfile:
FROM mysql:latest ENV MYSQL_ROOT_PASSWORD=root ENV MYSQL_DATABASE=test ENV MYSQL_CHARSET=utf8 ENV MYSQL_COLLATION=utf8_general_ci COPY ./init.sql /docker-entrypoint-initdb.d/
Among them, the ENV command is used to set environment variables, MYSQL_CHARSET is used to set the MySQL character set to UTF-8, and MYSQL_COLLATION is used to set the collation rule to utf8_general_ci. The initialization script init.sql is also copied to the image through the COPY command for automatic execution when starting the container.
4. Summary
Through the above three steps, we can use MySQL in the Docker container and set the character set to UTF-8 to avoid the problem of Chinese garbled characters. Of course, if you need to handle multilingual character sets, you need to make adjustments according to the specific situation. Hope this article can be helpful to everyone.
The above is the detailed content of docker mysql Chinese garbled code. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

DockerVolumes ensures that data remains safe when containers are restarted, deleted, or migrated. 1. Create Volume: dockervolumecreatemydata. 2. Run the container and mount Volume: dockerrun-it-vmydata:/app/dataubuntubash. 3. Advanced usage includes data sharing and backup.
