无法从节点连接AWS文档DB

发布于 2025-01-27 23:49:59 字数 1863 浏览 5 评论 0 原文

这是我的node.js代码连接文档db的代码。我也有SSL文件。我能够从蒙古尔(Mongoshell)连接,但无法从node.js应用程序连接,

var mongoose = require("mongoose");
//db connection
const dbConnect = require("./dbConnection.js");
const globalErrorMiddleware = require("./middlewares/appErrorHandler");

mongoose.connect(
"mongodb://lbw:dkdDKsKsl@dev-docdb-cluster-1.cluster-ct090j7ac20n.us-east-1.docdb.amazonaws.com:27017/cnox?ssl=true&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false",
process.env.ENV === "LIVE"
    ? {
        ssl: true,
        sslValidate: true,
        sslCA: `rds-combined-ca-bundle.pem`,
    }
    : {}
);

我在连接到DB时的错误低于错误。请看看

app listening on port: 5600
/home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/connection.js:203
                    callback(new error_1.MongoServerError(document));
                            ^

MongoServerError: Authorization failure
    at Connection.onMessage (/home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/connection.js:203:30)
    at MessageStream.<anonymous> (/home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/connection.js:63:60)
    at MessageStream.emit (node:events:527:28)
    at MessageStream.emit (node:domain:475:12)
    at processIncomingData (/home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/message_stream.js:108:16)
    at MessageStream._write (/home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/message_stream.js:28:9)
    at writeOrBuffer (node:internal/streams/writable:390:12)
    at _write (node:internal/streams/writable:331:10)
    at MessageStream.Writable.write (node:internal/streams/writable:335:10)
    at TLSSocket.ondata (node:internal/streams/readable:777:22) {
ok: 0,
operationTime: Timestamp { low: 1, high: 1652288522, unsigned: true },
code: 13,
[Symbol(errorLabels)]: Set(0) {}
}

Here is my node.js code using to connect document db. I have ssl file also there. I am able to connect from mongoshell but not working from node.js app

var mongoose = require("mongoose");
//db connection
const dbConnect = require("./dbConnection.js");
const globalErrorMiddleware = require("./middlewares/appErrorHandler");

mongoose.connect(
"mongodb://lbw:dkdDKsKsl@dev-docdb-cluster-1.cluster-ct090j7ac20n.us-east-1.docdb.amazonaws.com:27017/cnox?ssl=true&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false",
process.env.ENV === "LIVE"
    ? {
        ssl: true,
        sslValidate: true,
        sslCA: `rds-combined-ca-bundle.pem`,
    }
    : {}
);

I am getting below error when connecting to db . Please take a look

app listening on port: 5600
/home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/connection.js:203
                    callback(new error_1.MongoServerError(document));
                            ^

MongoServerError: Authorization failure
    at Connection.onMessage (/home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/connection.js:203:30)
    at MessageStream.<anonymous> (/home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/connection.js:63:60)
    at MessageStream.emit (node:events:527:28)
    at MessageStream.emit (node:domain:475:12)
    at processIncomingData (/home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/message_stream.js:108:16)
    at MessageStream._write (/home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/message_stream.js:28:9)
    at writeOrBuffer (node:internal/streams/writable:390:12)
    at _write (node:internal/streams/writable:331:10)
    at MessageStream.Writable.write (node:internal/streams/writable:335:10)
    at TLSSocket.ondata (node:internal/streams/readable:777:22) {
ok: 0,
operationTime: Timestamp { low: 1, high: 1652288522, unsigned: true },
code: 13,
[Symbol(errorLabels)]: Set(0) {}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆萌少年 2025-02-03 23:50:00

我在使用MongoDB JavaScript客户端时遇到了一个非常相似的问题。与其他一些客户端相比,Mongo Shell对服务器配置更宽容。

您可以尝试几件事:

  1. directConnection = true 添加到连接字符串。
  2. 添加 authsource = CNOX
  3. 单独尝试并组合尝试。

https://mongodb.github.io/node-mongodb-native/2.2/tutorials/connect/authenticatikating/

另一个猜测是您成功地连接,但是代码是代码正在尝试未经授权的东西。就像尝试与 cnox 以外的其他数据库进行交互或运行当前用户权利不支持的命令。

I was running to a very similar problem with using the mongodb javascript client. The mongo shell is much more forgiving to the server configuration than the some of the other clients.

There a couple of things you can try:

  1. add directConnection=true to your connection string.
  2. add authSource=cnox
  3. try those separately and in combination.

https://www.mongodb.com/docs/drivers/node/current/fundamentals/connection/connect/#direct-connection

https://mongodb.github.io/node-mongodb-native/2.2/tutorials/connect/authenticating/

Another guess is that you are successfully connecting but then the code is trying to something that isn't authorized. Like trying to interact with another database other than cnox or running a command that isn't supported by the current user rights.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文