mongodb - mongo db docker 镜像认证失败
问题描述
我在本地 docker 环境中使用https://hub.docker.com/_/mongoAuthentication failed
mongo 映像,但出现错误。在docker-compose
我添加它像:
my-mongo:
image: mongo
restart: always
container_name: my-mongo
environment:
MONGO_INITDB_ROOT_USERNAME: mongo
MONGO_INITDB_ROOT_PASSWORD: asdfasdf
networks:
- mynet
我还尝试从容器内部运行 mongo CLI,但仍然遇到相同的错误:
root@76e6db78228b:/# mongo
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c87c0f0e-fe83-41a6-96e9-4aa4ede8fa25") }
MongoDB server version: 4.2.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
> use translations
switched to db translations
> db.auth("mongo", "asdfasdf")
Error: Authentication failed.
0
另外,我正在尝试创建一个单独的用户:
> use admin
switched to db admin
db.auth("mongo", "asdfasdf")
1
> db.createUser({
user: "user",
pwd: "asdfasdf",
roles: [ {role: "readWrite", db: "translations" } ]
})
Successfully added user: {
"user" : "user",
"roles" : [
{
"role" : "readWrite",
"db" : "translations"
}
]
}
> use translations
switched to db translations
> db.auth("user", "asdfasdf")
Error: Authentication failed.
0
同样,我做错了什么???
更新:
root@8bf81ef1fc4f:/# mongo -u mongo -p asdfasdf --authenticationDatabase admin
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("02231489-eaf4-40be-a108-248cec88257e") }
MongoDB server version: 4.2.3
Server has startup warnings:
2020-02-26T16:24:12.942+0000 I STORAGE [initandlisten]
2020-02-26T16:24:12.943+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-02-26T16:24:12.943+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> db.createUser({user: "someuser", pwd: "asdfasdf", roles: [{role: "readWrite", db: "translations"}]})
Successfully added user: {
"user" : "someuser",
"roles" : [
{
"role" : "readWrite",
"db" : "translations"
}
]
}
> use translations
switched to db translations
> db.auth("someuser", "asdfasdf")
Error: Authentication failed.
0
>
解决方案
这些变量一起使用,创建一个新用户并设置该用户的密码。该用户是在管理员身份验证数据库中创建的,并被赋予 root 角色,这是一个“超级用户”角色。
所以你需要添加--authenticationDatabase admin
到你的命令中,因为 mongod 将以mongod --auth
例子:
mongo -u mongo -p asdfasdf --authenticationDatabase admin
推荐阅读
- json - 如何从另一个目录打开 JSON 文件
- computation-theory - 不接受以“101”结尾的字符串的 NFA
- cookies - Safari 在什么情况下会阻止第三方 cookie?
- python - 如何让两个粒子在 tkinter python 3 中碰撞?
- c++ - (C++) 文本的颜色可以在输入时改变吗?
- firebase - SwiftUI:将多个图像上传到 Firebase
- node.js - 如何将日期类型从 MongoDB 重新格式化为 Angular
- html - 如何重新创建“左侧图像,右侧文本”和“左侧图像右侧文本”模式?
- javascript - 在javascript中导入问题加载插件
- javascript - 如何在 redux 工具包 next js 中从 localstorage 设置初始状态