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

1
  • 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, 2015 at 14:26

3 Answers 3

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 maria-dump -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

Note: the MySQL equivalent binary is called mysqldump

Finally, you can use the docker exec command to shell into the container running the MariaDB/MySQL server and use the client tools there.

There are lots more helpful usage tips in the MySQL and MariaDB official image pages.

Updates to answer via Mr_Thorynque and Thomas.

2
  • You don't need another container you can use "docker exec" in the same way. Mar 29, 2016 at 13:17
  • 1
    If you use the mariadb image, the command is called mariadb-dump. See dockerhub page here
    – Thomas
    Jun 17 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.

2

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.