nodejs or golang client can not access mongodb, however, mongsh can connect and read with same auth setting

huangapple go评论87阅读模式
英文:

nodejs or golang client can not access mongodb, however, mongsh can connect and read with same auth setting

问题

我开发了一个应用程序,可以很好地读写远程的MongoDB数据。最近,我们切换到了另一个具有主从设置的远程MongoDB实例,但是遇到了以下奇怪的问题。

Mongshell可以使用用户/密码的认证连接并读取MongoDB,但是Node.js或Golang客户端无法使用相同的认证访问,下面的图片显示了错误信息**"Server selection timed out after 30000 ms"**,

nodejs or golang client can not access mongodb, however, mongsh can connect and read with same auth setting

我尝试了这个解决方案,将参数**"?directConnection=true"添加到连接字符串中,这样Node.js或Golang就可以连接MongoDB了,但是无法读取**,出现以下错误**"not primary and secondaryOk=false"**,

nodejs or golang client can not access mongodb, however, mongsh can connect and read with same auth setting

顺便说一下,我还尝试了其他解决方案,但都没有起作用,比如将参数**"readPreference=primaryPreferred"**添加到连接字符串中。

英文:

I develop an app which working well on reading and writing data to remote mongodb, recently, we change to another remote mongodb instance with master-slave setting, however, encounter the following strange issue.

Mongshell can connect and read mongodb with auth of user/pwd, however, nodejs or golang client can not access it with the same auth, the following pic is the error info "Server selection timed out after 30000 ms",

nodejs or golang client can not access mongodb, however, mongsh can connect and read with same auth setting

I tried this solution to add parameter "?directConnection=true" to connection string, this time, nodejs or golang can connect the mongodb, however, can not read, with the following error "not primary and secondaryOk=false",

nodejs or golang client can not access mongodb, however, mongsh can connect and read with same auth setting

BTW, I also tried other solutions, but not working, such as, add parameter "readPreference=primaryPreferred" to connection string.

答案1

得分: 0

我通过在连接字符串中添加参数后缀"?readPreference=secondary&directConnection=true"来修复它,所以连接字符串看起来像这样:

"mongodb://name:passwd@ip:port?readPreference=secondary&directConnection=true"
英文:

I fix it by adding parameter suffix "?readPreference=secondary&directConnection=true" to connection string, so the connection string look like,

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&quot;mongodb://name:passwd@ip:port?readPreference=secondary&amp;directConnection=true&quot;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2022年7月5日 14:35:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/72864918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定