Not authorized on admin to execute command

NextJS使用mongoose连接数据库的时候报错 …

MongoServerError: Authentication failed.
at Connection.onMessage (/Users/net.cctv3.i/chnqoo-recollections-service/node_modules/mongodb/lib/cmap/connection.js:205:26)
at MessageStream.<anonymous> (/Users/net.cctv3.i/chnqoo-recollections-service/node_modules/mongodb/lib/cmap/connection.js:64:60)
at MessageStream.emit (node:events:518:28)
at processIncomingData (/Users/net.cctv3.i/chnqoo-recollections-service/node_modules/mongodb/lib/cmap/message_stream.js:117:16)
at MessageStream._write (/Users/net.cctv3.i/chnqoo-recollections-service/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
at writeOrBuffer (node:internal/streams/writable:564:12)
at _write (node:internal/streams/writable:493:10)
at Writable.write (node:internal/streams/writable:502:10)
at Socket.ondata (node:internal/streams/readable:1007:22)
at Socket.emit (node:events:518:28)
at addChunk (node:internal/streams/readable:559:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
at Readable.push (node:internal/streams/readable:390:5)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
at TCP.callbackTrampoline (node:internal/async_hooks:130:17) {
ok: 0,
code: 18,
codeName: 'AuthenticationFailed',
connectionGeneration: 0,
[Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
}
阅读更多

MongoDB 设置远程连接

修改配置文件mongod.conf

Linux配置文件路径,/etc/mongod.conf

net:
port: 27017
- bindIp: 127.0.0.1
+ bindIp: 0.0.0.0

# 密码登录
security:
authorization: enabled

创建用户

> use admin
switched to db admin
> db.createUser({
user: '??????',
pwd: '123456',
roles: [{role: 'userAdminAnyDatabase', db: 'admin'}],
});
阅读更多

Not authorized on admin to execute command

MongoDB 使用账号和密码登录,操作数据库的时候报错,

10:07:12 PM	Error: Failed to refresh 'Collections'. ListCollections failed: { ok: 0.0, errmsg: "not authorized on admin to execute command { listCollections: 1, filter: {}, cursor: {}, $readPreference: { mode: "secondaryPreferred" }, $db: "admin" }", code: 13, codeName: "Unauthorized" }
阅读更多

Linux/Debian/Ubuntu 安装 MongoDB

准备工作

导入包管理系统使用的公钥

在终端上,发出以下命令以从https://www.mongodb.org/static/pgp/server-4.2.asc导入MongoDB公共GPG密钥:

root@VM-4-9-debian:~# wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt
-key add -
E: gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this
operation
阅读更多