DEV Community

Cover image for Import database to docker with terminal
mohammad hassani
mohammad hassani

Posted on

Import database to docker with terminal

Hey, this is Mohammad Hassani 😃

if you are developing with Linux and wanna import database to docker with terminal you can use this tutorial...

It's just 4 steps

1) copy database to _data folder:

first of all you should copy your .sql file to var/lib/docker/volumes/blog_blogmysql/_data

in terminal:

cp DATABASE.sql var/lib/docker/volumes/blog_blogmysql/_data
Enter fullscreen mode Exit fullscreen mode

// Replace DATABASE to your database file name

2) execute docker

now your database is ready to import, in this step execute docker in terminal,
(docker desktop is not supported in Linux but you can install dockstation for Linux, they are similar. and then you can execute there)

also in terminal:

docker exec -it CONTAINERNAME bash
Enter fullscreen mode Exit fullscreen mode

// Replace CONTAINERNAME to your container name like site_mysql_1

3) Bring up mysql

now open mysql in terminal:

mysql -uroot -p DATABASENAME
Enter fullscreen mode Exit fullscreen mode
  • if you dont have database in container you can use
mysql -uroot -p
Enter fullscreen mode Exit fullscreen mode

and then:

CREATE DATABASE DATABASENAME;
Enter fullscreen mode Exit fullscreen mode

// Replace DATABASENAME to your database name

4) source .sql file

ok! everything is set now and ready to go...
in terminal:

source var/lib/mysql/DATABASE.sql
Enter fullscreen mode Exit fullscreen mode

it maybe take some time depends on your .sql file size and your computer.

better way

  • you can use this code instead of step 3 and 4:
mysql --init-command="set autocommit=0" DATABASENAME < var/lib/mysql/DATABASE.sql
Enter fullscreen mode Exit fullscreen mode

that's it!

if you have another way, leave a message :)

Be happy! ;)

buy me a coffee

Top comments (0)