4

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

  • can you post how you setup mariadb server container? i.e. `docker run -e MYSQL_ROOT_PASSWORD=foo --name some-mysql mariadb` – booyaa Sep 16 '15 at 14:26

3 Answers3

11

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.

booyaa
  • 4,437
  • 2
  • 26
  • 32
  • You don't need another container you can use "docker exec" in the same way. – Mr_Thorynque Mar 29 '16 at 13:17
  • 1
    If you use the `mariadb` image, the command is called `mariadb-dump`. [See dockerhub page here](https://hub.docker.com/_/mariadb/) – Thomas Jun 17 '23 at 10:11
4

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.

Tirath
  • 2,294
  • 18
  • 27
1

Accepted answer looks deprecated for mariadb.

Starting with 11.0.1 mariadb, mysqldump is deprecated and removed from the mariadb Docker Official Image. Use mariadb-dump instead. (https://mariadb.com/kb/en/mysqldump/)

Sample mariadb backup command :

docker exec -t <containerID> mariadb-dump -u <USER> -p --all-databases > dump_db_`date +%d-%m-%Y"_"%H_%M_%S`.sql