5

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

1
  • Important to know: I start my container with this command: docker run -d --name mysql --rm -p 3306:3306 -v mysql-dump:/docker-entrypoint-initdb.d:ro -v datastorage:/var/lib/mysql:rw christian/mysql-test:1.0 Commented Jun 20, 2018 at 12:34

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.