Mongodb - Difference between running "mongo" and "mongod" databases

Solution 1:

I think there is some confusion here.

mongod is the "Mongo Daemon" it's basically the host process for the database. When you start mongod you're basically saying "start the MongoDB process and run it in the background". mongod has several default parameters, such as storing data in /data/db and running on port 27017.

mongo is the command-line shell that connects to a specific instance of mongod. When you run mongo with no parameters it defaults to connecting to the localhost on port 27017. If you run mongo against an invalid machine:port combination then it will fail to connect (and tell you as much).

Ideally, when doing anything other than just "playing around", you'll use the Command Line Parameters for starting mongod. By the same measure you should start the mongo shell with explicit instructions.

Based on your description, I think you may be encountering an issue regarding the use of default databases. Try starting mongo with the following (where dbname is your database name)

./mongo localhost:27017/dbname

Solution 2:

Yes, this might be a naive answer to this question but I am putting it forward so people can understand it easily!

Mongod:

mongod

(Short for Mongo Daemon) is a background process used by MongoDB server to get things done. This process is responsible for managing the whole MongoDB server tasks such as accepting requests, responding to users, managing memory requirement of MongoDB server operations and other things essential for MongoDB Server to run.

TLDR; Basically it is the MongoDB server

Mongo:

Mongo

on the other hand, is an interactive JavaScript shell interface to MongoDB, which provides a powerful interface for system administrators as well as a way for developers to test queries and operations directly with the database. mongo also provides a fully functional JavaScript environment for use with a MongoDB

TLDR; Basically I think it as mongodb client which can be used as a shell to get access to MongoDB database server run my mongod instances

Solution 3:

MongoD(Daemon process). In simple "Hello world" words - You won't communicate Mongod directly. Instead, you use DB client:

  • Mongo Shell (run mongo) - In an interactive JavaScript interface (-or- run mongosh the new MongoDB Shell).
  • Compass - Graphical view of MongoDB (Or studio 3t and other GUI apps).
  • Drivers (Like node -or- JAVA drivers).

And you find more tools like this.

In your case, you want to use Mongo Shell client (mongo command).

Visual

A great way to see "the client" - "in action" is to open two terminals (or more) side by side.

Run mongod (mongo command will not work without MongoDB server install and running), then run mongo:

enter image description here

Output (1 connection: application mongo shell} enter image description here

Next, run some CRUD Operations and see the messages under mongod.

Same idea if you connect Compass client: enter image description here

Manage mongodb processes docs (Change data directory, Port and so on): https://docs.mongodb.com/manual/tutorial/manage-mongodb-processes/