简体   繁体   English

Docker - 如何将 /var/lib/mysql 挂载到 docker mysql 容器

[英]Docker - how to mount /var/lib/mysql to docker mysql container

Is is possible to mount existing installed mysql /var/lib/mysql data to docker mysql container.是否可以将现有已安装的 mysql /var/lib/mysql 数据挂载到 docker mysql 容器。

Because i dont want to migrate all my mysql and mongodb because i have 50+ Gb data and which is increasing daily.因为我不想迁移我所有的 mysql 和 mongodb,因为我有 50+ Gb 数据并且每天都在增加。

I have set the volume to mount with the mysql data folder but unfortunately its not working for me.我已将卷设置为使用 mysql 数据文件夹安装,但不幸的是它对我不起作用。

I have tried but its not working am i missing something or doing something wrong?我试过了,但它不起作用是我错过了什么还是做错了什么?

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

Logs:日志:

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)

Getting different errors because i made some changes.得到不同的错误,因为我做了一些改变。 sometime container crash, some time cant connect to mysql (111)有时容器崩溃,有时无法连接到 mysql (111)

The problem问题

The problem may be the versions of Mysql and MongoDB and/or the user UID and GID for the /var/lib/mysql and /data/db/mongo .问题可能是 Mysql 和 MongoDB 的版本和/或/var/lib/mysql/data/db/mongo的用户 UID 和 GID。

Versions版本

First of all the Mysql versions and MongoDB versions running on the container need to be the same as the ones running on the host in order for you to be able to use the same folders used in the host.首先,容器上运行的 Mysql 版本和 MongoDB 版本需要与主机上运行的相同,以便您能够使用主机中使用的相同文件夹。

Bear in mind that you cannot have the Mysql server and MongoDB server running at same time in the host and containers while sharing the same files.请记住,您不能在共享相同文件的同时在主机和容器中同时运行 Mysql 服务器和 MongoDB 服务器。

By same files I mean /var/lib/mysql and /data/db/mongo on your host.相同的文件是指主机上的/var/lib/mysql/data/db/mongo

If you want to keep the servers running in localhost then you need to copy this folders elsewhere and have the same UID and GID for the user.如果您想让服务器在 localhost 中运行,那么您需要将此文件夹复制到其他地方,并为用户提供相同的 UID 和 GID。

User UID and GID用户 UID 和 GID

The Mysql user and Mongo DB user inside the docker container and host must have same UID and GID. docker 容器和主机内的 Mysql 用户和 Mongo DB 用户必须具有相同的 UID 和 GID。

Check them in both with:使用以下方法检查它们:

$ 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:

or with:或与:

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

As you can see UID is 999 and GID is also 999 for both user and group.如您所见,用户和组的 UID 是 999,GID 也是 999。 Now just repeat the same in the host.现在只需在主机中重复相同的操作。

Possible Solution可能的解决方案

Check the versions for Mysql and MongoDB in the host and in the container and if they mismatch, update the container to run the same versions of the host.检查主机和容器中的 Mysql 和 MongoDB 版本,如果它们不匹配,请更新容器以运行相同版本的主机。

Also set the user of Mysql and MongoDB in the host to have the same UID and GID that is used inside the container or the other way around... choose the one that best suits your use case.还要在主机中将 Mysql 和 MongoDB 的用户设置为具有在容器内使用的相同 UID 和 GID,或者相反……选择最适合您的用例的用户。

Changing UID and GID of a user更改用户的 UID 和 GID

run in the shell:在 shell 中运行:

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

replace 800 with the id number you want and mysql with the user name you want to change the ids.800替换为您想要的 ID 号,将mysql替换为您想要更改 ID 的用户名。

Next update the file permissions for that user:接下来更新该用户的文件权限:

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

and for the group:对于小组:

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

More info here更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM