DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on • Updated on

🐳 Login to MySQL on Docker and run a SQL file

🤔Situation

You maintain an app with MySQL and develop it on Docker. You have a dump file as .sql. Then, you want to run it.

👌Procedure

1. copy the SQL file into the Docker container



$ docker cp hoge.sql [cotaier-id]:/hoge.sql


Enter fullscreen mode Exit fullscreen mode

2. Login to Docker



$ docker ps
$ docker exec -it [container-id] /bin/bash


Enter fullscreen mode Exit fullscreen mode

3. Login to MySQL



$ mysql -u root -p
$ show databases;
$ use [your_database_name];
$ show tables;


Enter fullscreen mode Exit fullscreen mode

💡hint: if you use a rails app, the password and connection informations should be defined at config/database.yml. Perhaps, it is actually written in .env file.

4. Run the SQL file



$ source hoge.sql


Enter fullscreen mode Exit fullscreen mode

Parent Note

Top comments (2)

Collapse
 
akshay_nm profile image
Akshay Kumar

Thank you!

Collapse
 
luispa profile image
LuisPa

Simple and great post!