43

I'm using Mac Lion. I was using mongodb version 1.4. I wanted to upgrade to 1.8.5 and I followed http://shiftcommathree.com/articles/how-to-install-mongodb-on-os-x step wise replacing each mongodb-osx-x86_64-1.4.0 by mongodb-osx-x86_64-1.8.5. Everything goes smooth. I tried:

mongod 

it's ok. I can access localhost:28017 but,

mongo

shows command not found

How can I fix this?

Makis Milas
  • 147
  • 1
  • 1
  • 13
Ajay
  • 4,134
  • 3
  • 20
  • 19

12 Answers12

74

Starting from Mongodb version 6.0 mongo was replaced by mongosh

phoenixstudio
  • 1,776
  • 1
  • 14
  • 19
47

Editing because this answer is now outdated: See https://stackoverflow.com/a/74240679/683201

Original answer: You need to add the path to "mongo" to your terminal shell.

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

Did you do the last step with paths.d? If so, try restarting your terminals.

Do you have a good reason for using 1.8.5? The current stable is 2.0.4, and it has many useful upgrades from 1.8.x

Eve Freeman
  • 32,467
  • 4
  • 86
  • 101
  • i add to path doing sudo sh -c 'echo "/usr/local/mongodb/bin" > /etc/paths.d/mongodb' I found the problem though. Another version was not deleted from Cellar. i found it after i searched sudo find / -name mongod and remove it... then follow all the process . I had backup for 1.8.5 so tried that... will install 2.0.4 Anyway thanks.. – Ajay Apr 18 '12 at 08:32
  • We have to run it in each terminal window, we intend to run mongo – Saif Apr 21 '17 at 07:11
  • 1
    @Saif you can add it to your `.bash_profile` or equivalent to make it run automatically. – Eve Freeman Feb 14 '19 at 22:48
  • 22
    I installed mongodb community 4.0 on Mac with Homebrew and the correct command in this case is `export PATH=$PATH:/usr/local/opt/mongodb-community@4.0/bin` – Dónal Flanagan Feb 13 '20 at 08:30
  • what about when mongo resides in a docker container – rdmtinez Jan 12 '21 at 10:04
  • For people who don't bother scrolling down in 2022, try `mongosh` instead of `mongo` before editing your path. – R891 Dec 19 '22 at 23:06
14

For readers in 2021:

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

enter image description here

NeoZoom.lua
  • 2,269
  • 4
  • 30
  • 64
  • Your answer pointed me in the right direction but this was the 2022 path for me: `export PATH="$PATH:/opt/homebrew/Cellar/mongodb-community@4.2/4.2.19/bin"` – SuperCodeBrah May 11 '22 at 04:12
  • 3
    I installed mongodb 6.0 community edition, and there is not file "mongo" in this bin directory. So we still will get command not found even if we add this directory to the path. – dreameral Aug 24 '22 at 16:54
  • 1
    @dreameral, did you find any solution yet? – lone wolf Sep 10 '22 at 18:16
  • 9
    @lonewolf Yes, for mongodb 6.0 the file is named "mongosh", so it's no longer "mongo" like it was in previous versions of mongo. – dreameral Sep 10 '22 at 22:04
10

You'll have to add the location of the Mongo binary to PATH.

Follow the steps below in order to make the PATH variable permanent:

  1. Open Terminal and navigate to your user directory.
  2. Run touch ~/.bash_profile and then open ~/.bash_profile.
  3. In TextEdit, add export PATH="<mongo-directory>/bin:$PATH" (Keep the quote marks - related to white spaces).
  4. Save the .bash_profile file and Quit (Command + Q) Text Edit.
  5. Run source ~/.bash_profile.
  6. Run echo $PATH and check if the you see that the Mongo binary was added.

(*) Notice that the PATH variable is now available only for the current terminal and not to processes that were already started in the session.
In order to make it available outside the current terminal - you'll have to logout and login.

Rot-man
  • 18,045
  • 12
  • 118
  • 124
4

1.Go to your [mongodb installation dir]:

cd <mongodb installation dir>

2.Type ./bin/mongo to start mongo:

./bin/mongo

Read More

Note :

If above command gives error

Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_ environment variables are set correctly.*

Run bellow command: Read More

export LC_ALL=C

How can find installation directory

find / -name "mongodb" 

You can also set path

export PATH=$PATH:<mongodb installation dir>/bin
Community
  • 1
  • 1
Nanhe Kumar
  • 15,498
  • 5
  • 79
  • 71
3

documentation

export PATH={mongodb-install-directory}/bin:$PATH
3

In Mac:

Go into Bash Profile:

vi ~/.bash_profile

Add Path of Installation of MongoDB:

export PATH=$PATH:/usr/local/opt/mongodb-community@4.2/bin

Reload the Bash Profile:

source ~/.bash_profile
Ank
  • 402
  • 5
  • 12
1

You can navigate to the mongo directory using the command line and then start MongoDB with

./mongodb

I was facing the same problem this worked for me.

Blitz
  • 5,521
  • 3
  • 35
  • 53
Siddhant Saxena
  • 109
  • 1
  • 4
  • Any hint why this work? It did work for me too because I couldn't run 'mongod' while within the bin directory that contained the mongod executable. Only ./mongodb worked even thought I searched for another folder that could have the mongod executable but didn't find any. I did a npm mongod driver installation previously. I don't know if that is interfering with the launching of the mongod server. Trying to execute ./mongod outside of the containing folder didn't work neither. – Lex L. Nov 15 '15 at 00:48
1

you need to install mongodb shell to ge the mongo command working.

Download link

uday
  • 569
  • 1
  • 6
  • 16
1

I didn't have the right mongo command line tools installed. This from Brew worked for me though.

brew install mongodb-community

vancy-pants
  • 1,070
  • 12
  • 13
1

in place of mongo command use mongosh command and it will work.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 30 '23 at 18:31
0

just a little recommandation, if your path has a space just wrap the whole path with quote or doublequote example : C:\Program Files\MongoDB\Server\4.2 . notice the space in "Program Files"

export PATH=$PATH:"C:/Program Files/MongoDB/Server/X.X/bin"
No One
  • 1