Mysql container initialization

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh.sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.

Sh,.sql and.sql. Gz files in the /docker-entrypoint-initdb.d folder are executed when the container is started for the first time.

/docker-entrypoint-initdb.d /docker-entrypoint-initdb.d /docker-entrypoint-initdb.d

FROM mysql:8.0.27

# Environment Variables

# MYSQL_ROOT_PASSWORD required. Password set for the MySQL root superuser account.
# MYSQL_DATABASE Optional. Specifies the name of the database to be created when the image starts.

ENV MYSQL_DATABASE=mall \ 
    MYSQL_ROOT_PASSWORD=123456

ADD mall.sql /docker-entrypoint-initdb.d

EXPOSE 3306
Copy the code

Then run the build and run commands

$ docker build -t second-init-mysql .
$ docker run --name second-init-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 second-init-mysql
Copy the code

Also attached is the command to export the database:

mysqldump -h -u -p --no-data > schema.sql
Copy the code

Pit: The mysql version used by the exported database must be the same as that used by the imported database; otherwise, errors may occur.

Reference:

1, hub.docker.com/_/mysql (/docker-entrypoint-initdb.d)

2, stackoverflow.com/questions/2… Initialize a MySQL database with schema