首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >deno连接mongodb失败

deno连接mongodb失败
EN

Stack Overflow用户
提问于 2021-02-05 23:27:10
回答 2查看 354关注 0票数 5

我试图将我的deno应用程序连接到mongodb,但是我得到了错误。

代码语言:javascript
复制
import {MongoClient} from "https://deno.land/x/mongo@v0.21.2/mod.ts";

const client = await new MongoClient();
await client.connect("mongodb+srv://deno:i4vY8AtCEhr6ReqB@sample.7jp1l.mongodb.net/deno?retryWrites=true&w=majority");

const db = client.database("notes");

export default db;

一切似乎都很好,但是当我运行这个应用程序时,我得到了这个错误。

代码语言:javascript
复制
error: Uncaught (in promise) Error: MongoError: "Connection failed: failed to lookup address information: nodename nor servname provided, or not known"
                throw new MongoError(`Connection failed: ${e.message || e}`);
              ^
    at MongoClient.connect (client.ts:93:15)
    at async mongodb.ts:4:1
EN

回答 2

Stack Overflow用户

发布于 2021-04-12 23:19:27

我看到的两个问题:

  • 上面的代码片段只适用于安装在本地计算机上的Mongo。
  • 连接字符串使用DNS种子列表,但当前库无法解析为主机列表

要使其与Mongo Atlas一起工作,您需要使用不同的参数调用DNS connect方法,并找到正确的(静态)主机,而不是(动态)DNS种子列表:

代码语言:javascript
复制
const client = new MongoClient();

const db = await client.connect({
  db: '<your db or collection with work with>',
  tls: true,
  servers: [
    {
      host: '<correct host - the way to get the host - see bellow>',
      port: 27017,
    },
  ],
  credential: {
    username: '<your username>',
    password: '<your password>',
    mechanism: 'SCRAM-SHA-1',
  },
});

如何获取正确的主机:

  • 在later
  • Here中打开集群
  • 选择Connect

button

  • Select 连接到应用程序 Mongo驱动程序:Node.js和版本:2.2.12或later
  • Here您将看到@字符
票数 3
EN

Stack Overflow用户

发布于 2021-09-13 08:55:51

谢谢你,@nthung.vlvn给我的提示。实际上,主机需要是主分片。它修复了lookup address information,但我有另一个错误,那就是我的凭据不正确。我必须将数据库"admin“添加到凭据:

代码语言:javascript
复制
credential: {
    username: '<your username>',
    password: '<your password>',
    db: "admin",
    mechanism: 'SCRAM-SHA-1',
}

这很奇怪,因为我的Atlas中没有admin db。不管怎样,它开始起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66065998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档