Why does the mongo shell work without mongod server being explicitly run? - mongodb

Running MongoDB Shell v-3.2.8
I've noticed that articles and tutorials always mention to run the mongod server before running the mongo shell.
However, when I skip the first step and simply type mongo into my terminal, the mongo shell works without any errors / interruptions.
MacBook:Desktop user$ mongo
MongoDB shell version: 3.2.8
connecting to: test
Why does this work? Does mongo call mongod?

The mongod is being ran as a service or daemon, which means that there is always a mongod process running listening to a port. I use ubuntu, and when I install mongodb through the package manager, it immediately starts up a mongod process and begins listening on the standard port.
Running mongo is simply a small utility that attempts to connect to the localhost at the standard ip. The data reading, writing, and querying is done by the mongod process while mongo is a small program that sends the the commands to mongod.
If mongod wasn't running, you would see an error stating "Unable to connect to mongodb server"

I noticed the same. I think mongoose is doing some smart things there, which I'm not sure how it works. But I have noticed such things before, like mongoose will automatically add an "s" to your database's name when you declare it, which is a very thoughtful act =)).

I encountered the same thing and found that in the background services if MongoDB server is running, the mongo shell will work without any error. If we stop the service, the shell will throw an error.

Related

What's difference between using "sudo service mongod start" and "mongod"?

I'm using Ubuntu 16.04, and I can't understand what's difference between using
sudo service mongod start
and
mongod
In mongodb official documentation here
said that to start mongodb just use sudo service mongod start, and its log stores in /var/log/mongodb. However, I try to run mongodb using mongod this way, log shows on terminal, and after I turn off the terminal, I can not find the log file.
It is confused.
sudo - Runs the command as root.
service - Manages the following program as a daemon (background process).
mongod - Obviously the MongoDB program in question.
start - A command that tells service what to do with the program in question.
Together, we get "I want to start mongod as a background process, and I want to run it as root so it has permission to do the things it needs to do". Running mongod by itself, however, runs the program in an ordinary fashion, i.e. as a foreground process. Typically you want to run it as a background process so that you're free to do other things, e.g. connecting to the database via shell access.
This is pretty simplified, but it should explain what you actually need to know at this point in time.

How to login to mongodb console in server

I'm trying to create a new collection in an already existing mongodb database on an ubuntu server. I tried running the command mongod but it says the mongod not found
Make sure your mongo server is running:
you can start using command sudo service mongod restart . Once it started just type mongo , It will take you in mongo console.
Try mongo.
mongod is the one that starts the server. mongo is the client that connects to the server.
If your environment variable is set correctly, it should work.
If it still says command not found, try /usr/bin/mongo
You can find more info here: https://docs.mongodb.com/manual/mongo/

Mongodb is not running

I have installed mongodb in my computer. But whenever I try to launch it, it's not running. I have entered command , as "mongo" to start the mongodb, which shows following resuts, but the application is not opening.
Any one having any idea how can I fix this? Please share it. Thanx!
Environment: Ubuntu 16.04
Those are warnings that appear at the first mongo start. Don't worry to much about them.
Since the process is still running and you got the small > character there, that means you successfully connected to the MongoDB server.
mongod is the MongoDB server (you can check if it's running by running ps aux | grep mongod) and the mongo cli tool is a cli client connecting to the server. Here you can run commands.
For example, create your first document:
use myDatabase
db.people.insert({ name: "Rhea" })
db.people.find()
In conclusion, both the MongoDB server and client are running but you got a few warnings that only appear the first time when you run mongo.
Initially in one terminal write mongod.
Then on another terminal : mongo .
The first one will start the mongo server.
The second one will start the interpreter.
You can try various commands in the interpreter.

What are the practical differences between mongo and mongod?

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.
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.
With mongodyou are starting the server on your machine. As you stated correctly, mongo is your client, your user interface, if you want to. Per default it connects to your local instance of MongoDB. If you start your client without a server instance running, you would have to 'tell' it, where it should connect to (e.g. a remote instance):
http://docs.mongodb.org/manual/reference/program/mongo/

Can't connect to local (running) mongo using 'meteor mongo'

I'm playing with meteor, Installed it on Ubuntu 14.10 & followed the todos tutorial. The Meteor run log:
$ meteor run
[[[[[ ~/bckd/homedir/learn/meteor/mycoolapp ]]]]]
=> Started proxy.
=> Started MongoDB.
=> Started your app.
=> App running at: http://localhost:3000/
When I try to connect to mongo via another terminal, I get:
$ meteor mongo
mongo: Meteor isn't running a local MongoDB server.
This command only works while Meteor is running your application locally. Start your
application first. (This error will also occur if you asked Meteor to use a different MongoDB
server with $MONGO_URL when you ran your application.)
If you're trying to connect to the database of an app you deployed with `meteor deploy`,
specify your site's name with this command.
Trying to work around this problem I looked at the mongo command, and soon enough I figured I can connect using:
$ mongo localhost:3001
So great, that's working and it seems that I am able to use that for the time being.
But... why is the official way: meteor mongo not working in my setup, and can I do anything to fix it?
Edit 5/6/2015
After creating another account on my ubuntu, logging in and creating a new app, I run it and managed to connect to the mongo instance as intended with the meteor mongo command (without sudo). I thought it could be a result of a difference in my environment variables so I compared the two (dumping both environments with the env command. There were some extra bash variables in my primary account so one by one I've unset them until I reached two identical environments (with the exceptions of home directory values, user names, auto-generated values for gnome session tokens and such). Despite those changes, the problem consists. Another wild guess of mine was that the ~/.meteor folder has gone evil on me. So, I've removed it and reinstalled meteor. That didn't fix it either. Whatever the problem is, it's pretty stubborn.
Try:
sudo meteor mongo
it worked for me on a Vagrant box :)
You need to be the same user as the user who started Meteor.
Also you need to connect from within the application directory.
Can you connect to the database with the native MongoDB client?
Try running sudo meteor mongo - maybe the Meteor/Node process is running as root.
Looks like it's your environment variable problem.
export MONGO_URL=''
meteor mongo
Did you create the meteor app initially :
meteor create
I was having same issue on Ubuntu and found that i missed the create part as I copied the code from windows machine.
In my case(Windows 10),I had set MONGO_URL='remote url'.
In this case when you type 'meteor mongo' meteor will try to connect to the local mongodb server,but we are pointing to remote mogodb.
To point to local mongodb:
1) befor starting the app type 'set MONOGO_URL=' command in terminal.
2) now start the meteor app
3) now open a new terminal and try 'meteor mongo'
This worked for me with the same problem.
I had two terminals open: one running with meteor command, the other terminal I tried meteor mongo and received the very same message as stated in the question.
mongo: Meteor isn't running a local MongoDB server.
This command only works while Meteor is running your application locally. Start
your application first with 'meteor' and then run this command in a new
terminal. This error will also occur if you asked Meteor to use a different
MongoDB server with $MONGO_URL when you ran your application.
If you're trying to connect to the database of an app you deployed with
'meteor deploy', specify your site's name as an argument to this command.
I tried the sudo command, which did not work as well.
Here is what worked for me.
Goto running meteor server terminal and ctrl-c (shut down server).
In meteor server terminal type: meteor mongo.
Note: In my case the command worked and the terminal entered into mongo.
Type exit to exit mongo.
Type meteor to spool back up meteor server.
Goto meteor mongo terminal and type: meteor mongo.
Note: For me, Meteor was running in the other terminal and meteor mongo command did open meteor mongo for me without error.
Perhaps the something in Meteor needs to reboot which does not occur without shutting down meteor server first?
check if there is a running mongo process, and kill it manually:
ps -aux | grep mongo
sudo kill [pid]
..then restart your meteor app.
--> I think this specific situation exists only when starting meteor on a custom port (not tested, but pretty obvious)