MongoDB Getting Started
6:32 PMMongoDB
is a non-relational data store for JSON documents. By non-relational means, it doesn't store its data in tables like a relational database does. It stores data in JSON documents. MongoDB is schemaless, it means that two documents don't need to have the same schema. It is (from "humongous") a scalable, high-performance, open source, document-oriented database.By default, it stores data in '/data/db'. Default port is 27017.
Mongo Shell
Mongo shell is an administrative shell and has lot of similarities with node.js. It is a C++ program control using V8. Node.js application interact with MongoDB with help of node.js driver.Features supported
1) indexes2) secondary indexes
Features not supported
1) Joins2) Transactions across multiple collections
Standard commands
- db.names.insert()
- db.names.find()
- show dbs (to see the available databases)
- use database_name (to switch or use database)
Download
- Visit http://www.mongodb.org/downloads and choose the appropriate release for your OS
Installation
- Pre-create a user - u: mongouser g: mongo
- Pre-create directory structure for the Mongo Home and the database home
Eg:
mkdir /u01/mongodb - this will be the Mongo install base
mkdir -p /u01/mongodb/data/db - the main database files - Unpack the downloaded tarball into the Mongo install base
Eg:
cd /u01/mongodb
tar -xvf mongo-linux-x86_64-2.0.0.tgz - Move the contents of this back into /u01/mongodb and rm the dir mongo-linux-x86_64-2.0.0.
- Update the .bashrc to reflect the MONGO_HOME and update the PATH
MONGO_HOME=/u01/mongodb
export MONGO_HOME
PATH=$PATH:.:$MONGO_HOME/bin
Start the background process
nohup $MONGO_HOME/bin/mongod --dbpath /u01/mongodb/data/db > /u01/mongodb/mongod.nohup 2>&1 &Connect to Mongo and connect to a database called jimdb
[mongouser@iadorclgrid02 mongodb]$ mongoMongoDB shell version: 2.0.0
connecting to: test
> use jimdb
switched to db jimdb
Backup the jimdb database
- Lock the database to prevent writes
$MONGO_HOME/bin/mongo admin $MONGO_HOME/bin/fsync_lock.js - Backup the database
$MONGO_HOME/bin/mongodump -d jimdb -o /u01/mongodb/dump - Unlock the database to allow writes again
$MONGO_HOME/bin/mongo admin $MONGO_HOME/bin/unlock.js
Drop a database and shutdown the server process
- Connect to the chosen database
MongoDB shell version: 2.0.0
connecting to: test
> use jimdb
switched to db jimdb - Drop the database
> db.dropDatabase()
{ "dropped" : "jimdb", "ok" : 1 } - Connect to the admin database to shutdown a server
> use admin
switched to db admin - Shutdown the Mongo server (NB: this is not the host !)
> db.shutdownServer()
Thu Oct 13 11:26:55 DBClientCursor::init call() failed { shutdown: 1.0 }
Thu Oct 13 11:26:55 query failed : admin.$cmd to: 127.0.0.1 server should be down...
Thu Oct 13 11:26:55 trying reconnect to 127.0.0.1
Thu Oct 13 11:26:55 reconnect 127.0.0.1 failed couldn't connect to server 127.0.0.1
Thu Oct 13 11:26:55 Error: error doing query: unknown shell/collection.js:150
Thu Oct 13 11:26:55 dbexit: shutdown called
Thu Oct 13 11:26:55 [conn3] shutdown: going to close listening sockets...
Thu Oct 13 11:26:55 [conn3] closing listening socket: 6
Thu Oct 13 11:26:55 [conn3] closing listening socket: 7
Thu Oct 13 11:26:55 [conn3] closing listening socket: 8
Thu Oct 13 11:26:55 [conn3] removing socket file: /tmp/mongodb-27017.sock
Thu Oct 13 11:26:55 [conn3] shutdown: going to flush diaglog...
Thu Oct 13 11:26:55 [conn3] shutdown: going to close sockets...
Thu Oct 13 11:26:55 [conn3] shutdown: waiting for fs preallocator...
Thu Oct 13 11:26:55 [conn3] shutdown: lock for final commit...
Thu Oct 13 11:26:55 [conn3] shutdown: final commit...
Thu Oct 13 11:26:55 [conn3] shutdown: closing all files...
Thu Oct 13 11:26:55 [conn3] closeAllFiles() finished
Thu Oct 13 11:26:55 [conn3] journalCleanup...
Thu Oct 13 11:26:55 [conn3] removeJournalFiles
Thu Oct 13 11:26:55 [conn3] shutdown: removing fs lock...
Thu Oct 13 11:26:55 dbexit: really exiting now
0 comments