Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker MariaDB/Mysql dump

How can i mysqldump from running container on https://hub.docker.com/_/mariadb/ ?

I cant find any useful documentation or data?

Any method for backup and restore database.

This is my my continaer run command :

docker run --name myaapp-mariadb -v /databases/maria:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -d mariadb:10

like image 216
sweb Avatar asked Dec 08 '22 01:12

sweb


2 Answers

If we assume you created the mariadb server container this way:

docker run --name some-mariadb -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mariadb:latest

Then you access it from another client container:

docker run -it --link some-mariadb:mysql \
   --rm mariadb:latest \
   sh -c 'exec mysqldump -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD" database_name' > database_name_dump.sql

There's lots more helpful usage tip in the mysql official image page.

like image 81
booyaa Avatar answered Dec 11 '22 09:12

booyaa


Accepted answer stands accepted & correct in all its sense. Adding, this for the case where we have mapped the database to an external volume.

So, for example if the container was created using below command

docker run --name mysqldb -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -v /dir_path_on_your_machine/mysql-data:/var/lib/mysql -d mariadb:latest

then, we can execute the below command from cmd line or terminal

docker exec mysqldb mysqldump --user=root --password=password dbname > /dir_path_on_your_machine/mysql-data/dump/db.sql

However, the dump created using above commands will not dump stored procedures, functions and events. We would need extra params with the in order to do that

--triggers          Dump triggers for each dumped table.
--routines          Dump stored routines (functions and procedures).
--events            Dump events.

So, we can modify our command to include the above params for desired result.

Sample update command

docker exec mysqldb mysqldump --routines --triggers --user=root --password=password dbname > /dir_path_on_your_machine/mysql-data/dump/db1.sql

In case, you encounter any import related error ,, check if this helps.

like image 28
Tirath Avatar answered Dec 11 '22 10:12

Tirath