29

I am trying to test MongoDB and I have it all downloaded and moved into the root folder. I can navigate to the folder that holds the mongod, but when I try to run it by typing "mongod" into my terminal, I get a message that says:

"mongod: command not found"

Cœur
  • 34,719
  • 24
  • 185
  • 251
MissElizabeth
  • 489
  • 1
  • 5
  • 11

11 Answers11

29

Both answers above are correct. You can either specify the path in one of the following files: .profile, .bashrc, or .bash_profile

export PATH="$PATH:/usr/local/mongodb/bin"

then call the daemon or the shell directly

mongod
mongo

Or for the commands not in the $PATH, use ./mongo or ./mongod from the directory containing these files. This solution can be verbose has you will have to eventually append the whole path when calling these commands from another directory.

/usr/local/mongodb/bin/mongod
or
/usr/local/mongodb/bin$ ./mongod 
Laurent Jacquot
  • 561
  • 5
  • 7
  • 1
    NOTE: if you update your Mac to Sierra you will need to re-export your paths as shown above. –  Dec 28 '16 at 02:14
  • In the latest hombrew version (Homebrew 3.3.12) running in monterey, mongo is installed in `/opt/homebrew/opt/mongodb-community@x.x/` rather than `/usr/local/mongodb`. – rvazquezglez Jan 30 '22 at 14:25
13

"Mongod" isn't a stand-alone command. You need to run the command like this:

./mongodb/bin/mongod

I used this webpage to help me answer this question.

sɐunıɔןɐqɐp
  • 2,877
  • 15
  • 33
  • 38
MissElizabeth
  • 489
  • 1
  • 5
  • 11
11

For example, install 64bit MongoDB 2.6.12 on macOS Catalina. (for newest versions you may go to https://www.mongodb.com/download-center/community for your platform).

  1. Download, extract and move:

    wget http://downloads.mongodb.org/osx/mongodb-osx-x86_64-2.6.12.tgz

    tar xzf mongodb-osx-x86_64-2.6.12.tgz

    mv mongodb-osx-x86_64-2.6.12/ /usr/local/mongodb/

  2. Add to file ~/.zshrc this:

    export PATH="$PATH:/usr/local/mongodb/bin"

    PS: .bash_profile or .profile not worked in my case

  3. Reload terminal (or close, open it):

    source ~/.zshrc

  4. Make directory for data and set rights:

    mkdir -p ~/data/db

    chown -R mongodb.mongodb ~/data/db

  5. Run MongoDB:

    mongod --dbpath ~/data/db

shilovk
  • 9,510
  • 16
  • 66
  • 68
8

This worked for me:

  1. brew tap mongodb/brew
  2. brew install mongodb-community@4.2
  3. mongod

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

tryingToLearn
  • 9,007
  • 10
  • 70
  • 99
7

You need to add the name of the folder that contains the command mongod into your PATH so your shell knows where to find it.

So, if mongod is in /usr/bin/freddyfrog, you would edit ~/.profile and find the line that says PATH= and edit it to look like this:

export PATH=${PATH}:/usr/bin/freddyfrog

Then login again to make it take effect.

Mark Setchell
  • 169,892
  • 24
  • 238
  • 370
5

3 steps:

Step 1:

export PATH="$PATH:/usr/local/mongodb/bin"

OR

export PATH="$PATH:/usr/local/opt/mongodb@3.2/bin"

(replace version number with your local version)

The first step will allow you to run the command, but will get you another error: "/data/db does not exit" so you have to

Step 2 :

sudo mkdir -p /data/db

Now /data/db is read only, but it has to be writable also so

Step 3 :

sudo chown -R USERNAME /data/db 
Lee Dykes
  • 877
  • 7
  • 16
Haddad
  • 97
  • 2
  • 9
4

I was trying to install a previous version (3.6) using latest documentation (4.2 is already released). So, they now call it mongodb-community@3.6.

In order to update PATH for such setup, the statement should be

export PATH="$PATH:/usr/local/opt/mongodb-community@3.6/bin";

I got hint from @retroGiant 's answer

Kushagr Arora
  • 1,678
  • 1
  • 7
  • 11
  • My Indian bhrata, this worked. Thanks. Others - make sure you get the path right on Mac OS X Catalina. Other comments are linking to wrong path. – iCyborg Apr 05 '21 at 16:11
3

run this command, it works:

brew services start mongodb-community@4.0

tryingToLearn
  • 9,007
  • 10
  • 70
  • 99
Muhammad Amir
  • 495
  • 7
  • 18
2

In root directory

sudo mkdir data
cd data 
mkdir db

then

sudo chown -R yourUsername /data/ 

copy path of your mongodb/bin downloaded folder (I suggest you put it in home folder not root dir)

enter image description here

in terminal

export PATH="paste the link here :$PATH"

now it should work but if not

In case you are using different Unix shell and trying to execute mongod within visual studio code( for example ), make sure to read the documentation to link PATH.

For example, if you are using zsh create .zprofile in your home directory.

touch .zprofile

copy your previously made PATH into .zprofile

enter image description here

enter image description here

Now everything should work as expected.

Ahmed Younes
  • 804
  • 11
  • 15
2

I have installed mongodb-community@3.2, was facing the same issue. I followed below steps.

  1. open bash profile in any editor (you can also try - vi ~/.bash_profile)

  2. write this export PATH="$PATH:/usr/local/opt/mongodb-community@3.2/bin" & save.

  3. do this . source ~/.bash_profile

Dharman
  • 26,923
  • 21
  • 73
  • 125
-2

This worked for me: I just Updated the xcode application.