Running mongo on docker container
docker run -d \
-e MONGO_INITDB_ROOT_USERNAME=admin \ # creates user
-e MONGO_INITDB_ROOT_PASSWORD=changeme \ # creates passwd
-p 27017:27017 \
--name my-mongo-container \
mongo --auth # starts mongodb with authentication enabled
Create new database and its users:-
test> show dbs
admin 100.00 KiB
config 12.00 KiB
local 72.00 KiB
test> use schooldb
switched to db schooldb
schooldb> db.createUser({user:"user1",pwd:"changeme123",roles:["readWrite"]})
{ ok: 1 }
admin> db.system.users.find()
docker exec -it my-mongo-container mongosh -u user1 -p changeme123 --authenticationDatabase schooldb
db.students.insert({ name: "john doe" })