IT干货网

MongoDB 副本集 TLS/SSL

qq78292959 2023年12月10日 编程设计 264 0

我已经通过私有(private) IP 在 3 台服务器上成功启动了 MongoDB 4 副本集。现在我想绑定(bind)另一个 IP,它需要启用 TLS/SSL。

我已经创建了 PEMKeyFile 和 CAFile 并将这些文件复制到所有 3 个服务器上,并将以下代码添加到所有 3 个服务器的 mongod.config 文件中。

# network interfaces 
net: 
  port: 27017 
  bindIp: 10.10.20.21,5.22.25.45 # example private ip and one example valid IP 
  ssl: 
    mode: requireSSL 
    PEMKeyFile: /opt/mongo/mongo.pem 
    PEMKeyPassword: MyPassword 
    CAFile : /opt/mongo/CA.pem 
    allowInvalidCertificates: true 
    allowInvalidHostnames: true 
 
security: 
  keyFile: /opt/mongo/mongo-keyfile 

我遇到了错误

E STORAGE  [initandlisten] Failed to set up listener: SocketException: Cannot assign requested address 
I CONTROL  [initandlisten] now exiting 
I CONTROL  [initandlisten] shutting down with code:48 

它有什么问题?我该如何解决?

请您参考如下方法:

should I see both of these IPs

当然可以。

bindIp 告诉 mongodb 服务监听哪个系统网络接口(interface)。这些是本地系统接口(interface),而不是客户端。一旦 mongobd 绑定(bind)到一个接口(interface),来自任何地方的客户端都可以连接到这个 IP:

  • 绑定(bind)到 10.10.20.XXX:私有(private) A 类网络接口(interface)允许客户端从同一网络中的任何 10.XXX.XXX.XXX IP 连接
  • 绑定(bind)到 5.22.25.XXX :公共(public)网络接口(interface)允许客户端从互联网上的任何地方连接。

如果您想限制对 mongodb 的访问并只允许从特定 IP/网络连接,您需要启用身份验证并将限制应用于用户或组:https://docs.mongodb.com/manual/reference/method/db.createUser/#authentication-restrictions .

例如

use admin 
db.createUser( 
   { 
     user: "remote-client", 
     pwd: "password", 
     roles: [ { role: "readWrite", db: "reporting" } ], 
     authenticationRestrictions: [ { 
        clientSource: ["5.22.25.45"] 
     } ] 
   } 
) 

将允许 mongo -u remote-client -p password 仅从 IP 5.22.25.45 连接并允许读取和写入“报告”数据库。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!