Docker MariaDB/Mysql dump
Asked Answered
C

5

7

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

Ceasefire answered 16/9, 2015 at 14:0 Comment(1)
can you post how you setup mariadb server container? i.e. docker run -e MYSQL_ROOT_PASSWORD=foo --name some-mysql mariadbSundew
S
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.

Sundew answered 16/9, 2015 at 14:31 Comment(2)
You don't need another container you can use "docker exec" in the same way.Sledge
If you use the mariadb image, the command is called mariadb-dump. See dockerhub page hereGeordie
A
6

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
Alannaalano answered 1/7, 2023 at 7:59 Comment(0)
W
5

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.

Wingo answered 29/9, 2020 at 6:23 Comment(0)
W
1

Way to migrate MariaDB from one docker container to second:

docker exec -it seafile-db mariadb-dump -uroot -p{password} --opt ccnet-db > ccnet_db.sql

docker cp ccnet_db.sql seafile-mysql:/tmp/ccnet_db.sql

docker exec -it seafile-mysql /bin/sh -c "mysql -uroot -p{password} ccnet_db < /tmp/ccnet_db.sql"

You can't use mysqldump because it isn't part of docker container when created by: docker run mariadb...

Whippet answered 17/1 at 22:25 Comment(0)
R
1

You can access the Docker container and install the MySQL client tools inside it. Here's how you can do it:

First, access your Docker container using the following command:

docker exec -it container-id bash

Once inside the container, install the MySQL client tools. On Debian/Ubuntu-based systems, you can typically install it with:

apt update 
apt install mysql-client

Run mysqldump from Host Environment: Instead of running mysqldump from within the Docker container, you can run it directly from your host environment. This requires having MySQL client tools installed on your host machine.

Here's how you can do it:

mysqldump -h 127.0.0.1 -P 3306 -u username -pkus password > path.dump

Replace path.dump argument with the absolute path to the directory on your host machine where you want to save the dump file.

Ryeland answered 9/3 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.