The way to run MySQL using docker-compose
directory structure
.│ .env│ docker-compose.yml│└─mysql ├─config │ my.cnf │ └─data
mysql directory data for the data directory, mysql data table, binary log files here. The env file contains variables that can be referenced in the docker-compose.yml file as ${variable_name}.
Of course, you can also put mysql directory to other places, here figure a convenient, directly placed in the yml file peer directory.
.env file
MYSQL_ROOT_PASSWORD=rootMYSQL_ROOT_HOST=%MYSQL_DIR=./ mysql
MySQL configuration file my.cnf
[mysqld]character-set-server=utf8mb4default-time-zone='+8:00'innodb_rollback_on_timeout='ON'max_connections=500innodb_lock_wait_timeout=500
This file can be omitted if the default configuration is used.
docker-compose.yml
version: '3'services: mysql-db: container_name: mysql-docker #Specify the name of the container image: mysql:8.0 #Specify mirror and version ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} MYSQL_ROOT_HOST: ${MYSQL_ROOT_HOST} volumes: - "${MYSQL_DIR}/data:/var/lib/mysql" #Mount data directory- "${MYSQL_DIR}/config:/etc/mysql/conf.d" #Mount configuration file directory
Environment variable
MYSQL_ROOT_PASSWORD: This unexplained, root password. MYSQL_USER, MYSQL_PASSWORD: These two variables are optional and create a new user with superuser privileges on the database specified by the MYSQL_DATABASE variable. MYSQL_DATABASE: Specifies a database to be created at container startup. MYSQL_ALLOW_EMPTY_PASSWORD: Set to yes to allow root user passwords to be blank. MYSQL_RANDOM_ROOT_PASSWORD: Set to yes to generate a random password for root at container startup, and the password appears in the standard output stream (GENERATED ROOT PASSWORD: ...). MYSQL_ONETIME_PASSWORD: Literally means one-time password, set for root user, password must be changed after first login (only supports version 5.6 and above).
run containers
Execute in docker-compose.yml directory:
> docker-compose up
If you want to run in the background, use docker-compose up -d.
Stop container:
> docker-compose down
If running foreground, use Ctrl + C to stop. Both methods delete containers after stopping, and the next time you start, you must use the up command.
Stop without deleting containers:
> docker-compose stop
After stopping with stop, start again with start command.
summary
The above is a small series to introduce you to the use of docker-compose MySQL method, I hope to help you, if you have any questions please give me a message, small series will reply to you in time. Thank you very much for your support! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!