Saturday, May 26, 2012

MongoDB Install and Quick Setup


1.) Download MongoDB from - http://www.mongodb.org/downloads
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 (http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo#StartingandStoppingMongo-RunningasaDaemon)

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 (http://www.mongodb.org/display/DOCS/Command+Line+Parameters)

./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> db.help() --> Give list of Admin commands
DB methods:
db.addUser(username, password[, readOnly=false])
db.auth(username, password)
db.cloneDatabase(fromhost)
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.dropDatabase()
db.eval(func, args) run code server-side
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionNames()
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.getName()
db.getPrevError()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold 
db.getReplicationInfo()
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.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus()
db.removeUser(username)
db.repairDatabase()
db.resetError()
db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }
db.serverStatus()
db.setProfilingLevel(level,) 0=off 1=slow 2=all
db.shutdownServer()
db.stats()
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
customer
person
system.indexes
PRIMARY> db.customer.count() ---> select the row count (in oracle - select count(*) from customer;)
49996

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: 127.0.0.1:12345/test
> 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: 127.0.0.1:12345
server should be down...
Fri May 25 12:26:04 trying reconnect to 127.0.0.1:12345
Fri May 25 12:26:04 reconnect 127.0.0.1:12345 ok
Fri May 25 12:26:04 Socket say send() errno:104 Connection reset by peer 127.0.0.1:12345
Fri May 25 12:26:04 Error: error doing query: unknown shell/collection.js:151
> exit
bye
[oracle@host1 ~]$ ps -ef|grep mongo
oracle   11919 11755  0 12:26 pts/0    00:00:00 grep mongo


0 Comments: