July 17

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

Install all 3

Create folders C:\\data\db

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)

In Terminal-1

mongod --auth --dbpath=C:/data/db

In Terminal-2

//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.

PORT BLOCKING

(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:

  1. Open Windows Firewall:
    • Press Win + R, type firewall.cpl, and press Enter.
  2. Create a New Rule:
    • Click on Advanced settings.
    • In the left pane, select Inbound Rules.
    • In the right pane, click New Rule.
  3. Configure the Rule:
    • Select Port and click Next.
    • Choose TCP and enter 27017 in the Specific local ports field.
    • Select Block the connection and click Next.
    • Ensure all profiles are checked (Domain, Private, Public) and click Next.
    • Name the rule (e.g., "Block MongoDB Port") and click Finish.

This will block all incoming connections on port 27017.

Enjoy.