DEV Community

Memel Meless
Memel Meless

Posted on • Updated on

How to run a MongoDB instance on a specific port and activate authentification?

Sometimes you need to work on different projects that need MongoDB in differents ports /instances, We have a lot of situations for which that happens but I will not list them here. I faced that situation and I diced to share my experience on it with you.

In this tutorial you will learn how to run a specific instance of MongoDB :

  • Store the database files in a specific directory
  • Specify a custom port
  • Activate authentification by username and password
  • How to connect from mongo client directly from the terminal

Requirement

  • Have MongoDB 3.4 or higher installed

Create the directories to persist your data

mkdir /home/memel/data
mkdir /home/memel/data/log
Enter fullscreen mode Exit fullscreen mode

Create a Configuration file

Everything can be done in the config file.
create a file anywhere you want and keep in mind the full path of that file.

Name it externaldb.conf :

touch /home/memel/externaldb.conf
Enter fullscreen mode Exit fullscreen mode

Put content below and adapt it to your need :

systemLog:
   destination: file
# you can put the corresponding path for the log
   path: /home/memel/data/external/log/mongod.log
   logAppend: true
storage:
   journal:
      enabled: true
# you can put the corresponding path to store the DB
   dbPath: /home/memel/data/external
net:
   bindIp: 127.0.0.1
# put the port that you want
   port: 27020
setParameter:
   enableLocalhostAuthBypass: false
Enter fullscreen mode Exit fullscreen mode

From your terminal run the following to lunch your instance :

mongod --config "/home/memel/externaldb.conf"
Enter fullscreen mode Exit fullscreen mode

To check if everything is ok, from you terminal run :

mongo --port 27020
Enter fullscreen mode Exit fullscreen mode

Create a secured account

Select the admin database

use admin
Enter fullscreen mode Exit fullscreen mode

Create a server admin user

db.createUser(
{
user: "sa",
pwd: "your password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
})
Enter fullscreen mode Exit fullscreen mode

That user account will manage all the server, I mean all the databases.
you can create a user only for a specific database.

Create a database admin user

Creating a specific account myAppUser that will acceed only your app database myAppDB.

db.createUser(
{
user: "myAppUser",
pwd: "your password",
roles: [ { role: "readWrite", db: "myAppDB" }]
})
Enter fullscreen mode Exit fullscreen mode

You should see a success message, close your terminal.

Connect from mongo client

Now open a new terminal and test if the sa account works, run the next command line

mongo --port 27020  -u sa -p --authenticationDatabase admin
Enter fullscreen mode Exit fullscreen mode

Activate full authentification

Activate the security allow only access by password
you just need to set enableLocalhostAuthBypass to true
your config file should look like this

systemLog:
   destination: file
# you can put the corresponding path for the log
   path: /home/memel/data/external/log/mongod.log
   logAppend: true
storage:
   journal:
      enabled: true
# you can put the corresponding path to store the DB
   dbPath: /home/memel/data/external
net:
   bindIp: 127.0.0.1
# put the port that you want
   port: 27020
setParameter:
   enableLocalhostAuthBypass: true
Enter fullscreen mode Exit fullscreen mode

Enjoy !!!

Top comments (0)