慢慢赚钱博客

5月 26 2017

docker--创建一个数据库挂载本地物理机目录数据而且进行mysqldump备份

创建并启动mysql的容器,设置好数据库密码

# docker run --name mysqlwp -e MYSQL_ROOT_PASSWORD=123123 \

> -e MYSQL_DATABASE=wordpress \
> -e MYSQL_USER=wordpress \
> -e MYSQL_PASSWORD=wordpresspwd \
> -v /home/docker/mysql:/var/lib/mysql \
> -d mysql
Unable to find image 'mysql:latest' locally
latest: Pulling from library/mysql
10a267c67f42: Pull complete 
c2dcc7bb2a88: Pull complete 
17e7a0445698: Pull complete 
9a61839a176f: Pull complete 
a1033d2f1825: Pull complete 
0d6792140dcc: Pull complete 
cd3adf03d6e6: Pull complete 
d79d216fd92b: Pull complete 
b3c25bdeb4f4: Pull complete 
02556e8f331f: Pull complete 
4bed508a9e77: Pull complete 
Digest: sha256:2f4b1900c0ee53f344564db8d85733bd8d70b0a78cd00e6d92dc107224fc84a5
Status: Downloaded newer image for mysql:latest

8d8795f3fb146e31343be62739deae082d500a6419881f5c9ac8c26434f1a40a

# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
8d8795f3fb14        mysql               "docker-entrypoint..."   2 minutes ago       Up 2 minutes        3306/tcp            mysqlwp

利用docker-enter 进入容器

# docker-enter 8d8795f3fb14 mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

可以查看本地目录的文件:

# pwd
/home/docker/mysql
# ll
total 188680
-rw-r----- 1 systemd-bus-proxy ssh_keys       56 May 26 11:34 auto.cnf
-rw------- 1 systemd-bus-proxy ssh_keys     1675 May 26 11:34 ca-key.pem
-rw-r--r-- 1 systemd-bus-proxy ssh_keys     1074 May 26 11:34 ca.pem
-rw-r--r-- 1 systemd-bus-proxy ssh_keys     1078 May 26 11:34 client-cert.pem
-rw------- 1 systemd-bus-proxy ssh_keys     1679 May 26 11:34 client-key.pem
-rw-r----- 1 systemd-bus-proxy ssh_keys     1321 May 26 11:35 ib_buffer_pool
-rw-r----- 1 systemd-bus-proxy ssh_keys 79691776 May 26 11:35 ibdata1
-rw-r----- 1 systemd-bus-proxy ssh_keys 50331648 May 26 11:35 ib_logfile0
-rw-r----- 1 systemd-bus-proxy ssh_keys 50331648 May 26 11:34 ib_logfile1
-rw-r----- 1 systemd-bus-proxy ssh_keys 12582912 May 26 11:35 ibtmp1
drwxr-x--- 2 systemd-bus-proxy ssh_keys     4096 May 26 11:34 mysql
drwxr-x--- 2 systemd-bus-proxy ssh_keys     4096 May 26 11:34 performance_schema
-rw------- 1 systemd-bus-proxy ssh_keys     1679 May 26 11:34 private_key.pem
-rw-r--r-- 1 systemd-bus-proxy ssh_keys      451 May 26 11:34 public_key.pem
-rw-r--r-- 1 systemd-bus-proxy ssh_keys     1078 May 26 11:34 server-cert.pem
-rw------- 1 systemd-bus-proxy ssh_keys     1675 May 26 11:34 server-key.pem
drwxr-x--- 2 systemd-bus-proxy ssh_keys    12288 May 26 11:34 sys
drwxr-x--- 2 systemd-bus-proxy ssh_keys     4096 May 26 11:35 wordpress

来进行一个备份操作吧:

# docker-enter 8d8795f3fb14 mysqldump --all-databases --password=123123 > wordpress.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

Written by moneyslow.com

moneyslow.com真棒!