【发布时间】:2020-02-06 06:21:45
【问题描述】:
无法在“docker-compose”上连接数据库
你好。
我尝试连接数据库但无法连接。
您能告诉我一些解决这些问题的技巧吗?
码头工人-v
- Docker 版本 19.03.5,构建 633a0ea
docker-compose -v
- docker-compose 版本 1.25.4,构建 8d51620a
操作系统
- Centos8
我正在尝试启动 redmine,所以我从 bitnami 的 github repo 中获取“docker-compose.yml”
https://github.com/bitnami/bitnami-docker-redmine
然后运行这个命令
curl -sSL https://raw.githubusercontent.com/bitnami/bitnami-docker-redmine/master/docker-compose.yml > docker-compose.yml
并检查文件。
version: '3'
services:
mariadb:
image: 'bitnami/mariadb:10.3'
environment:
- ALLOW_EMPTY_PASSWORD=yes
- MARIADB_USER=bn_redmine
- MARIADB_DATABASE=bitnami_redmine
volumes:
- 'mariadb_data:/bitnami'
redmine:
image: 'bitnami/redmine:latest'
environment:
- REDMINE_DB_USERNAME=bn_redmine
- REDMINE_DB_NAME=bitnami_redmine
ports:
- '80:3000'
volumes:
- 'redmine_data:/bitnami'
depends_on:
- mariadb
volumes:
mariadb_data:
driver: local
redmine_data:
driver: local
它看起来不错,所以我在下面运行。
docker-compose up -d
并等待几分钟并使用此命令检查日志。
docker-compose logs
它显示此日志。它说“执行 'postInstallation' 时出错:尝试 36 次后无法连接到 mariadb:3306”。
mariadb_1 | mariadb 05:22:19.39
mariadb_1 | mariadb 05:22:19.40 Welcome to the Bitnami mariadb container
mariadb_1 | mariadb 05:22:19.40 Subscribe to project updates by watching https://github.com/bitnami/bitnami-docker-mariadb
mariadb_1 | mariadb 05:22:19.40 Submit issues and feature requests at https://github.com/bitnami/bitnami-docker-mariadb/issues
mariadb_1 | mariadb 05:22:19.40 Send us your feedback at containers@bitnami.com
mariadb_1 | mariadb 05:22:19.40
mariadb_1 | mariadb 05:22:19.40 INFO ==> ** Starting MariaDB setup **
mariadb_1 | mariadb 05:22:19.44 INFO ==> Validating settings in MYSQL_*/MARIADB_* env vars
mariadb_1 | mariadb 05:22:19.44 WARN ==> You set the environment variable ALLOW_EMPTY_PASSWORD=yes. For safety reasons, do not use this flag in a production environment.
mariadb_1 | mariadb 05:22:19.48 INFO ==> Initializing mariadb database
mariadb_1 | mariadb 05:22:19.51 INFO ==> Installing database
redmine_1 |
redmine_1 | Welcome to the Bitnami redmine container
redmine_1 | Subscribe to project updates by watching https://github.com/bitnami/bitnami-docker-redmine
redmine_1 | Submit issues and feature requests at https://github.com/bitnami/bitnami-docker-redmine/issues
redmine_1 | Send us your feedback at containers@bitnami.com
redmine_1 |
mariadb_1 | mariadb 05:22:23.05 INFO ==> Starting mariadb in background
redmine_1 | nami INFO Initializing redmine
redmine_1 | redmine INFO Configuring Redmine database...
redmine_1 | mysql-c INFO Trying to connect to MySQL server
mariadb_1 | mariadb 05:22:24.12 INFO ==> Configuring authentication
mariadb_1 | mariadb 05:22:24.17 INFO ==> Running mysql_upgrade
mariadb_1 | mariadb 05:22:24.35 INFO ==> Stopping mariadb
mariadb_1 | mariadb 05:22:26.38 INFO ==> ** MariaDB setup finished! **
mariadb_1 |
mariadb_1 | mariadb 05:22:26.42 INFO ==> ** Starting MariaDB **
mariadb_1 | 2020-02-06 5:22:26 0 [Note] /opt/bitnami/mariadb/sbin/mysqld (mysqld 10.3.22-MariaDB) starting as process 1 ...
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: Using Linux native AIO
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: Uses event mutexes
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: Number of pools: 1
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: Using SSE2 crc32 instructions
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: Completed initialization of buffer pool
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: Creating shared tablespace for temporary tables
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: Waiting for purge to start
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: 10.3.22 started; log sequence number 1625457; transaction id 21
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: Loading buffer pool(s) from /bitnami/mariadb/data/ib_buffer_pool
mariadb_1 | 2020-02-06 5:22:26 0 [Note] Plugin 'FEEDBACK' is disabled.
mariadb_1 | 2020-02-06 5:22:26 0 [Note] InnoDB: Buffer pool(s) load completed at 200206 5:22:26
mariadb_1 | 2020-02-06 5:22:26 0 [Note] Server socket created on IP: '0.0.0.0'.
mariadb_1 | 2020-02-06 5:22:26 0 [Warning] 'proxies_priv' entry '@% root@1643f97fe62c' ignored in --skip-name-resolve mode.
mariadb_1 | 2020-02-06 5:22:26 0 [Note] Reading of all Master_info entries succeeded
mariadb_1 | 2020-02-06 5:22:26 0 [Note] Added new Master_info '' to hash table
mariadb_1 | 2020-02-06 5:22:26 0 [Note] /opt/bitnami/mariadb/sbin/mysqld: ready for connections.
mariadb_1 | Version: '10.3.22-MariaDB' socket: '/opt/bitnami/mariadb/tmp/mysql.sock' port: 3306 Source distribution
redmine_1 | Error executing 'postInstallation': Failed to connect to mariadb:3306 after 36 tries
redmine_redmine_1 exited with code 1
看起来像数据库问题,所以我尝试访问此实例并尝试连接 mariadb。
工作正常,可以查看数据库。
docker exec -it <mariadb's ID> /bin/bash
--------------------------------------------------------------------------------
I have no name!@c97be876fcf9:/$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.22-MariaDB Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| bitnami_redmine |
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.001 sec)
MariaDB [(none)]>
所以我认为这是端口问题,我添加了使用特定端口 3306 并在主机中打开 3306 端口的选项。
ports:
- '3306:3306'
还是不行。
那我想试试另一个“docker-compose.yml”。
所以尝试创建新的 docker-compose.yml
docker-compose down -v
rm -rf docker-compose.yml
vi docker-compose.yml
并添加此参数
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
db:
image: mysql:latest
restart: always
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'
~
这个使用 WordPress 和 MySQL 和我之前使用的不同。
但它不起作用。
可以访问WordPress的网站,但无法连接数据库。
所以我认为这些问题是无法正确桥接每个容器,尤其是应用程序到数据库容器。
但我不知道应该尝试什么。
有人有技巧请告诉我你的知识。
最好的问候。
ps 2020-02-07
我试过了
systemctl stop firewalld
systemctl restart docker
它成功了。
看起来像防火墙问题。 但还是 不知道怎么回事……
【问题讨论】:
-
补充:这些“docker-compose.yml”在“docker for windows”上工作
-
和Centos8挂载在VPS上
标签: mysql docker docker-compose mariadb