MySQL container directory
is a special directory dedicated to MySQL containers in Docker.
When running a MySQL container using Docker, if this directory exists and contains SQL scripts or other executable files, Docker automatically executes these scripts at the beginning of the MySQL service.
This mechanism allows for easy initialization settings when starting the container for the first time, such as creating databases, tables, users, and importing data.
Specifically,
File execution in a directory has the following characteristics:
- The execution order is usually sorted by file name.
- support
.sh
Scripts (executable permissions) and SQL scripts (.sql
)。 - These scripts only run when the container's MySQL data volume is empty, i.e. when it is first started. If the data volume already has data (such as container restart), these initialization scripts are not executed again to prevent the data from being overwritten by repeated initializations.
Using this mechanism, developers can use Dockerfile or when running containers-v
Options mount local directory containing initialization scripts to, thereby realizing the automated configuration and data initialization of MySQL containers.
The SQL script underneath has no execution reason when starting docker
The script will be executed when the docker image is started;
The core code on whether to perform database initialization is as follows:
As can be seen from the script, for the SQL file in the directory, it will be judged whether DATABASE_ALREADY_EXISTS is false before execution.
That is, the $DATADIR/mysql directory does not exist;
If any database already exists on the server, the file will definitely not be empty, so the SQL script will not be executed;
docker_setup_env() { # Get config declare -g DATADIR SOCKET DATADIR="$(mysql_get_config 'datadir' "$@")" SOCKET="$(mysql_get_config 'socket' "$@")" # Initialize values that might be stored in a file file_env 'MYSQL_ROOT_HOST' '%' file_env 'MYSQL_DATABASE' file_env 'MYSQL_USER' file_env 'MYSQL_PASSWORD' file_env 'MYSQL_ROOT_PASSWORD' declare -g DATABASE_ALREADY_EXISTS if [ -d "$DATADIR/mysql" ]; then DATABASE_ALREADY_EXISTS='true' fi } _main() { # if command starts with an option, prepend mysqld if [ "${1:0:1}" = '-' ]; then set -- mysqld "$@" fi # skip setup if they aren't running mysqld or want an option that stops mysqld if [ "$1" = 'mysqld' ] && ! _mysql_want_help "$@"; then mysql_note "Entrypoint script for MySQL Server ${MYSQL_VERSION} started." mysql_check_config "$@" # Load various environment variables docker_setup_env "$@" docker_create_db_directories # If container is started as root user, restart as dedicated mysql user if [ "$(id -u)" = "0" ]; then mysql_note "Switching to dedicated user 'mysql'" exec gosu mysql "$BASH_SOURCE" "$@" fi # there's no database, so it needs to be initialized if [ -z "$DATABASE_ALREADY_EXISTS" ]; then docker_verify_minimum_env # check dir permissions to reduce likelihood of half-initialized database ls // > /dev/null docker_init_database_dir "$@" ......
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.