Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if mongo db is running on Mac?

I had installed MongoDB few days back on my Mac and am not sure how I installed, but now how do I check if MongoDB is up and running in my system?

like image 409
nshetty Avatar asked Jul 22 '15 10:07

nshetty


People also ask

How do I start MongoDB on Mac?

Start the MongoDB service on macOSIn the terminal window, start the service by typing mongod . When you want to stop the MongoDB server, it's the same as Homebrew. Press CTRL+C.


2 Answers

Quick Solution

Run the following in your Terminal:

ps -ef | grep mongod | grep -v grep | wc -l | tr -d ' ' 

This will get you the number of MongoDB processes running, thus if it is other than 0, then you have MongoDB running on your system.

Step-by-Step

  • The ps -ef | grep mongod part return all the running processes, that have any relation to the supplied string, i.e. mongod, e.g. have the string in the executable path, have the string in the username, etc.

  • When you run the previous command, the grep mongod also becomes a process containing the string mongod in the COMMAND column of ps output, so it will also appear in the output. For that reason you need to eliminate it by piping grep -v grep, which filters all the lines from the input that contain the string grep.

  • So now you have all possible lines that contain string mongod and are not the instances of grep. What to do? Count them, and do that with wc -l.

  • wc -l output contains additional formatting, i.e. spaces, so just for the sake of the beauty, run tr -d ' ' to remove the redundant spaces.

As a result you will get a single number, representing the number of processes you grep'ed for.

like image 80
bagrat Avatar answered Oct 12 '22 05:10

bagrat


The answers combining ps and grep should always get you what you need. However, if you have a standard installation that comes with the mongo shell, an easier to remember method is to start the mongo shell

mongo 

The shell will give you status of mongodb.

like image 26
lastoneisbearfood Avatar answered Oct 12 '22 06:10

lastoneisbearfood