[英]docker-compose mongodb fresh instance not working
Trying to bring docker-compose up with the following:尝试通过以下方式使 docker-compose 上升:
version: '3.1'
services:
mongo:
image: mongo
container_name: mongo-db
networks:
- mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_ROOT_USER}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ROOT_PASSWORD}
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
ports:
- 27017:27017
volumes:
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
mongo-express:
image: mongo-express:latest
container_name: mongo-express
restart: always
networks:
- mongo
depends_on:
- mongo
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_SERVER: mongo
ME_CONFIG_MONGODB_PORT: 27017
ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_ROOT_USER}
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_ROOT_PASSWORD}
The entry point js file as below:入口点js文件如下:
db.createUser(
{
user: "dba",
pwd: "dba",
roles: [
{
role: "readWrite",
db: "mydb"
}
]
}
);
The variables are defined in.env file as below:变量在 .env 文件中定义如下:
MONGO_ROOT_USER=root
MONGO_ROOT_PASSWORD=root
MONGO_INITDB_DATABASE=mydb
MONGOEXPRESS_LOGIN=dev
MONGOEXPRESS_PASSWORD=dev
When I login to mongo-express, I don't see the user or the db that is created by mongo-init.js Also, I can't login if I try to connect using:当我登录到 mongo-express 时,我看不到由 mongo-init.js 创建的用户或数据库此外,如果我尝试使用以下方式进行连接,我将无法登录:
docker exec -it mongo-db mongo --username dba
However, if I use the following I can connect but still don't see mydb when I run show dbs:但是,如果我使用以下内容,我可以连接,但在运行 show dbs 时仍然看不到 mydb:
docker exec -it mongo-db mongo --username root
What's happening here?这里发生了什么事?
Thanks in advance提前致谢
I think you need to change the env to:我认为您需要将环境更改为:
MONGO_INITDB_ROOT_USERNAME
MONGO_INITDB_ROOT_PASSWORD
These variables, used in conjunction, create a new user and set that user's password.这些变量一起使用,创建一个新用户并设置该用户的密码。 This user is created in the admin authentication database and given the role of root, which is a "superuser" role.该用户是在管理员身份验证数据库中创建的,并被赋予 root 角色,这是一个“超级用户”角色。
another note:另一个注意事项:
if you do not insert data with your JavaScript files, then no database is created.如果您不使用 JavaScript 文件插入数据,则不会创建数据库。
MONGO_INITDB_DATABASE MONGO_INITDB_DATABASE
This variable allows you to specify the name of a database to be used for creation scripts in /docker-entrypoint-initdb.d/*.js (see Initializing a fresh instance below).此变量允许您指定用于在 /docker-entrypoint-initdb.d/*.js 中创建脚本的数据库名称(请参阅下面的初始化新实例)。 MongoDB is fundamentally designed for "create on first use", so if you do not insert data with your JavaScript files, then no database is created. MongoDB 基本上是为“首次使用创建”而设计的,因此如果您不使用 JavaScript 文件插入数据,则不会创建数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.