英文:
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"**,
我尝试了这个解决方案,将参数**"?directConnection=true"添加到连接字符串中,这样Node.js或Golang就可以连接MongoDB了,但是无法读取**,出现以下错误**"not primary and secondaryOk=false"**,
顺便说一下,我还尝试了其他解决方案,但都没有起作用,比如将参数**"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",
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",
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 -->
"mongodb://name:passwd@ip:port?readPreference=secondary&directConnection=true"
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论