Minimal Docker-Compose Example
mongodb:
image: mongo
volumes:
- ./mongodb-data:/data/db
ports:
- "27017:27017"
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=passwordLocal Management
In order to manage mongo instances you can use Mongo Shell for interacting with databases and the Mongo DB Tools package for management operations like dumping and restoring data.
Initialization with Docker
The above config creates a user in the admin DB which we can then use to set up more databases and users.
Creating a Database
To create a new database and user in MongoDB, you can use the following commands:
Create a new database and grant a new user permissions on it:
- Connect to the admin database using mongosh:
mongosh localhost:27017/admin -u root -p password- Run
use mynewdatabaseto create a new db calledmynewdatabase - Now the local
dbvariable will refer to the db you just created and we can create the user:
db.createUser({
user: "<new_user_name>",
pwd: "<password>",
roles: ["readWrite", "dbOwner"]
})