美文网首页Docker
【现学现忘Docker基础】— 25.实战:MySQL同步数据(

【现学现忘Docker基础】— 25.实战:MySQL同步数据(

作者: 繁华似锦Fighting | 来源:发表于2021-04-14 00:07 被阅读0次

    (4)测试MySQL服务持久化

    我们使用第三方软件操作容器中的MySQL服务,创建一个新的数据库testDB,看看数据库能否同步到宿主机中。

    在Navicat中创建testDB数据库,如下图:


    然后在宿主机的/tmp/mysql/data目录中是否能够查看到。

    [root@192 ~]# ll /tmp/mysql/data/
    总用量 188484
    -rw-r-----. 1 polkitd input       56 3月  19 12:27 auto.cnf
    -rw-------. 1 polkitd input     1676 3月  19 12:27 ca-key.pem
    -rw-r--r--. 1 polkitd input     1112 3月  19 12:27 ca.pem
    -rw-r--r--. 1 polkitd input     1112 3月  19 12:27 client-cert.pem
    -rw-------. 1 polkitd input     1680 3月  19 12:27 client-key.pem
    -rw-r-----. 1 polkitd input     1359 3月  19 12:28 ib_buffer_pool
    -rw-r-----. 1 polkitd input 79691776 3月  19 12:28 ibdata1
    -rw-r-----. 1 polkitd input 50331648 3月  19 12:28 ib_logfile0
    -rw-r-----. 1 polkitd input 50331648 3月  19 12:27 ib_logfile1
    -rw-r-----. 1 polkitd input 12582912 3月  19 12:28 ibtmp1
    drwxr-x---. 2 polkitd input       20 3月  19 12:32 myDB
    drwxr-x---. 2 polkitd input     4096 3月  19 12:28 mysql
    drwxr-x---. 2 polkitd input     8192 3月  19 12:28 performance_schema
    -rw-------. 1 polkitd input     1676 3月  19 12:28 private_key.pem
    -rw-r--r--. 1 polkitd input      452 3月  19 12:28 public_key.pem
    -rw-r--r--. 1 polkitd input     1112 3月  19 12:27 server-cert.pem
    -rw-------. 1 polkitd input     1676 3月  19 12:27 server-key.pem
    drwxr-x---. 2 polkitd input     8192 3月  19 12:28 sys
    drwxr-x---. 2 polkitd input       20 3月  19 12:47 testDB  # 看这里
    

    在宿主机中看到了testDB数据库,说明MySQL容器持久化的配置是成功的。

    (5)问题说明

    在进行如上操作的时候,我发现/tmp/mysql/conf/目录是空的,如下:

    [root@192 ~]# ll /tmp/mysql/conf/
    总用量 0
    

    而且会把MySQL容器中的/etc/mysql/conf.d目录也清空,原本mysql 5.7 的/etc/mysql/conf.d目录内容如下:

    root@6a0bc07a843b:/# ls /etc/mysql/conf.d/
    docker.cnf  mysql.cnf  mysqldump.cnf
    

    是有三个文件的。

    说明:

    • MySQL 5.7的默认配置文件是 /etc/mysql/my.cnf 文件。
    • 如果想要自定义配置,建议向 /etc/mysql/conf.d 目录中创建 .cnf 文件。
    • 新建的文件可以任意起名,只要保证后缀名是.cnf即可,新建的文件中的配置项可以覆盖 /etc/mysql/my.cnf 中的配置项。
    • 提示:不同版本的MySQL 镜像,配置文件的位置可能会有不同。

    所以/etc/mysql/conf.d/目录是空的,我们不需要担心。

    如果我们要实现在宿主机进行对容器中MySQL服务的配置,解决的方式有两种

    1. 手动在宿主机的/tmp/mysql/conf/目录(自己配置的目录)创建以.cnf后缀的配置文件,然后手动按规则编辑。
    2. 在启动一个MySQL容器,把上面三个文件拷贝到宿主机,放入到/tmp/mysql/conf/目录中,然后在手动进行配置。

    (6)MySQL数据库的数据备份

    命令如下:

    docker exec mysql 容器ID \
    sh -c 'exec mysqldump --all-databases -uroot -p"123456"' > /tmp/mysql/all-databases.sql
    

    示例:

    先查看MySQL容器是否正在运行,要运行中才能执行数据备份命令。

    执行命令

    docker exec mysql 8f6a77ba4917 \ sh -c 'exec mysqldump --all-databases -uroot -p"123456"' > /tmp/mysql/all-databases.sql

    说明:mysqldump -u root -p 数据库名 > 导出的数据库文件名,上面是导出所有数据库到后边的文件中。

    这样就可以手动的实现MySQL容器数据库备份到宿主机了。

    6、停止容器

    # 退出容器
    root@8f6a77ba4917:/# exit
    exit
    
    # 查看当前正在运行的容器
    [root@192 ~]# docker ps
    CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS              
    8f6a77ba4917   mysql:5.7   "docker-entrypoint.s…"   55 minutes ago   Up 55 minutes   
    
    # 停止掉mysql:5.7容器
    [root@192 ~]# docker stop 8f6a77ba4917
    8f6a77ba4917
    

    7、移除容器

    # 查看本地容器
    [root@192 ~]# docker ps -a
    CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS               
    8f6a77ba4917   mysql:5.7   "docker-entrypoint.s…"   58 minutes ago   Exited (0) 2 minutes 
    
    # 删除mysql:5.7容器
    [root@192 ~]# docker rm 8f6a77ba4917
    8f6a77ba4917
    
    # 查看容器是否删除
    [root@192 ~]# docker ps -a
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    

    此时,我们创建的mysql:5.7容器已经被停止删除,最后我们在到宿主机的/tmp/mysql/data目录中,查看mysql:5.7容器持久化数据是否还在。

    [root@192 ~]# ll /tmp/mysql/data/
    总用量 176196
    -rw-r-----. 1 polkitd input       56 3月  19 12:27 auto.cnf
    -rw-------. 1 polkitd input     1676 3月  19 12:27 ca-key.pem
    -rw-r--r--. 1 polkitd input     1112 3月  19 12:27 ca.pem
    -rw-r--r--. 1 polkitd input     1112 3月  19 12:27 client-cert.pem
    -rw-------. 1 polkitd input     1680 3月  19 12:27 client-key.pem
    -rw-r-----. 1 polkitd input      694 3月  19 13:24 ib_buffer_pool
    -rw-r-----. 1 polkitd input 79691776 3月  19 13:24 ibdata1
    -rw-r-----. 1 polkitd input 50331648 3月  19 13:24 ib_logfile0
    -rw-r-----. 1 polkitd input 50331648 3月  19 12:27 ib_logfile1
    drwxr-x---. 2 polkitd input       20 3月  19 12:32 myDB
    drwxr-x---. 2 polkitd input     4096 3月  19 12:28 mysql
    drwxr-x---. 2 polkitd input     8192 3月  19 12:28 performance_schema
    -rw-------. 1 polkitd input     1676 3月  19 12:28 private_key.pem
    -rw-r--r--. 1 polkitd input      452 3月  19 12:28 public_key.pem
    -rw-r--r--. 1 polkitd input     1112 3月  19 12:27 server-cert.pem
    -rw-------. 1 polkitd input     1676 3月  19 12:27 server-key.pem
    drwxr-x---. 2 polkitd input     8192 3月  19 12:28 sys
    drwxr-x---. 2 polkitd input       20 3月  19 12:47 testDB
    

    说明:删除容器,持久化还在。我们挂载到本地的数据卷依旧没有丢失,这就实现了容器数据持久化功能!

    相关文章

      网友评论

        本文标题:【现学现忘Docker基础】— 25.实战:MySQL同步数据(

        本文链接:https://www.haomeiwen.com/subject/muvvcltx.html