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

MERN项目无法连接MongoDB Atlas求助:已配置IP白名单且依赖VPN

解决VPN环境下MERN栈连接MongoDB Atlas的MongooseServerSelectionError问题

问题背景

正在开发MERN栈项目,计划使用MongoDB Atlas作为云数据库,但连接时触发MongooseServerSelectionError,提示无法连接集群节点。已将本地IP加入Atlas的IP白名单,也尝试过「允许所有IP访问」选项;由于所在地区必须借助VPN才能访问外部服务,需要解决如何通过VPN正常连接MongoDB Atlas的问题。

报错信息

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://www.mongodb.com/docs/atlas/security-whitelist/
at _handleConnectionErrors (C:\Users\gedi\Desktop\stephan\car\server\node_modules\mongoose\lib\connection.js:755:11)
at NativeConnection.openUri (C:\Users\gedi\Desktop\stephan\car\server\node_modules\mongoose\lib\connection.js:730:11)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async mongodb (C:\Users\gedi\Desktop\stephan\car\server\config\db.js:4:3) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'ac-walbciw-shard-00-00.rhayeki.mongodb.net:27017' => [ServerDescription],
'ac-walbciw-shard-00-01.rhayeki.mongodb.net:27017' => [ServerDescription],
'ac-walbciw-shard-00-02.rhayeki.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-m5db59-shard-0',
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},

相关代码

db.js 文件

const mongoose = require("mongoose");
async function mongodb() {
   await mongoose.connect("mongodb+srv://user: test123@cluster0.rhayeki.mongodb.net/? retryWrites=true&w=majority", { useNewUrlParser: false });
   console.log("___________database connected___________");
 }
mongodb().catch((err) => console.log(err));

module.exports = mongoose;

模型文件

const mongoose = require("./../config/db");

const schema = new mongoose.Schema({
  carPhoto: String,
  mark: String,
  price: Number,
  ac: String,
  model: String,
  door: String,
  transmission: String,
  fuel: String,
  year: String,
  active: { type: Boolean, default: true },
  status: { type: String, default: "available" },
});

exports.Car = mongoose.model("car", schema);

解决方案

  1. 更新IP白名单为VPN出口IP

    • 连接VPN后,获取当前公网IP
    • 登录MongoDB Atlas控制台,将该VPN出口IP添加到集群IP白名单;若VPN为动态IP,可临时开启「允许所有IP访问(0.0.0.0/0)」测试
  2. 修复连接字符串错误

    • 连接字符串中密码部分存在空格:user: test123,需去掉空格改为user:test123
    • 建议直接从Atlas控制台复制生成的连接字符串,避免手动输入错误
  3. 调整Mongoose连接配置

    • 将useNewUrlParser: false改为useNewUrlParser: true,适配新的连接字符串格式
    • 添加稳定性配置项:
      {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        serverSelectionTimeoutMS: 5000 // 延长连接超时时间
      }
      
  4. 排查VPN网络限制

    • 确认VPN未屏蔽27017端口或MongoDB Atlas域名,可联系服务商核实
    • 切换不同地区的VPN节点测试连接
  5. 验证网络连通性

    • 使用ping命令测试Atlas集群节点,例如:ping ac-walbciw-shard-00-00.rhayeki.mongodb.net
    • 若无法ping通,说明VPN存在网络阻断,需更换VPN或调整设置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 12:28:31