Post

Run MySQL in Docker with character set utf8

MySql image’s default character set is latin1, which would cause problems when storing Chinese characters.

To run MySQL with character set utf8, you can mount a volume to the container and set the character set in the .cnf file:

[mysqld]
character-set-server=utf8
1
docker run -p 3306:3306 -v PATH\init.sql:/docker-entrypoint-initdb.d/init.sql -v PATH\sql.cnf:/etc/mysql/conf.d/sql.cnf -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=mydb -d mysql:latest

The PATH is the path to the directory where the init.sql and sql.cnf files are located.

The init.sql file is used to initialize the database.

The sql.cnf file is used to set the character set to utf8.

This post is licensed under CC BY 4.0 by the author.