[root@localhost local]# docker pull mysql:5.6
5.6: Pulling from library/mysql
802b00ed6f79: Pull complete
30f19a05b898: Pull complete
3e43303be5e9: Pull complete
94b281824ae2: Pull complete
51eb397095b1: Pull complete
3f6fe5e46bae: Pull complete
b5a334ca6427: Pull complete
115764d35d7a: Pull complete
719bba2efabc: Pull complete
284e66788ee1: Pull complete
0f085ade122c: Pull complete
Digest: sha256:4c44f46efaff3ebe7cdc7b35a616c77aa003dc5de4b26c80d0ccae1f9db4a372
Status: Downloaded newer image for mysql:5.6
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhaoyoungtomcat9 latest 124517434916 About an hour ago 751MB
myip_son latest 6c9507aea358 2 days ago 398MB
myip_father latest 2c22e721607a 2 days ago 299MB
myip2 latest dcbb4656e640 2 days ago 299MB
myip latest 9e3c14f76b1d 2 days ago 299MB
mycentos 1.0 c2d4f6acb9af 2 days ago 455MB
zhaoyoung/nodocstomcat 1.0 84498728984a 4 days ago 463MB
centos latest 75835a67d134 5 days ago 200MB
tomcat latest 41a54fe1f79d 4 weeks ago 463MB
hello-world latest 4ab4c602aa5e 5 weeks ago 1.84kB
mysql 5.6 1f47fade220d 5 weeks ago 256MB
[root@localhost local]# docker run -p 12345:3306 --name mysql -v /zhaoyoung/mysql/conf:/etc/mysql/conf.d -v /zhaoyoung/mysql/logs:/logs -v /zhaoyoung/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
839d49b39efc154d68b567827408eedbd3955345032fbb68ab072889878d96b8
-p 12345:3306:将主机的12345端映射到docker容器的3306端。
--name mysql:运行服务名字
-v /zhaoyoung/mysql/conf:/etc/mysq/conf.d:将主机/zhaoyoung/mysql目录下的conf/my.cnf挂载到容器的/etc/mysql/conf.d
-v /zhaoyoung/mysql/logs:/logs:将主机/zhaoyoung/mysq|目录下的logs目录挂载到容器的/logs。
-v /zhaoyoung/mysql/data:/var/lib/mysql:将主机/zhaoyoung/mysql目录下的data目录挂载到容器的/var/lib/mysql
- e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码。
-d mysql:5.6:后台程序运行mysql5.6
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
839d49b39efc mysql:5.6 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:12345->3306/tcp mysql
[root@localhost local]# docker exec -it 839d49b39efc /bin/bash
root@839d49b39efc:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.41 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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 |
+--------------------+
3 rows in set (0.04 sec)
mysql> create database db01;
Query OK, 1 row affected (0.00 sec)
mysql> use db01;
Database changed
mysql> create table tb_user(id bigint(20) not null primary key,user_name varchar(255));
Query OK, 0 rows affected (0.04 sec)
mysql> show tables;
+----------------+
| Tables_in_db01 |
+----------------+
| tb_user |
+----------------+
1 row in set (0.00 sec)
mysql> insert into tb_user(id,user_name) values (1,'zhaoyoung');
Query OK, 1 row affected (0.00 sec)
mysql> select * from tb_user ;
+----+-----------+
| id | user_name |
+----+-----------+
| 1 | zhaoyoung |
+----+-----------+
1 row in set (0.00 sec)
[root@localhost ~]# docker exec 839d49b39efc sh -c 'exec mysqldump --all-databases -uroot -p"123456" ' > /zhaoyoung/all-databases.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost test]# cd /zhaoyoung/
[root@localhost zhaoyoung]# ll
总用量 3244
-rw-r--r--. 1 root root 3318765 10月 15 17:10 all-databases.sql
drwxr-xr-x. 3 root root 21 10月 13 16:01 mydockerfile
drwxr-xr-x. 5 root root 42 10月 15 16:33 mysql
[root@localhost ~]# docker exec 839d49b39efc sh -c 'exec mysqldump -uroot -p"123456" db01' > /zhaoyoung/db01.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost zhaoyoung]# ll
总用量 3248
-rw-r--r--. 1 root root 3318765 10月 15 17:10 all-databases.sql
-rw-r--r--. 1 root root 1873 10月 15 17:24 db01.sql
drwxr-xr-x. 3 root root 21 10月 13 16:01 mydockerfile
drwxr-xr-x. 5 root root 42 10月 15 16:33 mysql
可以看到数据库已备份
[root@localhost zhaoyoung]# docker pull redis:3.2
3.2: Pulling from library/redis
802b00ed6f79: Already exists
8b4a21f633de: Pull complete
92e244f8ff14: Pull complete
ece9ebedc9a4: Pull complete
6c45ddcdd901: Pull complete
fa6b3d1893cb: Pull complete
Digest: sha256:5d1b6660fca7e77b133d7a8bdb130b3248a136c3ff37be229e96c150bf37bbb8
Status: Downloaded newer image for redis:3.2
[root@localhost /]# docker run -p 6379:6379 -v /zhaoyoung/redis/data:/data -v /zhaoyoung/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf -d redis:3.2 redis-server /usr/local/etc/redis/redis.conf --appendonly yes
0a30c2e016d1bc1e70a4bfd59ef2cfdac1843c7aeab943e28cc369949a59bba9
--appendonly yes 表示是否开启持久化AOF
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a30c2e016d1 redis:3.2 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:6379->6379/tcp condescending_boyd
839d49b39efc mysql:5.6 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:12345->3306/tcp mysql
[root@localhost /]# docker exec -it 9b732eb8eb98 redis-cli
127.0.0.1:6379> set ke1 v1
OK
127.0.0.1:6379> set k2 v2
OK
127.0.0.1:6379> set k3 v3
OK
127.0.0.1:6379> SHUTDOWN
[root@localhost redis.conf]# cd ../
[root@localhost conf]# cd ../
[root@localhost redis]# ll
总用量 0
drwxr-xr-x. 3 root root 24 10月 15 17:45 conf
drwxr-xr-x. 2 polkitd root 28 10月 15 17:45 data
[root@localhost redis]# cd data/
[root@localhost data]# ll
总用量 4
-rw-r--r--. 1 polkitd ssh_keys 111 10月 15 18:04 appendonly.aof
[root@localhost data]# cat appendonly.aof
*2
$6
SELECT
$1
0
*3
$3
set
$3
ke1
$2
v1
*3
$3
set
$2
k2
$2
v2
*3
$3
set
$2
k3
$2
v3