【问题标题】:how to use a environmental variable as Bind-Ip in mongo db configuration file (mongod.conf)如何在 mongo db 配置文件 (mongod.conf) 中使用环境变量作为 Bind-Ip
【发布时间】:2018-12-11 21:30:33
【问题描述】:

我是 mongodb 的新手,据我所知,我们总是在 mongod.conf 文件中将 127.0.0.1 或 172.17.0.5 等常量 IP 值作为绑定 IP 传递。

这是我的 mongod.conf 中的绑定 ip 配置>

net:
  port: 27017
  bindIp: 127.0.0.1, 172.17.0.5  # Listen to local interface only, comment to listen on all interfaces.

我在 /etc/environment 文件中定义了一个环境变量 DHOST= 172.17.0.5

当我尝试在 mongod.conf 中提供以下配置时,我无法连接到 mongo shell:

net:
  port: 27017
  bindIp: 127.0.0.1, *$DHOST*  # Listen to local interface only, comment to listen on all interfaces.

请帮我在 mongo db 配置中添加一个 ENV var 作为绑定 ip

【问题讨论】:

    标签: mongodb docker docker-compose centos7


    【解决方案1】:

    对于在 Docker 容器中运行的东西,您几乎应该始终绑定到 IPv4 0.0.0.0 或 IPv6 ::0(即“所有地址”)。 docker run -p 选项有一个可选字段,可以限制已发布端口将绑定到的主机上的 IP 地址;如果没有这样的配置,就无法直接从主机外访问容器,因此尝试绑定到容器内的特定接口并不是特别有用。

    【讨论】:

    • 感谢 David Maze 提供的信息丰富的回复。但是当我们将 Bind Ip 指定为 IPv4 0.0.0.0 时,假设我们有 mongo db 的三个副本集(并且三个副本集是同一主机中的三个 docker 容器),如何将副本集与0.0.0.0?
    • 每个容器都在自己的网络空间中,因此它们都可以成功使用相同的绑定参数而不会发生冲突。
    猜你喜欢
    • 2019-11-08
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多