23

I use the official MySQL docker image, and I am having difficulty exporting data from the instance without errors. I run my export like this:

docker run -it --link containername:mysql --rm mysql sh -c 
    'exec mysqldump 
        -h"$MYSQL_PORT_3306_TCP_ADDR" 
        -P"$MYSQL_PORT_3306_TCP_PORT" -uroot 
        -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD" 
     dbname'
| gz > output.sql.gz

However, this results in the warning:

"mysqldump: [Warning] Using a password on the command line interface can be insecure."

As the first line of the outputted file. Obviously this later causes problems for any other MySQL processes which are used to consume the data.

Is there any way to suppress this warning from the mysqldump client?

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
DMCoding
  • 1,167
  • 2
  • 15
  • 30

6 Answers6

67

A little late to answer but this command saved my day.

docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
priteshbaviskar
  • 2,139
  • 2
  • 20
  • 27
11

I realise that this is an old question, but for those stumbling across it now I put together a post about exporting and importing from mysql docker containers: https://medium.com/@tomsowerby/mysql-backup-and-restore-in-docker-fcc07137c757 It covers the "Using a password on the command line interface..." warning and how to bypass it.

tomsowerby
  • 498
  • 1
  • 3
  • 10
6

Run Following command on terminal

docker exec CONTAINER_id /usr/bin/mysqldump -uusername --password=yourpassword databasename> backup.sql

Replace the

  1. CONTAINER_id. username, yourpassword

with specific to your configuration.

To get Container Id :

docker container ls
Ankur Garg
  • 151
  • 2
  • 2
4

Here's how I solved this to dump a mysql db into a file.

I created a dump-db.sh file with the content:

# dump db from docker container
(docker exec -i CONTAINER_ID mysqldump -u DB_USER -pDB_PASS DB_NAME) > FILENAME.sql
  • To get the CONTAINER_ID list them: docker container list

Add run permissions to the script: chmod o+x dump-db.sh

Run it: ./dump-db.sh

Remember to replace the CONSTANTS above with your own data.

avlnx
  • 676
  • 1
  • 6
  • 19
3

To eliminate this exact warning you can pass password in MYSQL_PWD environment variable or use other connection method - see http://dev.mysql.com/doc/refman/5.7/en/password-security-user.html

docker run -it --link containername:mysql --rm mysql sh -c 
    'export MYSQL_PWD="$MYSQL_ENV_MYSQL_ROOT_PASSWORD"; exec mysqldump 
        -h"$MYSQL_PORT_3306_TCP_ADDR" 
        -P"$MYSQL_PORT_3306_TCP_PORT" -uroot 
     dbname'
| gz > output.sql.gz
Vasfed
  • 18,013
  • 10
  • 47
  • 53
  • How is that accomplished with the official docker image? – DMCoding Jan 13 '16 at 18:07
  • 1
    @DanielJames this has little to do with docker itself - you're still dealing with shell scripts, updated answer with example command. Also you can pass env variables from outside with docker's `-e` flag – Vasfed Jan 13 '16 at 18:57
0

I always create bash "tools" in my repo root with which I can repeat common tasks, such as database dumps. With bash, you can also load your .env file, so your credentials are not in a file in the repo, but just in your .env file.

#!/bin/bash

# load .env
set -o allexport; . ./.env; set +o allexport

# setup
TIMESTAMP=$(date +%Y-%m-%d__%H.%M)
BACKUP_DIR="dockerfiles/db"
CONTAINER_NAME="cp-db"

# dump
docker exec $CONTAINER_NAME /usr/bin/mysqldump -u$DB_USER --password=$DB_PASSWORD $DB_NAME> $BACKUP_DIR/dump__$TIMESTAMP.sql
Jos
  • 1,387
  • 1
  • 13
  • 27