Home > Database > Mysql Tutorial > How to Initialize a MySQL Database with a Schema in Docker?

How to Initialize a MySQL Database with a Schema in Docker?

Patricia Arquette
Release: 2025-01-05 08:26:47
Original
653 people have browsed it

How to Initialize a MySQL Database with a Schema in Docker?

Initializing MySQL Database with Schema in Docker

When attempting to create a Docker container with a MySQL database and add a schema, you may encounter challenges in executing the desired commands successfully. Here's a comprehensive solution to resolve this issue:

  1. Dump MySQL Schema to File: Execute the following command to export your MySQL schema to a file:
mysqldump -h <your_mysql_host> -u <user_name> -p --no-data <schema_name> > schema.sql
Copy after login
  1. Update Dockerfile: Modify your Dockerfile to include the ADD command, which adds the schema file to the /docker-entrypoint-initdb.d directory within the container:
FROM mysql:5.7.15

MAINTAINER me

ENV MYSQL_DATABASE=<schema_name> \
    MYSQL_ROOT_PASSWORD=<password>

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

EXPOSE 3306
Copy after login

The docker-entrypoint-initdb.d directory contains scripts that run during database initialization, including SQL files.

  1. Start Docker MySQL Instance: Build and run your Docker MySQL instance using Docker Compose:
docker-compose build
docker-compose up
Copy after login

By following these steps, you can effectively initialize a MySQL database with a schema within a Docker container.

The above is the detailed content of How to Initialize a MySQL Database with a Schema in Docker?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template