可以将现有已安装的 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
Run Code Online (Sandbox Code Playgroud)
日志:
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)
Run Code Online (Sandbox Code Playgroud)
因为我做了一些更改,所以出现了不同的错误。有时容器崩溃,有时无法连接到 mysql (111)
问题可能是 Mysql 和 MongoDB 的版本和/或 和 的用户 UID 和/var/lib/mysql
GID /data/db/mongo
。
首先,容器上运行的 Mysql 版本和 MongoDB 版本需要与主机上运行的版本相同,以便您能够使用主机中使用的相同文件夹。
请记住,您不能让 Mysql 服务器和 MongoDB 服务器在主机和容器中同时运行,同时共享相同的文件。
/var/lib/mysql
我的意思是和你的主机上的相同文件/data/db/mongo
。
如果您想让服务器在本地主机中运行,那么您需要将此文件夹复制到其他地方,并为用户提供相同的 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:
Run Code Online (Sandbox Code Playgroud)
或与:
root@4c07b20e88c4:/# id -u mysql
999
root@4c07b20e88c4:/# id -g mysql
999
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,对于用户和组来说,UID 是 999,GID 也是 999。现在只需在主机中重复相同的操作即可。
检查主机和容器中Mysql和MongoDB的版本,如果不匹配,则更新容器以运行与主机相同版本的容器。
还要将主机中的 Mysql 和 MongoDB 用户设置为与容器内使用的 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
Run Code Online (Sandbox Code Playgroud)
替换800
为您想要的 ID 号以及mysql
您想要更改 ID 的用户名。
接下来更新该用户的文件权限:
find / -user <OLDUID> -exec chown -h <NEWUID> {} \;
Run Code Online (Sandbox Code Playgroud)
对于团体:
find / -group <OLDGID> -exec chgrp -h <NEWGID> {} \;
Run Code Online (Sandbox Code Playgroud)
更多信息请点击此处
归档时间: |
|
查看次数: |
10328 次 |
最近记录: |