Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to import database in MySQL in Docker?

This is a simple way of importing MySQL database in Docker.

  1. In you Dockerfile you must have a shared folder. Shared folder is a directory in your host machine that is mounted to Docker instance.

  2. Put the exported sql file in the shared folder.

  3. Login to your Docker instance via docker exec -it DOCKER_CONTAINER_ID bin/bash.

  4. Login to MySQL via mysql -u USERNAME -p.

  5. While in MySQL CLI, create a database via create database DB_NAME;.

  6. While in MySQL CLI, use the database you just created via use DB_NAME;.

  7. While in MySQL CLI, import the sql file via source /path/to/file.sql.

Done

@selmonal

This comment has been minimized.

Copy link

selmonal commented Mar 20, 2019

How can i attach the sql file into running mysql docker container?

@selmonal

This comment has been minimized.

Copy link

selmonal commented Mar 20, 2019

Just run the simple command below:

docker exec -i mysql_container mysql -uroot -psecret mysql < db.sql
@deya-eldeen

This comment has been minimized.

Copy link

deya-eldeen commented Apr 23, 2019

docker exec -i container_name mysql -uroot dbname < data.sql;

@mohamedhafezqo

This comment has been minimized.

Copy link

mohamedhafezqo commented Jul 26, 2019

👍

@v3nt

This comment has been minimized.

Copy link

v3nt commented Jul 26, 2019

having a few issues with this. Tried the above and get an error.

docker exec -i 272ed5ba7607 mysql -uroot -p"$MYSQL_ROOT_PASSWORD" mysql < wordpress-database-2.sql

OCI runtime exec failed: exec failed: container_linux.go:344: starting container process caused "exec: \"mysql\": executable file not found in $PATH": unknown

any ideas ? thanks!

@Melzmann

This comment has been minimized.

Copy link

Melzmann commented Aug 13, 2019

I think you used the wrong container ID. The message says he doesn't know the command "mysql" inside the container.

@thamvtn123

This comment has been minimized.

Copy link

thamvtn123 commented Sep 30, 2019

docker exec -i [mysql_container_name] mysql -u[username] -p[password] [DB name] < [path/to/sql/file]

@jengeb

This comment has been minimized.

Copy link

jengeb commented Oct 21, 2019

Thanks for the manual!
$ docker cp file.sql <CONTAINER ID>:/file.sql worked for me.

@mpaz-redstage

This comment has been minimized.

Copy link

mpaz-redstage commented Dec 27, 2019

docker exec -i $container mysql -umagento -pmagento magento <<< "source $file"

@vinaysudani

This comment has been minimized.

Copy link

vinaysudani commented Jan 28, 2020

Just run the simple command below:

docker exec -i mysql_container mysql -uroot -psecret mysql < db.sql

👍

@k3z

This comment has been minimized.

Copy link

k3z commented Feb 1, 2020

I had to add -h127.0.0.1 to make it work

docker exec -i mysql_container mysql -h127.0.0.1 -uroot -psecret mysql < db.sql
@cpu100

This comment has been minimized.

Copy link

cpu100 commented Feb 13, 2020

spawn docker exec -i $mysql_container bash -l -c "mysql -uroot -p < /tmp/template.sql"
expect "*Enter password*"
send "thepassword\n"

Like above, no matter how I try, the password is always error:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
I guess the reason is that the password is confused with file stream stdin.

And the following seem to work:
docker exec -i $mysql_container bash -l -c "mysql -uroot -pthepassword < /tmp/template.sql"

@goroba

This comment has been minimized.

Copy link

goroba commented Feb 21, 2020

Using PowerShell in Window 10 gives

The '<' operator is reserved for future use.

I wrapped it up with cmd /c and it works:

cmd /c "docker exec -i $mysql_container mysql -uuser -ppassword dbname < data.sql"

@fheuer

This comment has been minimized.

Copy link

fheuer commented Mar 4, 2020

Using docker-compose you can import mysql.sql file as volume thus inserting into database on startup.

volumes: - ./datadump.sql:/docker-entrypoint-initdb.d/datadump.sql

@moroChev

This comment has been minimized.

Copy link

moroChev commented Apr 10, 2020

Guys where the data.sql file must be exactly ?

@JuanVqz

This comment has been minimized.

Copy link

JuanVqz commented Apr 11, 2020

@itsmeDontworry it means your backup file

docker exec -i mysql_container mysql -uroot -psecret database < database_backup.sql

From left to right:
docker it's the docker command
exec When you want yo execute some command an actually running container
mysql_container it's your running container
mysql it's the command itself like mysql --version
-u It asks for your MySQL user like -uroot
-p It asks for your MySQL user like -ppassword
database It's your database name, previously created
database_backup.sql It's your backup from other database or same database

@flouet-company

This comment has been minimized.

Copy link

flouet-company commented Apr 18, 2020

Thanks for the manual!
$ docker cp file.sql <CONTAINER ID>:/file.sql worked for me.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.