简体   繁体   English

MongoDB 热重载时 Atlas 连接速度非常慢

[英]MongoDB Atlas very slow connection on hot reload

I have been working on a React application that would connect to MongoDB Atlas with Mongoose .我一直在研究一个React 应用程序,它将连接到MongoDB Atlas 和 Mongoose Using nodemon and webpack for hot reloading it would reconnect to Mongo within milliseconds every time there was a reload.使用 nodemon 和 webpack 进行热重载,每次重载时它都会在几毫秒内重新连接到 Mongo。 It was working nicely like this for months, then suddenly began taking 20+ seconds to connect on every reload - which has significantly increased dev time.它像这样工作了几个月,然后突然开始需要20 多秒才能在每次重新加载时连接 - 这显着增加了开发时间。

I cannot figure out what caused this to start happening and how to fix it.我无法弄清楚是什么原因导致这种情况开始发生以及如何解决它。 Any advice would be greatly appreciated.任何建议将不胜感激。

Note: My IP Address has already been whitelisted.注意:我的IP地址已经加入白名单了。

const db = "mongodb+srv://<username>:<password>@cluster0-omitted.mongodb.net/<dbname>?retryWrites=true&w=majority"

const connectDB = async () => {
  try {
    console.log('Attempting to connect to MongoDB...');
    await mongoose.connect(db, {
      useNewUrlParser: true,
      useCreateIndex: true,
      useFindAndModify: false,
      useUnifiedTopology: true,
    });
    console.log('MongoDB connected...');
  } catch (err) {
    console.log(err.message);
    process.exit(1); // Exit process with failure
  }
};

I was having this same issue, MongoDB Compass taking 10+ seconds to connect, suddenly all my tests started to fail and hot reload while in local dev server was taking 10+ seconds..我遇到了同样的问题,MongoDB Compass 需要 10 多秒才能连接,突然间我所有的测试都开始失败,并且在本地开发服务器中热重载需要 10 多秒..

I had recently moved to a new MacBook Pro M1 device, and my previous laptop was connecting almost instantly.我最近换了一台新的 MacBook Pro M1 设备,而我以前的笔记本电脑几乎可以立即连接。

Eventually I figured out I was missing 8.8.8.8 in my.network settings.最终我发现我在 my.network 设置中缺少8.8.8.8

Mac 系统偏好设置 > 网络 > Wi-Fi > DNS

As ridiculous as it sounds, reboot your router.听起来很荒谬,请重新启动路由器。 Or more technically, you have a problem with DNS and rebooting can resolve it.或者从技术上讲,您遇到了 DNS 的问题,重新启动可以解决它。

I had a similar problem after a power failure at home.家里停电后,我也遇到了类似的问题。 When the NAS came back not all Docker containers came back up, including my pihole.当 NAS 回来时,并不是所有的 Docker 容器都回来了,包括我的 pihole。 DNS seemed fine elsewhere so I didn't notice, but MongoDB Atlas specifically took 20 seconds to connect. DNS 在其他地方似乎很好,所以我没有注意到,但是 MongoDB Atlas 特别花了 20 秒来连接。 After starting the Pihole docker container, it now connects in 600ms, which is much closer to normal.启动 Pihole docker 容器后,它现在在 600 毫秒内连接,这更接近正常。

I don't know if this helps you out but have you tried to change IP allowed list to all and try it again.我不知道这是否对您有帮助,但您是否尝试过将 IP 允许列表更改为所有人,然后再试一次。 I had the same issue where I had assigned my network IP to allowed list.我有同样的问题,我将我的网络 IP 分配到允许列表。 After changing it to allow all IP in MongoDB dashboard it seems to have solved the problem.在将其更改为允许 MongoDB 仪表板中的所有 IP 后,它似乎已经解决了问题。

Just go to the Network Access page and just update the IP access list.只需 go 到网络访问页面,只需更新 IP 访问列表。 it fixed problem.它解决了问题。

  1. Connect to MongoDB Compass as usual像往常一样连接到 MongoDB 指南针
  2. Copy the three hosts shown where indicated below复制下面所示的三个主机

  1. Replace your old connection string for the following one:将旧连接字符串替换为以下连接字符串:
mongodb://<username>:<password>@<HOST1>,<HOST2>,<HOST3>/<dbname>?authSource=admin&ssl=true&retryWrites=true&w=majority

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM