Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Problem: I installed MongoDB using the official method but now I need to secure it via username/password
- ####Environment: MongoDB Enterprise 3.4 on Ubuntu 16.04
- **Solution** (there is more than 1 way, but this is the one I prefer):
- *Step 1 - Create username/password:*
- Start mongo shell
- mongo
- Create a new user in admin database
- use admin
- db.createUser(
- {
- user: "myUserAdmin",
- pwd: "abc123",
- roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
- }
- )
- Stop the DB service
- sudo service mongod stop
- sudo vim /etc/mongod.conf
- In mongod.conf file, uncomment this line, so it looks like this:
- security:
- authorization: 'enabled'
- Start the service
- sudo service mongod start
- Start the shell and run a query, you should see an error:
- MongoDB Enterprise > show databases
- ...
- listDatabases failed:{
- "ok" : 0,
- "errmsg" : "not authorized on admin to execute
- command { listDatabases: 1.0 }",
- ...
- Open the shell, this time with the correct username and password:
- mongo -u myUserAdmin --authenticationDatabase=admin -p abc123
- It should work this time
- MongoDB Enterprise > show databases
- admin 0.000GB
- local 0.000GB
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement