Click here to Skip to main content
15,880,427 members
Articles / Database Development / NoSQL
Tip/Trick

Connect MongoDB from Any Working Directory on ubuntu

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
4 Nov 2014CPOL2 min read 8.9K   4  
Connect MongoDB from any working directory on ubuntu

Introduction

I have downloaded and used MongoDB by following a few steps for my Ubuntu machine.

Step 1: Download from http://www.mongodb.org/downloads.

Step 2: Extract contents from zipped folder.

Step 3: Go inside bin folder present inside extracted folder.

Step 4: Run mongod server like this ./mongod.

Step 5: Now as mongo server is running, run mongo shell to interact with database like this ./mongo.

But here, I have faced one problem, everyday to run mongod server or use mongo shell, I have to go inside my downloaded folder and run mongod from that folder. Everytime I have to repeat step 3 to step 5. What if we will have one such process so that we can connect to MongoDB database in any present working directory of our machine, then it will be a time saver for us. We don't have to remember where we have downloaded MongoDB 6 months ago.

Let's Create Alias

We have to create some alias for connecting MongoDB databases from any working directory. Aliases are shortcuts that will run those repeating steps on behalf of us. We will assign one keyword for each alias that we will use to connect with MongoDB server.

Step 1

Go to your home folder:

cd /home/binitsingh/

Step 2

Create .bash_aliases file with an editor of your choice. I will prefer vim editor. If you don't have vim editor, you can install it using the following command:

#Install vim

sudo apt-get install vim

# Create .bash_aliases file

vim .bash_aliases

Step 3

Create alias inside the above created file:

alias <alias name>='<instruction>'

#Steps to start mongod server
>cd /home/binitsingh/Downloads/mongo_download/mongodb-linux-x86_64-2.6.5/bin
>./mongod

#create alias like this for starting mongod server with mongod alias name
alias mongod='/home/binitsingh/Downloads/mongo_download/mongodb-linux-x86_64-2.6.5/bin/./mongod'

#Steps to start mongod shell
>cd  /home/binitsingh/Downloads/mongo_download/mongodb-linux-x86_64-2.6.5/bin
>./mongo

#create alias like this for starting mongod shell with mongo alias name
alias mongo='/home/binitsingh/Downloads/mongo_download/mongodb-linux-x86_64-2.6.5/bin/./mongo'

Save file with ESC key :wq ENTER key.

Note: While creating alias there will be no space between <alias name> = and <instruction>.

Step 4

Open bashrc file present in your home folder and add the following lines:

vim /home/binitsingh/.bachrc


if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Save file with ESC key :wq ENTER key.

Let's Test Alias

Let's suppose we are in our projects folder and we want to connect to mongoDB database to run some queries.

#Run mongod server
binitsingh@binit-u12:~/projects/guinness/guinness/apps$ mongod
/home/binitsingh/Downloads/mongo_download/mongodb-linux-x86_64-2.6.5/bin/./mongod --help for help and startup options
2014-11-05T00:36:02.467+0530 [initandlisten] MongoDB starting : pid=28551 port=27017 dbpath=/data/db 64-bit host=binit-u12
2014-11-05T00:36:02.467+0530 [initandlisten] db version v2.6.5
2014-11-05T00:36:02.467+0530 [initandlisten] git version: e99d4fcb4279c0279796f237aa92fe3b64560bf6
2014-11-05T00:36:02.467+0530 [initandlisten] build info: 
Linux build8.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-11-05T00:36:02.467+0530 [initandlisten] allocator: tcmalloc
2014-11-05T00:36:02.467+0530 [initandlisten] options: {}
2014-11-05T00:36:02.558+0530 [initandlisten] journal dir=/data/db/journal
2014-11-05T00:36:02.558+0530 [initandlisten] recover : no journal files present, no recovery needed
2014-11-05T00:36:02.656+0530 [initandlisten] waiting for connections on port 27017


#Open mongo shell

binitsingh@binit-u12:~/projects/guinness/guinness/apps$ mongo
MongoDB shell version: 2.6.5
connecting to: test
> 

Now you can connect to mongoDB database even if you are not present bin folder of downloaded MongoDB. We don't need to remember where we have downloaded mongodb on our machine.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --