Last active
October 20, 2016 09:57
-
-
Save toan2406/32ec2f82969c3844f4ef3dbcbdfacdaf to your computer and use it in GitHub Desktop.
MongoDB authentication setting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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