V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
awker
V2EX  ›  Docker

运行在容器里的 MySQL 删除数据目录依然可以正常使用?

  •  
  •   awker · 2021-04-20 14:41:53 +08:00 · 1157 次点击
    这是一个创建于 1312 天前的主题,其中的信息可能已经有所发展或是发生改变。

    运行在容器里的 MySQL 删除数据目录依然可以正常使用,这是什么原因? 同样的步骤在虚拟机操作,MySQL 就无法正常使用。

    操作步骤如下:

    // 创建 mysql 挂载目录
    root@aliyun:~#  mkdir /data/mysql
    
    // 启动一个 mysql
    root@aliyun:~#  docker run -p 3306:3306 -v /data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
    
    root@aliyun:~#  docker ps
    CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
    5a27786e1657   mysql     "docker-entrypoint.s…"   32 seconds ago   Up 31 seconds   0.0.0.0:3306->3306/tcp, 33060/tcp   relaxed_jang
    
    // 进入 mysql 容器
    root@aliyun:~# docker exec -it 5a27786e1657 /bin/bash
    
    root@5a27786e1657:/# ls /var/lib/mysql/
    '#ib_16384_0.dblwr'   auto.cnf	      binlog.index   client-cert.pem   ib_logfile0   ibtmp1	 performance_schema   server-cert.pem   undo_001
    '#ib_16384_1.dblwr'   binlog.000001   ca-key.pem     client-key.pem    ib_logfile1   mysql	 private_key.pem      server-key.pem    undo_002
    '#innodb_temp'	      binlog.000002   ca.pem	     ib_buffer_pool    ibdata1	     mysql.ibd	 public_key.pem       sys
    // 删除容器里的 mysql 数据目录
    root@5a27786e1657:/# rm -rf /var/lib/mysql/
    rm: cannot remove '/var/lib/mysql/': Device or resource busy
    root@5a27786e1657:/# ls /var/lib/mysql/
    root@5a27786e1657:/# exit
    
    root@aliyun:~#  ifconfig eth0
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 172.17.17.108  netmask 255.255.192.0  broadcast 172.17.63.255
            ether 00:16:3e:00:46:06  txqueuelen 1000  (Ethernet)
            RX packets 3678  bytes 638716 (638.7 KB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 3294  bytes 1044025 (1.0 MB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    root@aliyun:~# netstat -nltup|grep 3306
    tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1329/docker-proxy
    // 删除 mysql 数据目录后,可以正常创建一个测试库 foobar
    root@aliyun:~# mysql -uroot -p123456 -h172.17.17.108 -P 3306 -e "create database foobar;show databases;"
    mysql: [Warning] Using a password on the command line interface can be insecure.
    +--------------------+
    | Database           |
    +--------------------+
    | foobar             |
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | test               |
    +--------------------+
    
    root@aliyun:~# ls /data/mysql/
    foobar
    root@aliyun:~# ls /data/mysql/foobar/
    root@aliyun:~# 
    
    
    awker
        1
    awker  
    OP
       2021-04-20 14:47:31 +08:00
    可以看到其实文件描述符还是存在的
    # lsof -p 1405|grep deleted
    mysqld 1405 999 9uW REG 252,1 50331648 1442005 /var/lib/mysql/ib_logfile1 (deleted)
    mysqld 1405 999 10uW REG 252,1 196608 1442006 /var/lib/mysql/#ib_16384_0.dblwr (deleted)
    mysqld 1405 999 11uW REG 252,1 8585216 1442007 /var/lib/mysql/#ib_16384_1.dblwr (deleted)
    mysqld 1405 999 12uW REG 252,1 16777216 1442009 /var/lib/mysql/undo_001 (deleted)
    mysqld 1405 999 13u REG 0,47 0 1442013 /tmp/ibI2pqU8 (deleted)
    mysqld 1405 999 14uW REG 252,1 16777216 1442011 /var/lib/mysql/undo_002 (deleted)
    mysqld 1405 999 15uW REG 252,1 12582912 1442015 /var/lib/mysql/ibtmp1 (deleted)
    mysqld 1405 999 16uW REG 252,1 31457280 1442008 /var/lib/mysql/mysql.ibd (deleted)
    mysqld 1405 999 24w REG 252,1 532 1442321 /var/lib/mysql/binlog.000002 (deleted)
    mysqld 1405 999 26uW REG 252,1 81920 1442016 /var/lib/mysql/#innodb_temp/temp_1.ibt (deleted)
    mysqld 1405 999 27uW REG 252,1 81920 1442017 /var/lib/mysql/#innodb_temp/temp_2.ibt (deleted)
    mysqld 1405 999 28uW REG 252,1 81920 1442018 /var/lib/mysql/#innodb_temp/temp_3.ibt (deleted)
    mysqld 1405 999 29uW REG 252,1 81920 1442019 /var/lib/mysql/#innodb_temp/temp_4.ibt (deleted)
    mysqld 1405 999 30uW REG 252,1 81920 1442020 /var/lib/mysql/#innodb_temp/temp_5.ibt (deleted)
    mysqld 1405 999 31uW REG 252,1 81920 1442021 /var/lib/mysql/#innodb_temp/temp_6.ibt (deleted)
    mysqld 1405 999 32uW REG 252,1 81920 1442022 /var/lib/mysql/#innodb_temp/temp_7.ibt (deleted)
    mysqld 1405 999 33uW REG 252,1 81920 1442023 /var/lib/mysql/#innodb_temp/temp_8.ibt (deleted)
    mysqld 1405 999 34uW REG 252,1 81920 1442024 /var/lib/mysql/#innodb_temp/temp_9.ibt (deleted)
    mysqld 1405 999 35uW REG 252,1 81920 1442315 /var/lib/mysql/#innodb_temp/temp_10.ibt (deleted)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5340 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 07:52 · PVG 15:52 · LAX 23:52 · JFK 02:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.