Mongoose 连接到身份验证数据库语法以包括 auth 和 useNewUrlParser 选项
Posted
技术标签:
【中文标题】Mongoose 连接到身份验证数据库语法以包括 auth 和 useNewUrlParser 选项【英文标题】:Mongoose connect to authentication db syntax to include both auth and useNewUrlParser options 【发布时间】:2020-05-19 17:31:59 【问题描述】:我正在尝试本地连接到身份验证数据库以访问我的应用程序数据库。如果我包含 useNewUrlParser: true
选项,它会破坏整个事情,因为身份验证失败。删除useNewUrlParser
选项连接但不勾选此选项:
config =
database: "mongodb://localhost:27017/authapp",
auth:
user : "admin",
password : "123456",
authdb : "admin"
...
mongoose
.connect(config.database,
auth: config.auth,
useNewUrlParser: true
)
.then(() => ...
我明白了:
[nodemon] 开始
node index.js
(node:17814) DeprecationWarning: 当前的服务器发现和监控引擎已被弃用,并将 在未来的版本中被删除。要使用新的 Server Discover 和 监控引擎,将选项 useUnifiedTopology: true 传递给 MongoClient 构造函数。应用程序正在运行 4000 database_error: MongoNetworkError:无法连接到服务器 [localhost:27017] 首先连接 [MongoError: 身份验证失败。 在 Function._getError (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/auth/scram.js:141:14) 在 /Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/auth/scram.js:191:31 在 _callback (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connect.js:320:5) 在 Connection.messageHandler (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connect.js:349:5) 在 Connection.emit (events.js:321:20) 在 processMessage (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connection.js:384:10) 在套接字。 (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connection.js:553:15) 在 Socket.emit (events.js:321:20) 在 addChunk (_stream_readable.js:305:12) 在 readableAddChunk (_stream_readable.js:280:11) 在 Socket.Readable.push (_stream_readable.js:214:10) 在 TCP.onStreamRead (internal/stream_base_commons.js:186:23) 名称:'MongoError', [符号(mongoErrorContextSymbol)]:] 在游泳池。 (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/topologies/server.js:433:11) 在 Pool.emit (events.js:321:20) 在 /Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/pool.js:577:14 在 /Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/pool.js:1007:11 在回调(/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connect.js:93:5) 在 /Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connect.js:367:21 在 /Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/auth/auth_provider.js:66:11 在 /Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/auth/scram.js:193:16 在 _callback (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connect.js:320:5) 在 Connection.messageHandler (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connect.js:349:5) 在 Connection.emit (events.js:321:20) 在 processMessage (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connection.js:384:10) 在套接字。 (/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connection.js:553:15) 在 Socket.emit (events.js:321:20) 在 addChunk (_stream_readable.js:305:12) 在 readableAddChunk (_stream_readable.js:280:11) 名称:'MongoNetworkError', [符号(mongoErrorContextSymbol)]:
是否有我遗漏的模式或适用于这种情况的方法?我还在数据库连接字符串中尝试了用户+密码,但它也没有按预期连接 - 可能缺少身份验证数据库名称?
希望有人能为这个简单的问题指出正确的方向。
【问题讨论】:
【参考方案1】:使用 authSource 代替 authdb
auth:
user: "admin",
password: "123456",
authSource: "admin",
,
【讨论】:
您能否编辑您的答案以包含解释?为什么是 authSource?以上是关于Mongoose 连接到身份验证数据库语法以包括 auth 和 useNewUrlParser 选项的主要内容,如果未能解决你的问题,请参考以下文章
使用 Windows 身份验证连接到 sql db 的 Powershell 脚本
在节点应用程序上使用 mongoose 的远程 mongo 身份验证问题
在节点应用程序上使用 mongoose 的远程 mongo 身份验证问题
使用双跳 Windows 身份验证连接到 Microsoft Analysis Services 多维数据集
通过powershell Enter-PSSession连接到SQL Server 2012(使用Windows身份验证)