Skip to content

Instantly share code, notes, and snippets.

@toan2406
Last active October 20, 2016 09:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save toan2406/32ec2f82969c3844f4ef3dbcbdfacdaf to your computer and use it in GitHub Desktop.
Save toan2406/32ec2f82969c3844f4ef3dbcbdfacdaf to your computer and use it in GitHub Desktop.
MongoDB authentication setting
docker run -v /datadir:/data/db --name mongo -p 27017:27017 -d mongo --auth
docker exec -it mongo mongo
> use admin
> db.system.users.find()
> db.createUser({ user: 'admin', pwd: 'admin', roles: [ { role: "root", db: "admin" } ] });
> use some-db
> db.createUser({ user: 'user', pwd: 'user', roles: [ "readWrite" ] });
> exit
# Authenticate
> use some-db
> db.auth('user', 'user')
# Or
mongo -u user -p user --authenticationDatabase some-db
# Connect URI
# Note: /database: The name of the database to authenticate if the connection string includes authentication credentials in the form of username:password@. If /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database.
mongodb://user:user@localhost:27017/some-db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment