无法使用“ docker-compose”连接(桥接)任何数据库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法使用“ docker-compose”连接(桥接)任何数据库相关的知识,希望对你有一定的参考价值。

无法在“ docker-compose”上连接数据库

你好。

我尝试连接到数据库但无法建立。

您能告诉我一些解决这些问题的技巧吗?

docker -v

  • Docker版本19.03.5,内部版本633a0ea

docker-compose -v

  • docker-compose版本1.25.4,内部版本8d51620a

OS

  • Centos8

我正在尝试启动redmine,所以我从bitnami的github存储库中获取“ 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

并且有效。

看来是防火墙问题。但是还是我不知道怎么了...

答案

我解决了这个问题!!

firewall-cmd --zone=public --add-masquerade --permanent
firewall-cmd --reload
systemctl restart docker

如果不行,请尝试此。


firewall-cmd --permanent --zone=trusted --add-interface=docker0
firewall-cmd --permanent --zone=trusted --add-port=4243/tcp

firewall-cmd --reload
systemctl restart docker

以上是关于无法使用“ docker-compose”连接(桥接)任何数据库的主要内容,如果未能解决你的问题,请参考以下文章

使用 docker-compose 无法在同一网络中连接 mysql 和 spring boot

docker-compose:nodejs + mysql无法连接mysql

Docker-Compose WordPress MySql 无法连接

无法将docker Spring-Boot应用程序与docker-compose中的mysql容器和flyway连接起来

docker-compose部署mysql无法访问

Django / Docker-compose:重试数据库连接时:django.db.utils.OperationalError:(2002,“无法连接到'db'(115)上的MySQL服务器”)