How do I ensure that Mongo binaries are in my PATH - in my shell' rc (~/.bashrc) on a Mac

shareyourpeace picture shareyourpeace · Feb 1, 2015 · Viewed 28.3k times · Source

I am learning mongoDb.
I did the following: I understand this.

mkdir -p mongodb
cp -R -n mongodb-osx-x86_64-2.6.7/ mongodb

These next ** instructions are not crystal clear to me.

** Ensure the location of the binaries is in the PATH variable. The MongoDB binaries are in the bin/ directory of the archive. To ensure that the binaries are in your PATH, you can modify your PATH.

For example, you can add the following line to your shell’s rc file (e.g. ~/.bashrc):

export PATH=<mongodb-install-directory>/bin:$PATH

Replace with the path to the extracted MongoDB archive.

End of the instructions.

From terminal on my Mac

$which bash
/bin/bash

The bin directory has a bunch of commands: bash, chmod, ln, pid, mkdir.

I cannot find the directory where I should include the PATH code above.

Thanks

Abhay's instructions worked ! Sorry that the following is long but I believe it is necessary because you were so clear last time and I want you to see my file structure.
If email is easier let me know but this will more likely than not help others :) I really want to understand my file structure so if you do not mind a bit of further clarification. Thanks again for your expertise.

1. But why are all of the binaries (the executables) listed individually in this bin folder ? Are they the 'only' files that should 'live' in this folder ? Perhaps when I was installing bower grunt etc. I should installed those (bower grunt etc) into a different location. I am trying to keep my file structures logical and clean. Here is what I have.

MacBookPro:usr regina$ cd ..
MacBookPro:/ regina$ ls
Applications        bin         net
CoreSyncInstall.log cores           private
Library         data            sbin
Network         dev         tmp
System          etc         usr
Users           home            var
Volumes         mach_kernel
MacBookPro:/ regina$ cd usr/local/bin
MacBookPro:bin regina$ ls
bower       grunt       mongod      mongorestore    npm
browserify  gulp        mongodump   mongos      statsd
bsondump    http-server mongoexport mongosniff  yo
cake        karma       mongofiles  mongostat
coffee      lessc       mongoimport mongotop
cordova     mkdirp      mongooplog  node
express     mongo       mongoperf   nodemon
MacBookPro:bin regina$ 

2. Also, I wanted to look at my .bashrc file. I do not have one. I am on osx 10.8.5 I want to 'view' my $PATH variable. I believe I found it somewhere months ago. I was using nano to open a bunch of files because I was curious. Was it listed as an environmental variable ?

MacBookPro:/ regina$ ls
Applications        bin         net
CoreSyncInstall.log cores           private
Library         data            sbin
Network         dev         tmp
System          etc         usr
Users           home            var
Volumes         mach_kernel
MacBookPro:/ regina$ cd home
MacBookPro:home regina$ ls -la
total 2
dr-xr-xr-x   2 root  wheel     1 Feb  3 08:29 .
drwxr-xr-x  33 root  wheel  1190 Jan 25 17:21 ..
MacBookPro:home regina$ 
  1. I start the mongo shell like this. I am not showing all of the lines output in terminal.
    Look at my file structure. I originally followed a cp command but it did not explain 'where' to copy the archived (desktop) mongodb-osx-x84.... folder so look where I copied it to. Now I have these files in 2 locations - yes ? And I have a mongodb folder inside of a higher level mongodb folder. Why ? How did I manage this ? It seems illogical. I have been indented 4 spaces but the below will not format so I just added line breaks. sorry.

    MacBookPro:home regina$ cd ..

    MacBookPro:/ regina$ ls Applications bin net CoreSyncInstall.log cores private Library data sbin Network dev tmp System etc usr Users home var Volumes mach_kernel

    MacBookPro:/ regina$ cd data

    MacBookPro:data regina$ ls

    db mongodb

    MacBookPro:data regina$ cd mongodb

    MacBookPro:mongodb regina$ ls

    mongodb MacBookPro:mongodb regina$ cd mongodb MacBookPro:mongodb regina$ ls GNU-AGPL-3.0 THIRD-PARTY-NOTICES README bin

    MacBookPro:mongodb regina$ cd bin

    MacBookPro:bin regina$ ls

    bsondump mongodump mongoimport mongorestore mongostat mongo mongoexport mongooplog mongos mongotop mongod mongofiles mongoperf mongosniff

  2. This is what happens when I try and type 'mongo' as another forum suggested.

    MacBookPro:bin regina$ mongo MongoDB shell version: 2.6.7 connecting to: test 2015-02-03T08:36:54.157-0500 warning: Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused

So I typed this which still failed.

MacBookPro:bin regina$ ./mongo
MongoDB shell version: 2.6.7
connecting to: test
2015-02-03T08:37:03.599-0500 warning: Failed to connect to    127.0.0.1:27017, reason: errno:61 Connection refused

Then I tried this which connected - opened port 27017 - and is listening. Why did I need the daemon command ?

MacBookPro:bin regina$ ./mongod
./mongod --help for help and startup options
2015-02-03T08:37:08.712-0500 [initandlisten] MongoDB starting :    pid=487 port=27017 dbpath=/data/db 64-bit host=macbookpro.home
more lines of code
2015-02-03T08:37:09.018-0500 [initandlisten] waiting for connections on port 27017

Answer

Abhay PS picture Abhay PS · Feb 2, 2015

When you download MongoDB for Mac, you get an archive file. First thing you need to is uncompress the archive. To do that you just double click on the archive. The uncompressed folder will have the binaries in the bin folder, like mongodb-osx-x86_64-2.6.7/bin. What you need to do next is to make sure these binaries are accessible. So, for this you can do either of the two things -

  1. Copy the binaries to /usr/local/bin folder. As /usr/local/bin is always in PATH, so you don't have to add it to the PATH.

    This is pretty simple. Run cp mongodb-osx-x86_64-2.6.7/bin/* /usr/local/bin/. That's it. You are all done.

  2. Add the binary folder's path to your $PATH variable.

    Open the .bashrc file, which is located in your home folder, and put export PATH=$HOME/mongodb/bin:$PATH line in the end. I am assuming that mongodb uncompressed in downloads.

If going by the second method, make sure you don't accidently delete the mongodb folder that is added in the PATH.

Once you have finished doing this, close the terminal, open a new terminal window and run:

mongo --version

If you get the mongodb info with version, then good.

If you still get something like command mongo not found, then make sure you followed above instructions right. Also make sure you are not using any other shell, like zsh. If you are, then you have to add the export statement in the respective shell file. For zsh the file name is .zshrc.


Okay! So as you have updated the initial question with some more questions. So here goes the explanation point by point:

  1. The files you see in /usr/local/bin folder are binaries. In simple language, they are executable programs. These executable binaries live there though you can run them from anywhere in the command line.

  2. The .bashrc file is found in user's home directory. On Mac it's /Users/username. In your case, it should be /Users/regina because your name is Regina.

  3. mongo is the client application. It is used to interact with the mongodb database. And, mongod is the database deamon that stores the data. So first you have to run mongod, and then only you can connect to it using the client mongo and start querying.