Saturday, May 26, 2012

MongoDB Install and Quick Setup

1.) Download MongoDB from -
Copy is over to your host, unzip it.
Thats all to install Mongo DB.

2.) Create directory for storing your data

mkdir /home/oracle/data
You can create this directory anywhere on the host/filer/shared storage.

3.) Start mongoDB (

cd to bin directory inside the downloaded/uncompress mongoDB software.
cd /home/oracle/mongodb/bin

run ./mongod

By default it will start the mongoDB on port 27017.

Startup options:

If you want to change the port start it as:
./mongod --port 12345

To specify the data location:

./mongod --dbpath /home/oracle/data --port 12345

To specify the log directory path (store DB log like the alert log in oracle):

./mongod --dbpath /home/oracle/data --port 12345 -logpath /home/oracle/logs/mongo.log

To run it as a daemon

./mongod --dbpath /home/oracle/data --port 12345 --fork --logpath /home/oracle/logs/mongo.log

To get all the command like options (

./mongod -h

4.) Login/Connect to the mongoDB database instance

./mongo --port

./mongo --port 12345

Note: Just by starting multiple mongoDB instance on multiple port creates multiple mongoDB instances.

You can now run mongoDB commands

./mongo --port 12345
PRIMARY> use admin
switched to db admin
PRIMARY> show dbs --> Shows the databases.
local 10.1982421875GB
test 0.203125GB
PRIMARY> --> Give list of Admin commands
DB methods:
db.addUser(username, password[, readOnly=false])
db.auth(username, password)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost)
db.createCollection(name, { size : ..., capped : ..., max : ... } )
db.currentOp() displays the current operation in the db
db.eval(func, args) run code server-side
db.getCollection(cname) same as db['cname'] or db.cname
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold 
db.getSiblingDB(name) get the db at the same server as this one
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }
db.setProfilingLevel(level,) 0=off 1=slow 2=all
db.version() current version of the server
db.getMongo().setSlaveOk() allow queries on a replication slave server
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnock() unlocks server following a db.fsyncLock()

PRIMARY> use test  --> Change database
switched to db test
PRIMARY> show collections ---> Query the tables/collections in the database
PRIMARY> db.customer.count() ---> select the row count (in oracle - select count(*) from customer;)

5.) Shutdown MongoDB

[oracle@host1 ~]$ ps -ef|grep mongo
oracle   11899     1  0 12:25 ?        00:00:00 mongod --port 12345 --dbpath /home/oracle/test --rest --fork --logpath /home/oracle/test/mongo.log
oracle   11912 11755  0 12:25 pts/0    00:00:00 grep mongo
[oracle@host1 ~]$ mongo --port 12345
MongoDB shell version: 2.0.5
connecting to:
> use admin
switched to db admin
> db.shutdownServer()
Fri May 25 12:26:04 DBClientCursor::init call() failed
Fri May 25 12:26:04 query failed : admin.$cmd { shutdown: 1.0 } to:
server should be down...
Fri May 25 12:26:04 trying reconnect to
Fri May 25 12:26:04 reconnect ok
Fri May 25 12:26:04 Socket say send() errno:104 Connection reset by peer
Fri May 25 12:26:04 Error: error doing query: unknown shell/collection.js:151
> exit
[oracle@host1 ~]$ ps -ef|grep mongo
oracle   11919 11755  0 12:26 pts/0    00:00:00 grep mongo


Adsense Privacy Policy:

  • Google, as a third party vendor, uses cookies to serve ads on your site.
  • Google's use of the DART cookie enables it to serve ads to your users based on their visit to your sites and other sites on the Internet.
  • Users may opt out of the use of the DART cookie by visiting the Google ad and content network privacy policy.