What is Mongo Daemon?

Chandan Gr
4 min readMay 25, 2021

In this blog, let’s try to understand …
What is a daemon?
How to create a daemon server?
How to connect to that daemon server?…..

All databases, collections( or tables), documents(or records) are stored in the hard disk. Adding/deleting/reading records .. all these operations will be done on the hard disk itself.

Let’s see the problem from this…

Problem Statement:

How do we access those records from the disk in our code (node.js, python, etc)?
How to query in our code on the tables which is present on the disk?
Should we need to write the logic for finding, updating, deleting queries by ourselves?

Solution:

Mongo Daemon (or MongoD).

How the solution works:

  • mongoD (or mongo daemon) is an instance that will run on
    port 27017 (as default ) which points to the hard disk memory (
    /var/lib/mongodb by default). So when we create a table, the request is sent to mongoD, the mongoD then automatically creates a table in /var/lib/mongodb location as shown below. If we want to change the location, please read further.
  • For any DB query from the code, the request is sent to the Mongo daemon. It then automatically executes the query on the tables which are stored in the disk and finally sends the result back to the code.

Recap:
Any mongo query from code -> goes to mongoD (on port 27017 by default)
-> it executes the query on data which is on hard disk -> returns the result to the code.

PS: mongoD points to /var/lib/mongodb path by default where the database will be stored. We can change the mongoD to point to a different directory and reads/writes will happen in that directory.

Set up MongoD:

mongod

when you type the above command in a terminal, it creates a mongo daemon instance on port 27017 (by default) and it will be pointing to the directory /var/lib/mongodb (by default) where all the databases are stored/created/read/deleted/updated.

mongod --dbpath /home/db

Now you want to change the dbpath to a different directory:
To change the path: use — dbpath
Type the above command in a terminal, it creates a mongo daemon instance on port 27017 (by default) and it will be pointing to the directory /home/db.
PS: Make sure the created directory should have proper permissions.

mongod --dbpath /home/db --port 27019

To change port: use — port.
Type the above command in a terminal…it creates a mongo daemon instance on port 27019 and it will be pointing to the directory /home/db.

Setting up Mongo daemon in local:

mkdir /home/db
chmod 777 /home/db
(new terminal(A)): mongod --dbpath /home/db --port 27019
(new terminal(B)): mongo --port 27019

1st command creates a directory at /home/db.
2nd command creates to read/write/execute permission to the owner, group, and others to that directory.
3rd command creates a new daemon instance which will run on port 27019.

options: { net: { port: 27019 }, storage: { dbPath: “/home/db” }
this line in the logs, tells the daemon will be started on port 27019 and the dbpath will be set to “/home/db”.

  • waiting for connections on port 27019
    This line tells it is waiting to get connected, usually, we connect to this daemon in the code or we can manually connect to the daemon using the shell.

4th command must be typed in new terminal B: It creates a new connection to the daemon server running on port 27019. If the --port is not provided..it will try to connect to the 27017 port by default.

Once the connection is successful (in terminal A), a new log will appear saying a new connection has been established.

Now once the connection has been established you will be able to see the log in the terminal A.

--

--