如何配置Debian MongoDB的网络设置
导读:Debian 上配置 MongoDB 的网络设置 一 基础网络参数配置 编辑配置文件:sudo nano /etc/mongod.conf 常用网络项说明: net.port:监听端口,默认 27017 net.bindIp:监听地址,...
Debian 上配置 MongoDB 的网络设置
一 基础网络参数配置
- 编辑配置文件:sudo nano /etc/mongod.conf
- 常用网络项说明:
- net.port:监听端口,默认 27017
- net.bindIp:监听地址,默认仅本地 127.0.0.1;如需远程访问,可改为服务器内网 IP 或 0.0.0.0(不推荐直接对公网开放)
- 示例(仅内网):
net: port: 27017 bindIp: 192.168.1.100 - 示例(监听多地址):
net: port: 27017 bindIp: 127.0.0.1,192.168.1.100
- 使配置生效:sudo systemctl restart mongod
- 注意:自 MongoDB 3.6 起,官方包默认只绑定 localhost,如需远程访问必须显式配置 bindIp。
二 防火墙与端口放行
- UFW(Debian 常用):
- 放行本机到 MongoDB 端口(示例):sudo ufw allow 27017/tcp
- 更安全的做法:仅允许内网或指定主机
- 允许某台主机:sudo ufw allow from 192.168.1.50 to any port 27017
- 允许整个内网段:sudo ufw allow from 192.168.1.0/24 to any port 27017
- 其他防火墙(如 iptables/nftables)同样需放行 TCP 27017,并尽量限制来源网段。
三 远程连接与安全加固
- 启用身份验证(强烈建议):
- 配置:
security: authorization: enabled - 重启:sudo systemctl restart mongod
- 创建管理员并验证:
mongo use admin db.createUser({ user: "admin", pwd: "StrongPass!", roles: ["root"] } ) exit mongo -u admin -p StrongPass! --authenticationDatabase admin
- 配置:
- 限制监听范围:生产环境优先绑定内网 IP(如 192.168.x.x),避免使用 0.0.0.0 直连公网。
- 加密传输(可选但推荐):
net: ssl: mode: requireSSL PEMKeyFile: /path/to/mongodb.pem CAFile: /path/to/ca.pem - 安全要点归纳:绑定局域网 IP、配置防火墙限制来源、开启账号密码认证。
四 连接测试与排障
- 服务状态与端口:
- 查看状态:sudo systemctl status mongod
- 监听端口:ss -lntp | grep 27017 或 sudo lsof -iTCP:27017 -sTCP:LISTEN
- 本机与远程连通性:
- 本机:mongo --host 127.0.0.1 --port 27017
- 远程:mongo --host 服务器IP --port 27017
- 端口探测:telnet 服务器IP 27017 或 nc -vz 服务器IP 27017
- 常见原因:bindIp 未包含客户端网段、防火墙未放行、服务未启动、认证失败。
五 附录 常用配置片段
- 仅本地访问(默认最安全)
net: port: 27017 bindIp: 127.0.0.1 - 监听内网单 IP
net: port: 27017 bindIp: 192.168.1.100 - 监听多地址(本地 + 内网)
net: port: 27017 bindIp: 127.0.0.1,192.168.1.100 - 启用认证
security: authorization: enabled - 启用 TLS/SSL
net: ssl: mode: requireSSL PEMKeyFile: /path/to/mongodb.pem CAFile: /path/to/ca.pem - 应用修改后务必执行:sudo systemctl restart mongod。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: 如何配置Debian MongoDB的网络设置
本文地址: https://pptw.com/jishu/778379.html
