Home >Database >Mysql Tutorial >Docker official mysql image custom configuration method
Copy code The code is as follows:
$ docker run --name some-mysql -e mysql_root_password=my-secret-pw -d daocloud.io/mysql:tag
some-mysql The name of the container is specified, my-secret-pw specifies the password of the root user, and the tag parameter specifies the mysql version you want
This way the data is not persisted, so it needs to be mounted in the startup parameters Local directory
So the database keeps running like this, but because the program recently needs to support emoji expressions, the character set of mysql has to be changed.
Copy code The code is as follows:
$ docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e mysql_root_password=my-secret-pw -d daocloud.io/mysql:tag
At this time, you can mount the custom configuration file. The official document states that
When the mysql service starts, it will be /etc/mysql/my.cnf As a configuration file, this file will import all files with the .cnf suffix in the /etc/mysql/conf.d directory. These files extend or override the configuration in the /etc/mysql/my.cnf file. So you can create your own configuration file and mount it to the /etc/mysql/conf.d directory in the mysql container.
So the easiest way to change the database configuration is to create a new configuration file on the host machine and change it to utf8mb4
[client] default-character-set=utf8mb4 [mysqld] character-set-client-handshake = false character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci [mysql] default-character-set=utf8mb4
Then copy the file to the corresponding docker container folder
docker cp /home/my.cnf(宿主机文件路径) [容器id]:/etc/mysql/mysql.conf.d
Finally, use the docker stop and start commands to restart the container to load the custom configuration.
The container configured by the official mysql image of docker cannot start the problem
I am using the docker image of mysql. First create and start the image:
# docker run --name mysql-b \ > -p 33002:3306 -v /zc/mysql/datadir-b:/var/lib/mysql \ > -e mysql_root_password='123456' -d mysql:latest
Start normally, no problem. Usually when we use mysql, we need to set parameters. To set parameters, we must first enter the bash of the container and perform the operation:
docker exec -it mysql-b bash
The default configuration file of mysql is the /etc/mysql/my.cnf file. If you want to customize the configuration, it is recommended to create a .cnf file in the /etc/mysql/conf.d directory. The newly created file can be named arbitrarily, as long as the suffix is cnf. The configuration items in the newly created file can override the configuration items in /etc/mysql/my.cnf. Because the official docker image of mysql does not provide a vim editor, I used the cat command to generate the file and add the content:
# cat >test.cnf <<eof [mysqldump] user=root password='123456' [mysqld] max_allowed_packet=8m lower_case_table_names=1 character_set_server=utf8 max_connections=900 max_connect_errors=600 default-character-set=utf8 eof
After exiting, I stopped the container and then restarted the container. I found that the container could not be started.
Solution
Delete the original container that cannot be started. Re-create a new container. The crux of the problem is that the original test.cnf file has errors. Find the last line of the original configuration file:
default-character-set=utf8
Delete this line. Just make sure there is no such line when adding the configuration file.
Cause of the problem
In the official docker image of mysql, under the label latest, there is no default-character-set in the [mysqld] configuration section this configuration item.
If you want to view all configuration items, you can use the following command and use the pipeline to put the output help into the help.txt file:
docker run -it --rm mysql:tag --verbose --help > help.txt
where tag represents the label of the image, such as latest and 5.6 .
The above is the detailed content of Docker official mysql image custom configuration method. For more information, please follow other related articles on the PHP Chinese website!