Before you begin

In this tutorial will learn how to restore a MySQL database. A Linux machine, Docker and a database dump file will be required to follow this tutorial. You can have a look at this post to easily create a dump file from your MySQL database.

Restore backup to a local or remote MySQL server

Command to restore a local or remote MySQL server database using Docker:

$ cat backup.sql | docker run -i mysql -h [MYSQL_HOST] -u [MYSQL_USER] --password=[MYSQL_PASSWORD] [MYSQL_DATABASE]

Command to restore a local or remote MySQL database using Docker with compression (using gzip):

$ gunzip < backup.sql.gz | docker run -i mysql /usr/bin/mysqldump -h [MYSQL_HOST] -u [MYSQL_USER] --password=[MYSQL_PASSWORD] [MYSQL_DATABASE]


Restore backup into a MySQL Server Docker container

Command to restore a database from plain SQL file:

$ cat backup.sql | docker exec -i [MYSQL_CONTAINER] /usr/bin/mysql -u [MYSQL_USER] --password=[MYSQL_PASSWORD] [MYSQL_DATABASE]

Command to restore a database from compressed gz file:

$ gunzip < backup.sql.gz | docker exec -i [MYSQL_CONTAINER] /usr/bin/mysql -u [MYSQL_USER] --password=[MYSQL_PASSWORD] [MYSQL_DATABASE]
echo “insert into tbl1 values(‘goodbye’, 20);” docker run –rm -i -v pwd:/db test3 db.db
cat file.sql docker run –rm -i -v pwd:/db test3 db.db
echo “select * from tbl1 docker run –rm -i -v pwd:/db test3 db.db
echo “select * from tbl1;” docker run –rm -i -v pwd:/db test3 db.db
hello! 10
goodbye 20