在 Debian 上修改 MongoDB 的端口,通常步骤如下(以 MongoDB 4.x / 5.x / 6.x 常见配置为例):
MongoDB 默认配置文件位置一般是:
/etc/mongod.conf
使用编辑器打开:
sudo nano /etc/mongod.conf
找到 net 部分,修改 port:
net:
port: 27018 # 改成你想要的端口
bindIp: 127.0.0.1
⚠️ 注意 YAML 格式,缩进必须是空格,不能是 Tab
保存后,重启 MongoDB:
sudo systemctl restart mongod
查看状态:
sudo systemctl status mongod
确认监听端口:
sudo ss -lntp | grep mongod
如果开启了 ufw 或 iptables,需要放行新端口:
sudo ufw allow 27018
mongo --port 27018
或
mongosh --port 27018
port + 不同 dbPath + 不同 logPath如果你是用 Docker / 源码安装 / 非默认路径,可以告诉我具体环境,我可以给你更精确的改法。