Docker Tips — How to import database in MySQL in Docker?
2 min readJul 1, 2021
First, We must know what our MYSQL Container name first. We can check it by this command
docker ps
then docker will show us container list like this example
Then, we must identification our MYSQL container. In this example, my MYSQL container name is app_database_1
.
After we know our MYSQL container name, we can upload our sql file with this command.
docker exec -i [mysql_container_name] mysql -u[username] -p[password] [DB name] < [path/to/sql/file]
I will explain this command.
docker exec -i
: command at docker to execute something in docker that keep STDIN open even if not attached.[mysql_container_name]
: change it with your container name. In this case, i will change it toapp_database_1
.mysql -u[username] -p[password] [DB name]
: this command is to access your database. In this case, I will change[username]
to my DB hostname, I will change[password]
to my DB password, and I will change[DB name]
to my DB name.< [path/to/sql/file]
: mysql command to import sql file. In this case we can change to our sql file path.
Then, voila! It will execute our sql file to the our MYSQL database container.
If this short tips still make you confuse, please comment down in this thread. I will try to help.