Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the practical differences between mongo and mongod?

Tags:

mongodb

Just finished installing mongodb, however, I have not been able to make complete sense of the difference between mongo vs mongod commands. Yes, I do understand that

mongod is the primary daemon process for the MongoDB system

and that

mongo is an interactive JavaScript shell interface to MongoDB

but what does that mean practically? I presume every time I want to use mongodb, I need to run mongod first. But then why am I able to run mongo without having started mongod first? Does mongo run mongod in the background automatically? Secondly, if I run mongod it eventually ends with something like

waiting for connections on port 27017

but then I can't type anything after that. Again, I presume that mongodb has been started in the background so I can safely close the terminal. But if I close the terminal by mistake (on a mac), how can I get that back up on the terminal? Also, how can I terminate the service for it to stop listening to the port?

So as you can I see, I have a bunch of simple questions... but most are related to the practical uses of when and when not to mongo or mongod. I can't seem to find anything online that will help me explain these in the practical sense.

like image 412
Grateful Avatar asked Aug 13 '15 07:08

Grateful


1 Answers

As with most database software, Mongo is split into a server and client. The server is the main database component which stores and manages data. Clients come in various flavours and connect to the server to insert or query data.

mongod is the server portion. You start it, it runs, end of story.
mongo is a default command line client. You start it, you connect to a server, you enter commands, you quit it.

You have to run mongod first, otherwise you have no database to interact with. Simply running mongod on a command line will make it the frontmost running application, and it does not offer any interactivity. So yes, you'll just see something like "Waiting for connections...", and nothing more. You typically don't run mongod like that on the command line. You most typically create an init.d script or launchd file or however you manage your daemons, and have the system start it automatically at system boot time.

If you want to launch mongod as a one-off thing without having it permanently running on your systems, put it in the background:

$ mongod &

The & puts it in the background and you can continue to use your command line. You can see it and kill it like this:

~ deceze$ mongod &
[1] 1065
~ deceze$ jobs
[1]+  Running                 mongod &
~ deceze$ kill %1
[1]+  Done                    mongod

Once your server is running, start mongo, connect to the server, and interact with it. If you try to run mongo without a running server, it should complain that it's not able to connect:

~ deceze$ mongo
MongoDB shell version: 3.0.2
connecting to: test
2015-08-13T09:36:13.518+0200 W NETWORK  Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2015-08-13T09:36:13.521+0200 E QUERY    Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
    at connect (src/mongo/shell/mongo.js:179:14)
    at (connect):1:6 at src/mongo/shell/mongo.js:179
exception: connect failed

If your mongo shell does connect to something, you might unknowingly have another instance of mongod running on your system.

like image 53
deceze Avatar answered Sep 27 '22 23:09

deceze