泊坞窗 - 如何挂载/ var / lib中/ MySQL的到码头工人mysql的容器

时间:2019-02-01 20:25:53

标签: mysql docker

可以将现有已安装的mysql / var / lib / mysql数据挂载到docker mysql容器中。

因为我不想迁移所有的mysql和mongodb,因为我有50+ Gb数据,并且每天都在增加。

我已将卷设置为与mysql数据文件夹一起挂载,但不幸的是它不适用于我。

我已经尝试过,但是没有用,是我丢失了东西还是做错了什么?

version: "3"
services:
  app:
    build:
      context: ./app
      dockerfile: ./dockerfile      
    container_name: flaskPython
    links:
      - mysqldb
      - mongodb
    ports:
      - "5000:5000"
  myadmin:
        image: phpmyadmin/phpmyadmin
        container_name: phpmyadmin
        ports:
            - "8282:80"
        environment:
            - PMA_ARBITRARY=1
            - PMA_HOST=${MYSQL_HOST}
        restart: always
        depends_on:
            - mysqldb
            - mongodb  
  mysqldb:
        image: mysql:${MYSQL_VERSION}
        container_name: ${MYSQL_HOST}
        restart: 'always'
        expose:
            - '3306'
        env_file:
            - ".env"
        environment:
            - MYSQL_DATABASE=${MYSQL_DATABASE}
            - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
            - MYSQL_USER=${MYSQL_USER}
            - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        ports:
            - "8989:3306"
        volumes:
            #- ./data/db/mysql:/var/lib/mysql
            - /var/lib/mysql:/var/lib/mysql
  mongodb:
        image: mongo
        container_name: mongodb
        restart: always
        environment:

          - MONGO_INITDB_ROOT_USERNAME=root
          - MONGO_INITDB_ROOT_PASSWORD=root
        # if you wish to setup additional user accounts specific per DB or with different roles you can use following entry point
        #no --auth is needed here as presence of username and password add this option automatically
        command: mongod
        ports:
            - "27017:27017"
        volumes:
          - ./mongo-entrypoint/:/docker-entrypoint-initdb.d/
          - ./data/db/mongo:/usr/data/db

日志:

2019-02-01T16:08:20.100825Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-02-01T16:08:20.101919Z 0 [Note] mysqld (mysqld 5.7.22) starting as process 1 ...
2019-02-01T16:08:20.104031Z 0 [Note] InnoDB: PUNCH HOLE support available
2019-02-01T16:08:20.104043Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-02-01T16:08:20.104046Z 0 [Note] InnoDB: Uses event mutexes
2019-02-01T16:08:20.104048Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2019-02-01T16:08:20.104050Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2019-02-01T16:08:20.104052Z 0 [Note] InnoDB: Using Linux native AIO
2019-02-01T16:08:20.104214Z 0 [Note] InnoDB: Number of pools: 1
2019-02-01T16:08:20.104285Z 0 [Note] InnoDB: Using CPU crc32 instructions
2019-02-01T16:08:20.105476Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2019-02-01T16:08:20.110790Z 0 [Note] InnoDB: Completed initialization of buffer pool
2019-02-01T16:08:20.112288Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2019-02-01T16:08:20.123892Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2019-02-01T16:08:20.139921Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2019-02-01T16:08:20.139996Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2019-02-01T16:08:20.170786Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2019-02-01T16:08:20.173104Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2019-02-01T16:08:20.173143Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2019-02-01T16:08:20.174483Z 0 [Note] InnoDB: Waiting for purge to start
2019-02-01T16:08:20.224878Z 0 [Note] InnoDB: 5.7.22 started; log sequence number 1529088986
2019-02-01T16:08:20.225403Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2019-02-01T16:08:20.225887Z 0 [Note] Plugin 'FEDERATED' is disabled.
2019-02-01T16:08:20.231185Z 0 [Note] InnoDB: Buffer pool(s) load completed at 190201 16:08:20
2019-02-01T16:08:20.233231Z 0 [Warning] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key
2019-02-01T16:08:20.233502Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2019-02-01T16:08:20.233535Z 0 [Note] IPv6 is available.
2019-02-01T16:08:20.233544Z 0 [Note]   - '::' resolves to '::';
2019-02-01T16:08:20.233556Z 0 [Note] Server socket created on IP: '::'.
2019-02-01T16:08:20.236451Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2019-02-01T16:08:20.248740Z 0 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2019-02-01T16:08:20.248832Z 0 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
2019-02-01T16:08:20.248857Z 0 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2019-02-01T16:08:20.248875Z 0 [Warning] 'user' entry 'debian-sys-maint@localhost' ignored in --skip-name-resolve mode.
2019-02-01T16:08:20.248921Z 0 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
2019-02-01T16:08:20.248935Z 0 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2019-02-01T16:08:20.249216Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2019-02-01T16:08:20.256300Z 0 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
2019-02-01T16:08:20.256349Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
2019-02-01T16:08:20.301149Z 0 [Note] Event Scheduler: Loaded 0 events
2019-02-01T16:08:20.301811Z 0 [Note] mysqld: ready for connections.
Version: '5.7.22'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)

由于我进行了一些更改,因此获得了不同的错误。 有时容器崩溃,有时无法连接到mysql(111)

1 个答案:

答案 0 :(得分:2)

问题

问题可能是Mysql和MongoDB的版本和/或/var/lib/mysql/data/db/mongo的用户UID和GID。

版本

首先,容器上运行的Mysql版本和MongoDB版本必须与主机上运行的版本相同,以便您能够使用主机中使用的相同文件夹。

请记住,共享相同文件时,主机和容器中无法同时运行Mysql服务器和MongoDB服务器。

用相同的文件表示主机上的/var/lib/mysql/data/db/mongo

如果要使服务器在本地主机上运行,​​则需要将此文件夹复制到其他位置,并为用户提供相同的UID和GID。

用户的UID和GID

docker容器和主机中的Mysql用户和Mongo DB用户必须具有相同的UID和GID。

通过以下方式同时检查它们:

$ sudo docker run --rm -it mysql bash
root@4c07b20e88c4:/# grep -irn mysql /etc/passwd
20:mysql:x:999:999::/home/mysql:
root@4c07b20e88c4:/# grep -irn mysql /etc/group
40:mysql:x:999:

或:

root@4c07b20e88c4:/# id -u mysql
999
root@4c07b20e88c4:/# id -g mysql
999

您可以看到,用户和组的UID为999,GID也为999。现在,只需在主机中重复相同的操作即可。

可能的解决方案

检查主机和容器中Mysql和MongoDB的版本,如果它们不匹配,请更新容器以运行相同版本的主机。

还要在主机中将Mysql和MongoDB的用户设置为与容器内使用的UID和GID相同,或者反过来...选择最适合您的用例的。

更改用户的UID和GID

在外壳中运行:

root@4c07b20e88c4:/# usermod -u 800 mysql
root@4c07b20e88c4:/# id -u mysql
800
root@4c07b20e88c4:/# groupmod -g 800 mysql
root@4c07b20e88c4:/# id -g mysql
800

800替换为所需的ID号,并将mysql替换为您要更改ID的用户名。

下一步,更新该用户的文件权限:

find / -user <OLDUID> -exec chown -h <NEWUID> {} \;

并针对该组:

find / -group <OLDGID> -exec chgrp -h <NEWGID> {} \;

更多信息here