• docker中的Mysql数据卷与持久化


    本文通过两个mysql容器(一个挂载一个无挂载)测试docker的持久化

    结论

    容器状态/持久化 无挂载 有挂载
    pause Y Y
    stop->start Y Y
    rm -> up N Y

    环境

    mac docker-compose

     ~/Workspace/docker/volume-learn � docker-compose -v
     
    docker-compose version 1.23.2, build 1110ad01
    
     ~/Workspace/docker/volume-learn � docker version
    Client: Docker Engine - Community
     Version:           18.09.2
     API version:       1.39
     Go version:        go1.10.8
     Git commit:        6247962
     Built:             Sun Feb 10 04:12:39 2019
     OS/Arch:           darwin/amd64
     Experimental:      false
    
    Server: Docker Engine - Community
     Engine:
      Version:          18.09.2
      API version:      1.39 (minimum version 1.12)
      Go version:       go1.10.6
      Git commit:       6247962
      Built:            Sun Feb 10 04:13:06 2019
      OS/Arch:          linux/amd64
      Experimental:     false
    

    步骤

    docker-compose.yml

    version: '3'
    
    services:
    
        mysql_1:
            image: mysql:5.7
            container_name: test_mysql_1
            environment:
                - MYSQL_ROOT_PASSWORD=123456
                - MYSQL_DATABASE=base
                - MYSQL_USER=dbuser
                - MYSQL_PASSWORD=topsecret
                - MYSQL_PORT=3306
            ports:
                - "3306:3306"
        mysql_2:
            image: mysql:5.7
            container_name: test_mysql_2
            environment:
                - MYSQL_ROOT_PASSWORD=123456
                - MYSQL_DATABASE=base
                - MYSQL_USER=dbuser
                - MYSQL_PASSWORD=topsecret
                - MYSQL_PORT=3306
            ports:
                - "3307:3306"
            volumes:
                - ./data/mysql_db:/var/lib/mysql
    

    启动

    docker-compose up -d
    Creating test_mysql_1 ... done
    Creating test_mysql_2 ... done
    

    查看挂载情况

    docker volume ls
    DRIVER              VOLUME NAME
    local               8c45070e36f1884b5d8a38fe73667fd0ed2bd6bfb8ad55ca6ff66f1fcebded6d
    
     ~/Workspace/docker/volume-learn > � docker inspect test_mysql_1 | grep Mounts -A 10
            "Mounts": [
                {
                    "Type": "volume",
                    "Name": "8c45070e36f1884b5d8a38fe73667fd0ed2bd6bfb8ad55ca6ff66f1fcebded6d",
                    "Source": "/var/lib/docker/volumes/8c45070e36f1884b5d8a38fe73667fd0ed2bd6bfb8ad55ca6ff66f1fcebded6d/_data",
                    "Destination": "/var/lib/mysql",
                    "Driver": "local",
                    "Mode": "",
                    "RW": true,
                    "Propagation": ""
                }
     ~/Workspace/docker/volume-learn >� docker inspect test_mysql_2 | grep Mounts -A 10
            "Mounts": [
                {
                    "Type": "bind",
                    "Source": "/Users/lifan/Workspace/docker/volume-learn/data/mysql_db",
                    "Destination": "/var/lib/mysql",
                    "Mode": "rw",
                    "RW": true,
                    "Propagation": "rprivate"
                }
            ],
            "Config": {
    
     ~/Workspace/docker/volume-learn � ll
    total 8
    drwxr-xr-x  3 lifan  staff   102B  8  5 16:46 data
    -rw-r--r--  1 lifan  staff   713B  8  5 16:50 docker-compose.yml
    

    MacOS 进入默认挂载目录

    mac OS

    cd ~/Library/Containers/com.docker.docker/Data/vms/0/
    screen tty
    # 页面悬停
    Ctrl + C
    
    linuxkit-025000000001:~# cd /var/lib/docker/volumes/
    linuxkit-025000000001:/var/lib/docker/volumes# ls
    8c45070e36f1884b5d8a38fe73667fd0ed2bd6bfb8ad55ca6ff66f1fcebded6d
    metadata.db
    linuxkit-025000000001:/var/lib/docker/volumes#
    
    # 同时按住Ctrl和a,然后放开再按下d
    Ctrl + A  D # 退出
    screen -r # 重新登入
    
    # 同时按住Ctrl和a,然后放开再按下k
    Ctrl + A + K # 彻底退出
    
    

    写入数据

    test_mysql_1

    CREATE TABLE `user_1` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    

    test_mysql_2

    CREATE TABLE `user_2` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(11) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    

    测试

    停止重启

     ~/Workspace/docker/volume-learn � docker-compose stop
    Stopping test_mysql_2 ... done
    Stopping test_mysql_1 ... done
     ~/Workspace/docker/volume-learn � docker-compose start
    Starting mysql_1 ... done
    Starting mysql_2 ... done
    

    两个数据都还在

    暂停重启

     ~/Workspace/docker/volume-learn � docker-compose pause
    Pausing test_mysql_1 ... done
    Pausing test_mysql_2 ... done
    
     ~/Workspace/docker/volume-learn � docker inspect test_mysql_1
    [
        {
            "Id": "55193429b7f8b548bcc74ac02239468ef80b439a81597a68503e6ed37097fa78",
            "Created": "2019-08-05T09:00:36.5867032Z",
            "Path": "docker-entrypoint.sh",
            "Args": [
                "mysqld"
            ],
            "State": {
                "Status": "paused",
                "Running": true,
                "Paused": true,
                "Restarting": false,
                "OOMKilled": false,
                "Dead": false,
                "Pid": 37015,
                "ExitCode": 0,
                "Error": "",
                "StartedAt": "2019-08-05T09:49:14.9745542Z",
                "FinishedAt": "2019-08-05T09:44:37.1531094Z"
            },
    
    
     ~/Workspace/docker/volume-learn � docker-compose unpause
    Unpausing test_mysql_2 ... done
    Unpausing test_mysql_1 ... done
    

    数据没有丢失

    删除容器重启

     ~/Workspace/docker/volume-learn � docker-compose stop
    Stopping test_mysql_2 ... done
    Stopping test_mysql_1 ... done
     ~/Workspace/docker/volume-learn � docker container ls -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
    317f7f035b4e        mysql:5.7           "docker-entrypoint.s…"   About an hour ago   Exited (0) 27 seconds ago                       test_mysql_2
    55193429b7f8        mysql:5.7           "docker-entrypoint.s…"   About an hour ago   Exited (0) 27 seconds ago                       test_mysql_1
    1e330d4da45d        46f8a1ba43b9        "./web"                  About an hour ago   Up About an hour                                k8s_leeroy-web_leeroy-web-66575d8bc7-t4n88_default_f3fe20ff-8b43-11e9-8f64-025000000001_1
    1aabdb1a4df2        4e70f93f0150        "./app"                  About an hour ago   Up About an hour                                k8s_leeroy-app_leeroy-app-5b597594f4-7kwwr_default_f3dc703c-8b43-11e9-8f64-025000000001_1
     ~/Workspace/docker/volume-learn �
    
     ~/Workspace/docker/volume-learn � docker container prune -f
    Deleted Containers:
    317f7f035b4e6bd80f987119acb9280735572dfa4fb74492aa365acf89b66e7b
    55193429b7f8b548bcc74ac02239468ef80b439a81597a68503e6ed37097fa78
    df6251c751c704805fa682a2950b50102589790099f4abd29a3d0c8d37f38ec4
    3f513d14c78300cbc88fdc794326caaca5caea549110ceeb8288b5223f314dd4
    a9f27228b8e6754594152f9566e7864ec0780c57ef7bf215a2bcf0423bee5d24
    a8992b319d0af91a8d69f8c37e7f76068e84cbdb3a1816dffeaa9f1ce1b09dbf
    
    Total reclaimed space: 0B
    
    docker-compose up 
    

    test_mysql_2 数据没有丢失
    test_mysql_1 数据丢失

    
     ~/Workspace/docker/volume-learn � docker inspect test_mysql_1 | grep Mounts -A 10
            "Mounts": [
                {
                    "Type": "volume",
                    "Name": "695c64bb2b3e0d36e4c14170aca4ba2e81dba29915458e8384456eb7751351d0",
                    "Source": "/var/lib/docker/volumes/695c64bb2b3e0d36e4c14170aca4ba2e81dba29915458e8384456eb7751351d0/_data",
                    "Destination": "/var/lib/mysql",
                    "Driver": "local",
                    "Mode": "",
                    "RW": true,
                    "Propagation": ""
                }
     ~/Workspace/docker/volume-learn � docker volume ls
    DRIVER              VOLUME NAME
    local               8c45070e36f1884b5d8a38fe73667fd0ed2bd6bfb8ad55ca6ff66f1fcebded6d
    local               695c64bb2b3e0d36e4c14170aca4ba2e81dba29915458e8384456eb7751351d0
    

    删除test_mysql_2的挂载目录再重启

    直接删除挂载目录

    容器没有停止

     ~/Workspace/docker/volume-learn � ll
    total 8
    drwxr-xr-x  3 lifan  staff   102B  8  5 16:46 data
    -rw-r--r--  1 lifan  staff   713B  8  5 16:50 docker-compose.yml
     ~/Workspace/docker/volume-learn � rm -rf data
     ~/Workspace/docker/volume-learn �
     
    

    访问数据库,可以连接,但是没有数据

    创建数据库失败

    create database base;
    
    Can't create database 'base' (errno: 1808635584)
    

    重启容器

    data目录被创建,再次挂载到主机目录

     ~/Workspace/docker/volume-learn � docker-compose stop
    Stopping test_mysql_2 ... done
    Stopping test_mysql_1 ... done
     ~/Workspace/docker/volume-learn � docker-compose start
    Starting mysql_1 ... done
    Starting mysql_2 ... done
     ~/Workspace/docker/volume-learn � ll
    total 8
    drwxr-xr-x  3 lifan  staff   102B  8  5 18:25 data
    -rw-r--r--  1 lifan  staff   713B  8  5 16:50 docker-compose.yml
     ~/Workspace/docker/volume-learn �
    

    创建表,写入数据

    CREATE TABLE `user_2` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(11) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    

    停止删除容器再启动

     ~/Workspace/docker/volume-learn � docker-compose stop
    Stopping test_mysql_2 ... done
    Stopping test_mysql_1 ... done
     ~/Workspace/docker/volume-learn � docker rm 77466edd2bd2
    77466edd2bd2
     ~/Workspace/docker/volume-learn � docker-compose up -d
    Starting test_mysql_1 ... done
    Creating test_mysql_2 ... done
     ~/Workspace/docker/volume-learn �
    

    数据还在,容器工作正常

  • 相关阅读:
    构造方法
    $this
    public/private/protected三种访问控制符的区别
    vsftpd的虚拟主机配置,错误记录
    高斯模糊css
    css绘制三角形
    禁止缩放的一行代码
    自适应的Javascript代码resize.js
    判断是pc端还是手机端js
    倒计时
  • 原文地址:https://www.cnblogs.com/lifan1998/p/14326741.html
Copyright © 2020-2023  润新知