使用云MongoDB--Atlas出现的连接问题

这是第一次在csdn上发布博客

最近在使用云MongoDB–Atlas时在连接数据库的时候虽然是使用官网给出的连接字符串但还是出现了莫名其妙的问题。想记录分享一下。这是第一次使用 Markdown编辑器 ,也是第一次使用csdn发布博客。嘻嘻嘻,比较简陋,但也是自己的尝试。

问题描述

在连接云MongoDB–Atlas时,虽然使用的连接字符串时官网上直接复制过来的,但是总是报错连接不上:
报错如图

解决过程

1、测试个端口是否可用
2、为避免是代码的问题,通过compass测试连接,查看问题是否出现在云数据库创建那边

最终解决方法

将连接字符串修改为旧版本格式

//需要修改为连接旧版本的字符串
module.exports = {
    mongoURI:"mongodb://TU2:1111@cluster0-shard-00-00.ggxst.mongodb.net:27017,cluster0-shard-00-01.ggxst.mongodb.net:27017,cluster0-shard-00-02.ggxst.mongodb.net:27017/restful-api-prod?ssl=true&replicaSet=atlas-11vnte-shard-0&authSource=admin&retryWrites=true&w=majority"
}

//错误版本

//错误版本
/*module.exports = {
    mongoURI:'mongodb+srv://TU2:1111@cluster0.ggxst.mongodb.net/restful-api-prod?retryWrites=true&w=majority'
}*/

出现错误原因

连接方式不同导致采用的不是公共DNS服务器, 而是本地DNS服务器

Most of the time, this is because you are not using a public DNS server such as Cloudflare orGoogle, but rather a localDNS server. Sometimes this seems to be caused by being on a public wifi network like in Starbucks, in an office environment where IT have set up a local DNS server, or a home DNS blocking service likePiHole. The workaround for this issue is to use the older connection string (the one I shared), or alternatively alter their DNS server to a public one。

译文:大多数情况下,这是因为您使用的不是Cloudflare orGoogle这样的公共DNS服务器,而是localDNS服务器。有时,这似乎是由于在星巴克这样的公共wifi网络上,在办公室环境中设置了本地DNS服务器,或者像pihole这样的家庭DNS阻塞服务造成的。
解决这个问题的方法是使用较旧的连接字符串(我共享的那个),或者将它们的DNS服务器更改为公共的。

新旧连接版本影响

不管采用新版的还是旧版的连接字符串,只要连接上了,后续操作都不会存在问题。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值