1. 拉取MySQL镜像
# 1. 获取mysql镜像 docker pull mysql # 运行容器,需要做数据挂,安装启动mysql,需要配置密码!!! # 官方测试 # $ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag # 2. 启动 -d 后台运行 -p 端口映射 -v 挂载卷 -e 环境配置 --name 容器名称 docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql # 3. Navicat本地测试mysq数据库,通过3310映射容器内3306,如图
2.本地创建测试
2.1 在本地创建数据库
创建test库
2.2 主机查看
root@docker:/home# ls ceshi henggao mysql test.java root@docker:/home# cd mysql/ root@docker:/home/mysql# ls conf data root@docker:/home/mysql# cd data/ root@docker:/home/mysql/data# ls auto.cnf client-cert.pem ib_logfile0 performance_schema undo_001 binlog.000001 client-key.pem ib_logfile1 private_key.pem undo_002 binlog.000002 '#ib_16384_0.dblwr' ibtmp1 public_key.pem binlog.index '#ib_16384_1.dblwr' '#innodb_temp' server-cert.pem ca-key.pem ib_buffer_pool mysql server-key.pem ca.pem ibdata1 mysql.ibd sys root@docker:/home/mysql/data# ls auto.cnf client-cert.pem ib_logfile0 performance_schema test binlog.000001 client-key.pem ib_logfile1 private_key.pem undo_001 binlog.000002 '#ib_16384_0.dblwr' ibtmp1 public_key.pem undo_002 binlog.index '#ib_16384_1.dblwr' '#innodb_temp' server-cert.pem ca-key.pem ib_buffer_pool mysql server-key.pem ca.pem ibdata1 mysql.ibd sys root@docker:/home/mysql/data#
3. 删除容器测试
3.1 假设删除容器
root@docker:/home/henggao/Desktop# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dca126bc38d7 mysql "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 33060/tcp, 0.0.0.0:3310->3306/tcp, :::3310->3306/tcp mysql01 root@docker:/home/henggao/Desktop# docker rm -f mysql01 mysql01 root@docker:/home/henggao/Desktop# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3.2 查看主机数据
数据test库依然存在
root@docker:/home/mysql/data# ls auto.cnf client-cert.pem ib_logfile0 performance_schema test binlog.000001 client-key.pem ib_logfile1 private_key.pem undo_001 binlog.000002 '#ib_16384_0.dblwr' ibtmp1 public_key.pem undo_002 binlog.index '#ib_16384_1.dblwr' '#innodb_temp' server-cert.pem ca-key.pem ib_buffer_pool mysql server-key.pem ca.pem ibdata1 mysql.ibd sys