You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Compose部署Graylog连接本地MongoDB失败求助

解决Graylog容器连接宿主机MongoDB失败的问题

核心原因

容器内的127.0.0.1指向容器自身,而非宿主机,因此无法连接到宿主机上运行的MongoDB服务。

具体修复步骤

  1. 修正MongoDB连接地址

    • 将GRAYLOG_MONGODB_URI中的127.0.0.1替换为宿主机的实际IP地址(例如192.168.1.100),修改后的配置项如下:
      GRAYLOG_MONGODB_URI: "mongodb://192.168.1.100:27017/graylog"
      
    • 若使用Linux环境,也可通过Docker特殊域名host.docker.internal访问宿主机,但需在graylog服务中添加extra_hosts配置:
      graylog:
        # 其他配置保持不变
        extra_hosts:
          - "host.docker.internal:host-gateway"
      
      此时连接地址改为:
      GRAYLOG_MONGODB_URI: "mongodb://host.docker.internal:27017/graylog"
      
  2. 配置MongoDB允许远程连接

    • 编辑MongoDB配置文件(通常路径为/etc/mongod.conf),将bindIp设置为0.0.0.0(允许所有IP访问)或包含宿主机IP:
      net:
        bindIp: 0.0.0.0
      
    • 重启MongoDB服务使配置生效:
      sudo systemctl restart mongod
      
    • 确保MongoDB中存在graylog数据库的授权用户,执行以下MongoDB命令创建用户(若未创建):
      use graylog
      db.createUser({
        user: "graylog",
        pwd: "你的数据库密码",
        roles: [{ role: "readWrite", db: "graylog" }]
      })
      
      若使用授权用户,连接地址需带上用户名密码:
      GRAYLOG_MONGODB_URI: "mongodb://graylog:你的数据库密码@192.168.1.100:27017/graylog"
      
  3. 开放宿主机端口

    • 确保宿主机防火墙/安全组允许27017端口的入站流量,让Graylog容器能够访问:
      sudo ufw allow 27017/tcp
      

修改后的完整docker-compose示例

version: "3.8"

services:
  opensearch:
    image: "opensearchproject/opensearch:2.4.0"
    environment:
      - "OPENSEARCH_JAVA_OPTS=-Xms1g -Xmx1g"
      - "bootstrap.memory_lock=true"
      - "discovery.type=single-node"
      - "action.auto_create_index=false"
      - "plugins.security.ssl.http.enabled=false"
      - "plugins.security.disabled=true"
    ulimits:
      memlock:
        hard: -1
        soft: -1
      nofile:
        soft: 65536
        hard: 65536
    volumes:
      - "os_data:/usr/share/opensearch/data"
    restart: "on-failure"

  graylog:
    hostname: "server"
    image: "${GRAYLOG_IMAGE:-graylog/graylog:5.0}"
    depends_on:
      opensearch:
        condition: "service_started"
    entrypoint: "/usr/bin/tini -- wait-for-it opensearch:9200 --  /docker-entrypoint.sh"
    environment:
      GRAYLOG_NODE_ID_FILE: "/usr/share/graylog/data/config/node-id"
      GRAYLOG_PASSWORD_SECRET: "${GRAYLOG_PASSWORD_SECRET:?Please configure GRAYLOG_PASSWORD_SECRET in the .env file}"
      GRAYLOG_ROOT_PASSWORD_SHA2: "${GRAYLOG_ROOT_PASSWORD_SHA2:?Please configure GRAYLOG_ROOT_PASSWORD_SHA2 in the .env file}"
      GRAYLOG_HTTP_BIND_ADDRESS: "0.0.0.0:9000"
      GRAYLOG_HTTP_EXTERNAL_URI: "http://<server_ip_address>:9000/"
      GRAYLOG_ELASTICSEARCH_HOSTS: "http://opensearch:9200"
      # 替换为宿主机IP或host.docker.internal
      GRAYLOG_MONGODB_URI: "mongodb://192.168.1.100:27017/graylog"
    extra_hosts:
      - "host.docker.internal:host-gateway" # Linux环境需添加此行
    ports:
    - "5044:5044/tcp"   # Beats
    - "5140:5140/udp"   # Syslog
    - "5140:5140/tcp"   # Syslog
    - "5555:5555/tcp"   # RAW TCP
    - "5555:5555/udp"   # RAW TCP
    - "9000:9000/tcp"   # Server API
    - "12201:12201/tcp" # GELF TCP
    - "12201:12201/udp" # GELF UDP
    - "13301:13301/tcp" # Forwarder data
    - "13302:13302/tcp" # Forwarder config
    volumes:
      - "graylog_data:/usr/share/graylog/data/data"
      - "graylog_journal:/usr/share/graylog/data/journal"
    restart: "on-failure"

volumes:
  os_data:
  graylog_data:
  graylog_journal:

内容的提问来源于stack exchange,提问作者Mohamadamin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 04:20:36