docker部署MySQL并同步数据 数据持久化

本文介绍了如何在Docker中搜索、拉取和使用MySQL镜像,包括查看现有镜像、下载特定版本、运行容器并挂载数据卷实现数据持久化,以及验证容器删除后数据是否丢失的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搜索mysql镜像

[root@wq ~]# docker search mysql
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   14891     [OK]
mariadb                         MariaDB Server is a high performing open sou…   5685      [OK]
percona                         Percona Server is a fork of the MySQL relati…   624       [OK]
phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   953       [OK]
bitnami/mysql                   Bitnami MySQL Docker Image                      107                  [OK]
bitnami/mysqld-exporter                                                         6
cimg/mysql                                                                      3
ubuntu/mysql                    MySQL open source fast, stable, multi-thread…   59
rapidfort/mysql                 RapidFort optimized, hardened image for MySQL   25
rapidfort/mysql8-ib             RapidFort optimized, hardened image for MySQ…   9
google/mysql                    MySQL server for Google Compute Engine          25                   [OK]
rapidfort/mysql-official        RapidFort optimized, hardened image for MySQ…   9
elestio/mysql                   Mysql, verified and packaged by Elestio         0
hashicorp/mysql-portworx-demo                                                   0
bitnamicharts/mysql                                                             0
databack/mysql-backup           Back up mysql databases to... anywhere!         109
linuxserver/mysql               A Mysql container, brought to you by LinuxSe…   41
mirantis/mysql                                                                  0
docksal/mysql                   MySQL service images for Docksal - https://d…   0
linuxserver/mysql-workbench                                                     55
vitess/mysqlctld                vitess/mysqlctld                                1                    [OK]
eclipse/mysql                   Mysql 5.7, curl, rsync                          1                    [OK]
drupalci/mysql-5.5              https://www.drupal.org/project/drupalci         3                    [OK]
drupalci/mysql-5.7              https://www.drupal.org/project/drupalci         0
datajoint/mysql                 MySQL image pre-configured to work smoothly …   2                    [OK]

查看当前已有镜像,并下载镜像查看

[root@wq ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED       SIZE
tomcat02              1.0       02ea20370fec   3 hours ago   684MB
nginx                 latest    605c77e624dd   2 years ago   141MB
tomcat                latest    fb5657adc892   2 years ago   680MB
wordpress             latest    c3c92cc3dcb1   2 years ago   616MB
redis                 latest    7614ae9453d1   2 years ago   113MB
mysql                 latest    3218b38490ce   2 years ago   516MB
centos                latest    5d0da3dc9764   2 years ago   231MB
portainer/portainer   latest    580c0e4e98b0   2 years ago   79.1MB
elasticsearch         7.6.2     f29a1ee41030   3 years ago   791MB
[root@wq ~]#
[root@wq ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@wq ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED       SIZE
tomcat02              1.0       02ea20370fec   3 hours ago   684MB
nginx                 latest    605c77e624dd   2 years ago   141MB
tomcat                latest    fb5657adc892   2 years ago   680MB
wordpress             latest    c3c92cc3dcb1   2 years ago   616MB
redis                 latest    7614ae9453d1   2 years ago   113MB
mysql                 5.7       c20987f18b13   2 years ago   448MB
mysql                 latest    3218b38490ce   2 years ago   516MB
centos                latest    5d0da3dc9764   2 years ago   231MB
portainer/portainer   latest    580c0e4e98b0   2 years ago   79.1MB
elasticsearch         7.6.2     f29a1ee41030   3 years ago   791MB
[root@wq ~]#

运行容器,需要进行数据挂载

[root@wq ~]# docker images |grep mysql
mysql                 5.7       c20987f18b13   2 years ago   448MB
mysql                 latest    3218b38490ce   2 years ago   516MB
[root@wq ~]# docker run -d -p 8030:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=000000 --name mysql01 mysql:5.7
3943bfb6fbd7e6106f391f23e4ab52b3ad3391586866c9491f4e63a48f882626
[root@wq ~]# docker ps | grep mysql
3943bfb6fbd7   mysql:5.7             "docker-entrypoint.s…"   7 seconds ago   Up 5 seconds       33060/tcp, 0.0.0.0:8030->3306/tcp, :::8030->3306/tcp   mysql01
94239899c5bd   mysql                 "docker-entrypoint.s…"   45 hours ago    Up 45 hours        33060/tcp, 0.0.0.0:8006->3306/tcp, :::8006->3306/tcp   my_mysql
[root@wq ~]#

-d 后台运行

-p 端口映射

-v 卷挂载

-e 环境配置

--name 容器名字

测试是否同步

进入容器,登录数据库,创建test库

[root@wq ~]# docker exec -it 3943bfb6fbd7 /bin/bash
root@3943bfb6fbd7:/# mysql -uroot -p000000
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql>

新建终端查看

[root@wq data]# pwd
/home/mysql/data
[root@wq data]# ll
total 188476
-rw-r----- 1 polkitd input       56 Mar  2 18:22 auto.cnf
-rw------- 1 polkitd input     1676 Mar  2 18:22 ca-key.pem
-rw-r--r-- 1 polkitd input     1112 Mar  2 18:22 ca.pem
-rw-r--r-- 1 polkitd input     1112 Mar  2 18:22 client-cert.pem
-rw------- 1 polkitd input     1680 Mar  2 18:22 client-key.pem
-rw-r----- 1 polkitd input      469 Mar  2 18:30 ib_buffer_pool
-rw-r----- 1 polkitd input 79691776 Mar  2 18:30 ibdata1
-rw-r----- 1 polkitd input 50331648 Mar  2 18:30 ib_logfile0
-rw-r----- 1 polkitd input 50331648 Mar  2 18:22 ib_logfile1
-rw-r----- 1 polkitd input 12582912 Mar  2 18:30 ibtmp1
drwxr-x--- 2 polkitd input     4096 Mar  2 18:22 mysql
drwxr-x--- 2 polkitd input     4096 Mar  2 18:22 performance_schema
-rw------- 1 polkitd input     1676 Mar  2 18:22 private_key.pem
-rw-r--r-- 1 polkitd input      452 Mar  2 18:22 public_key.pem
-rw-r--r-- 1 polkitd input     1112 Mar  2 18:22 server-cert.pem
-rw------- 1 polkitd input     1676 Mar  2 18:22 server-key.pem
drwxr-x--- 2 polkitd input    12288 Mar  2 18:22 sys
[root@wq data]# ll
total 188480
-rw-r----- 1 polkitd input       56 Mar  2 18:22 auto.cnf
-rw------- 1 polkitd input     1676 Mar  2 18:22 ca-key.pem
-rw-r--r-- 1 polkitd input     1112 Mar  2 18:22 ca.pem
-rw-r--r-- 1 polkitd input     1112 Mar  2 18:22 client-cert.pem
-rw------- 1 polkitd input     1680 Mar  2 18:22 client-key.pem
-rw-r----- 1 polkitd input      469 Mar  2 18:30 ib_buffer_pool
-rw-r----- 1 polkitd input 79691776 Mar  2 18:30 ibdata1
-rw-r----- 1 polkitd input 50331648 Mar  2 18:30 ib_logfile0
-rw-r----- 1 polkitd input 50331648 Mar  2 18:22 ib_logfile1
-rw-r----- 1 polkitd input 12582912 Mar  2 18:30 ibtmp1
drwxr-x--- 2 polkitd input     4096 Mar  2 18:22 mysql
drwxr-x--- 2 polkitd input     4096 Mar  2 18:22 performance_schema
-rw------- 1 polkitd input     1676 Mar  2 18:22 private_key.pem
-rw-r--r-- 1 polkitd input      452 Mar  2 18:22 public_key.pem
-rw-r--r-- 1 polkitd input     1112 Mar  2 18:22 server-cert.pem
-rw------- 1 polkitd input     1676 Mar  2 18:22 server-key.pem
drwxr-x--- 2 polkitd input    12288 Mar  2 18:22 sys
drwxr-x--- 2 polkitd input     4096 Mar  2 18:31 test
[root@wq data]#

第一次查看没有test,第二次出现了,因为第一次查看的时候库还没有创建

因此测试成功,数据同步

容器数据持久化功能验证

假设将容器删掉,验证数据是否会丢失

1.删掉容器

[root@wq ~]# docker ps |grep mysql01
3943bfb6fbd7   mysql:5.7             "docker-entrypoint.s…"   About an hour ago   Up About an hour   33060/tcp, 0.0.0.0:8030->3306/tcp, :::8030->3306/tcp   mysql01
[root@wq ~]# docker rm -f 3943bfb6fbd7
3943bfb6fbd7
[root@wq ~]# docker ps -a |grep mysql01
[root@wq ~]#

2.查看目录是否存在

[root@wq ~]# cd /home/
admin/ ceshi/ mysql/ www/
[root@wq ~]# cd /home/mysql/
conf/ data/
[root@wq ~]# cd /home/mysql/data/
[root@wq data]# ll
total 188480
-rw-r----- 1 polkitd input       56 Mar  2 18:22 auto.cnf
-rw------- 1 polkitd input     1676 Mar  2 18:22 ca-key.pem
-rw-r--r-- 1 polkitd input     1112 Mar  2 18:22 ca.pem
-rw-r--r-- 1 polkitd input     1112 Mar  2 18:22 client-cert.pem
-rw------- 1 polkitd input     1680 Mar  2 18:22 client-key.pem
-rw-r----- 1 polkitd input      469 Mar  2 18:30 ib_buffer_pool
-rw-r----- 1 polkitd input 79691776 Mar  2 18:30 ibdata1
-rw-r----- 1 polkitd input 50331648 Mar  2 18:30 ib_logfile0
-rw-r----- 1 polkitd input 50331648 Mar  2 18:22 ib_logfile1
-rw-r----- 1 polkitd input 12582912 Mar  2 18:30 ibtmp1
drwxr-x--- 2 polkitd input     4096 Mar  2 18:22 mysql
drwxr-x--- 2 polkitd input     4096 Mar  2 18:22 performance_schema
-rw------- 1 polkitd input     1676 Mar  2 18:22 private_key.pem
-rw-r--r-- 1 polkitd input      452 Mar  2 18:22 public_key.pem
-rw-r--r-- 1 polkitd input     1112 Mar  2 18:22 server-cert.pem
-rw------- 1 polkitd input     1676 Mar  2 18:22 server-key.pem
drwxr-x--- 2 polkitd input    12288 Mar  2 18:22 sys
drwxr-x--- 2 polkitd input     4096 Mar  2 18:31 test
[root@wq data]#

发现挂载到本地的数据卷依旧没有丢失,在/home/mysql/data下。这就实现了容器数据持久化功能

### 如何在 Docker Desktop 上安装配置 MySQL 容器 #### 准备工作 为了确保能够成功运行 MySQL 容器,需确认 Docker Desktop 已经正确安装,切换至 Linux 容器模式。如果当前使用的操作系统为 Windows,默认情况下可能处于 Windows 容器模式,因此需要通过右下角的鲸鱼图标右键菜单选择“Switch to Linux containers”[^2]。 #### 创建宿主机挂载目录 为了让数据持久化存储,建议在本地创建用于挂载的文件夹结构。以下是推荐的路径及其用途: - 数据目录:`D:/dev/dockerData/mysql/data` - 日志目录:`D:/dev/dockerData/mysql/log` - 配置目录:`D:/dev/dockerData/mysql/conf` 这些目录可以依据实际需求调整位置和名称,但必须提前手动创建好以便后续挂载使用。 #### 启动 MySQL 容器 启动容器时可以通过 `docker run` 命令指定版本号以及其他参数来完成初始化设置。下面是一个典型的命令示例: ```bash docker run --name mysql-container \ -p 3306:3306 \ -v D:/dev/dockerData/mysql/data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=your_password_here \ -d mysql:5.7 ``` 上述命令解释如下: - `--name mysql-container`: 给容器命名。 - `-p 3306:3306`: 将宿主机端口映射到容器内部的服务端口。 - `-v D:/dev/dockerData/mysql/data:/var/lib/mysql`: 实现数据卷绑定,使得数据库中的数据可以在重启后保留下来。 - `-e MYSQL_ROOT_PASSWORD=your_password_here`: 设置 root 用户密码。 - `-d mysql:5.7`: 使用官方镜像仓库拉取特定版本 (这里是 5.7),且以后台方式运行该服务[^1]。 #### 更新 WSL 版本(仅限 Win10) 对于部分用户而言,可能会遇到由于WSL子系统不兼容而导致的问题。此时可尝试升级您的 WSL 到最新稳定版或者启用 WSL2 支持功能。具体操作方法包括但不限于执行以下 PowerShell 脚本来实现自动更新: ```powershell wsl --update wsl --status ``` 这一步骤有助于解决某些因环境差异引发的异常状况,比如 Docker Desktop 即使经过多次重新安装仍无法正常启动的情况[^4]。 #### 测试连接 最后验证是否能顺利访问新部署好的实例。可以借助 Navicat 或者其他图形界面工具输入对应地址(`localhost`) 和之前定义过的认证凭证进行登录测试;也可以直接利用 CLI 方式进入交互界面进一步探索其基本特性。 ```bash docker exec -it mysql-container mysql -uroot -pyour_password_here ``` 以上即完成了整个流程概述,按照此指南应该可以帮助您顺利完成目标设定下的任务处理过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SKY慕雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值