[英]Docker compose mongo wont create root user
I am trying to create to compose a docker container with a mongo image.我正在尝试创建一个带有 mongo 图像的 docker 容器。 I want the container to have a root user called
admin
and also be able to create a database called experts_db
.我希望容器有一个名为
admin
的根用户,并且还能够创建一个名为experts_db
的数据库。 Here is my docker-compose.yaml
:这是我
docker-compose.yaml
:
version: '3.7'
services:
mongodb:
image: mongo:4.0.4
container_name: mongodbtest
restart: always
env_file: .env
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: 123456789
MONGO_INITDB_DATABASE: experts_db
ports:
- 27017:27017
volumes:
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
and here is my mongo-init.js
file:这是我的
mongo-init.js
文件:
use admin;
db.createUser(
{
user: "andrea",
pwd: "123456789",
roles: [
"userAdminAnyDatabase",
"dbAdminAnyDatabase",
"readWriteAnyDatabase"
]
}
);
Despite that, when I am in the mongo shell and try to get a list of all users, I get the following error:尽管如此,当我在 mongo shell 中并尝试获取所有用户的列表时,我收到以下错误:
use experts_db
switched to db experts_db
> db.getUsers()
2020-01-22T14:30:00.569+0000 E QUERY [js] Error: command usersInfo requires authentication :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1763:1
@(shell):1:1
Does anyone know why this happens?有谁知道为什么会这样? I am new to mongo and dont understand how I could fix this
我是 mongo 的新手,不明白如何解决这个问题
You need to remove the data/db/ volume inside your container then rerun and build again您需要删除容器内的数据/数据库/卷,然后重新运行并重新构建
rm -rf data/db/
docker-compose up --build
I have tested the JS script below and it works as expected:我已经测试了下面的 JS 脚本,它按预期工作:
db.createUser(
{
user: "aaa",
pwd: "123",
roles: [ { role: "readAnyDatabase", db: "admin" }, { role: "dbAdminAnyDatabase", db: "admin" }, { role: "userAdminAnyDatabase", db: "admin" }]
});
After the creation, you can hop on mongoDB shell and run db.getUsers() to make sure the user has been successfully created.创建后,您可以跳上 mongoDB shell 并运行 db.getUsers() 以确保用户已成功创建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.