I just installed MongoDB 4.4 on Ubuntu 20.04. Now, I want one user with a password to have full access (create database, write to it, delete it, etc.) over TCP port 27017. How can I do this?
Asked
Active
Viewed 100 times
1
-
First, you need to enable the security on the database server. Basic concepts are the Authentication and Authorization - these will allow to create users with id/password _and_ the access control (specify what the users can do and not) – prasad_ Aug 01 '22 at 02:51
1 Answers
0
First, I changed 127.0.0.1
to 0.0.0.0
in /etc/mongodb.conf
.
Then, I created a new user:
$ mongo
use admin
db.createUser({user:"admin",pwd:"foo",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
Then, added these lines to /etc/mongod.conf
:
security:
authorization: enabled
Then, restarted it:
$ sudo service mongod restart
Finally, I was able to connect to it both from the server and remotely (after adding TCP port 27017 to the list of inbound rules of my AWS EC2 security group):
$ mongo localhost:27017 -u admin -p foo

yegor256
- 102,010
- 123
- 446
- 597