$ cd ~
$ mkdir mongodb
$ docker run -d --name mongodb --restart=always -v ~/mongodb/data/db:/data/db -p 27017:27017 mongo:latest --auth
$ docker exec -it mongodb mongo admin
> db.createUser({
user: "admin",
pwd: "<ADMIN_PASSWORD>",
roles: [ { role: "root", db: "admin" } ]
})
> db.getUsers()
> db.auth("admin","<ADMIN_PASSWORD>")
> use <NEW_DATABASE>
> db.createUser({
user: "<NEW_USERNAME>",
pwd: "<NEW_PASSWORD>",
roles: [ { role: "dbOwner", db: "<NEW_DATABASE>" } ]
})
> db.getUsers()
> db.auth("<NEW_USERNAME>","<NEW_PASSWORD>")
> db.dropUser("<NEW_USERNAME>")
> exit