我试图将我的deno应用程序连接到mongodb,但是我得到了错误。
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;一切似乎都很好,但是当我运行这个应用程序时,我得到了这个错误。
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发布于 2021-04-12 23:19:27
我看到的两个问题:
要使其与Mongo Atlas一起工作,您需要使用不同的参数调用DNS connect方法,并找到正确的(静态)主机,而不是(动态)DNS种子列表:
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',
},
});如何获取正确的主机:
button
发布于 2021-09-13 08:55:51
谢谢你,@nthung.vlvn给我的提示。实际上,主机需要是主分片。它修复了lookup address information,但我有另一个错误,那就是我的凭据不正确。我必须将数据库"admin“添加到凭据:
credential: {
username: '<your username>',
password: '<your password>',
db: "admin",
mechanism: 'SCRAM-SHA-1',
}这很奇怪,因为我的Atlas中没有admin db。不管怎样,它开始起作用了。
https://stackoverflow.com/questions/66065998
复制相似问题