Mongo DB set up with auth and port blocking.
Download Mongo msi https://www.mongodb.com/try/download/community
Download Compass msi https://www.mongodb.com/try/download/atlascli
Download Shell msi https://www.mongodb.com/try/download/shell
Win+R -> cmd (We will call it Terminal-1)
We will launch Mongo database without authorization first to create an admin user and then after switching to it create a regular user with the needed permissions
// You might need to replace 6.0 with the version you installed cd C:\Program Files\MongoDB\Server\6.0\bin mongod
Win+R -> cmd (We will call it Terminal-2)
We are opening another terminal window to connect to the database we just launched
//Set up your own value istead of ADMINPASS mongosh use admin db.createUser( { user: "adminUser", pwd: "ADMINPASS", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } ) // get the responce {ok:1} exit
Now we will launch our database again, now with authentication to create a regular user.
In Terminal-1 press ctrl+c (might take some time or press ctrl+c again)
mongod --auth --dbpath=C:/data/db
//Set up your own values istead of DBNAME, USER, USERPASS mongosh use admin db.auth('adminUser', 'yourAdminPassword') // get the responce {ok:1} mongosh use DBNAME db.createUser( { user: "USER", pwd: "USERPASS", roles: [ { role: "readWrite", db: "DBNAME" } ] } ) db.auth('adminUser', 'ADMINPASS') db.auth('bot01', 'USERPASS') // get the responce {ok:1}
Open up Compass and in Advanced choose Username/Password, enter your info, and proceed.
You are all set, now check out the info about port blocking below.
(This is a necessary step to block the port if you do not plan to connect to this db from another machine )
To close port 27017 used by MongoDB, you can do so through the Windows Firewall. Here's how:
- Open Windows Firewall:
- Create a New Rule:
- Click on Advanced settings.
- In the left pane, select Inbound Rules.
- In the right pane, click New Rule.
- Configure the Rule: