banana-joe
banana-joe

Reputation: 61

How to import an existing MySQL-Database in my MySQL-Docker Container?

I have created a docker container based on the official image of MySQL from Docker Hub. It works fine, but I have some troubles with the database import.

My file with the SQL-Instructions is already stored in the folder /docker-entrypoint-initdb.d of my container, but it doesn't work! I have copied my sql-import.sql to /var/lib/docker/volumes/mysql-dump/_data, but I can only see the name of my database when I call "SHOW DATABASES;" within my container. There is no table available when I call "SHOW TABLES FROM myDB;". What can I do to import the content of my MySQL Database?

Here is my Dockerfile:

FROM mysql:5.7
ADD ./init-scripts/*.sql /docker-entrypoint-initdb.d/
ADD ./config/my.cnf /root/
RUN cd /root/ && \
chmod 0600 my.cnf && \
mv my.cnf .my.cnf

ENV MYSQL_DATABASE=regex
ENV MYSQL_ROOT_PASSWORD=mypassword

EXPOSE 3306

MySQL View in the MySQL-Container

Upvotes: 5

Views: 6239

Answers (1)

GoA Oz
GoA Oz

Reputation: 328

There are several methods to import. With docker exec if your contener is running:

Solution 1:

docker exec -i <id_conteneur> /usr/bin/mysql -u <fooUser> -e "CREATE DATABASE mydb"

cat schema.sql | docker exec -i <id_conteneur> /usr/bin/mysql -u <fooUser> --password=<password> <database>

Solution 2:

In a bash script:

#!/bin/bash 
/usr/bin/mysqld_safe --skip-grant-tables &
mysql -u <fooUser> -e "CREATE DATABASE mydb" 
mysql -u <fooUser> mydb < schema.sql

And add to your DockerFile with Run command:

ADD create_db.sh /tmp/create_db.sh
RUN /tmp/create_db.sh

Upvotes: 3

Related Questions