关于mongoDB连接的问题

关于mongoDB连接的问题

老师,我把docker里mongoDB是停止了,然后重启服务器,auth服务没有报错,但只要访问使用mongo的方法就会报server selection error: server selection timeout超时。
我的意思是auth.main方法里连接mongo客户端咋不报错cannot connect mongdb呀,需要额外加ping()方法检查连接超时么?
图片描述

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
ccmouse 2021-12-20 18:12:17

mongo,包括grpc client,都是在Connect的时候并没有真正去connect,而是放到了之后。Connect的文档说:

// The NewClient function does not do any I/O and returns an error if the given options are invalid.

// The Client.Connect method starts background goroutines to monitor the state of the deployment and does not do

// any I/O in the main goroutine to prevent the main goroutine from blocking. Therefore, it will not error if the

// deployment is down.

//

// The Client.Ping method can be used to verify that the deployment is successfully connected and the

// Client was correctly configured.

就是在Connect的时候只是开启了后台用来检测状态的goroutine,但不会i真正的去做I/O,所以不会在无法连接的时候报错。它也说了用Client.Ping可以检测链接是否健康。

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师