Installing on .deb systems
If you are running a .deb based system, first you need to add the repository for mongodb so that you can get latest packages and timely updates.
Create a file with below content.
# vim /etc/apt/sources.list.d/mongodb.list 'deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen'
You have to update local database, with the help of below
# apt-get update
Install the latest mongo-db package
[root@mongo1 ~]# apt-get install mongodb-org
Now, we have to start MongoDB, run:
# service mongodb restart
How to use Mongodb
We will now create databases and users on MongoDB. Get connected with mongo shell with the help of mongo utility.
# mongo --host 127.0.0.1 --port 27017
You should see this in shell if everything is installed correctly:
MongoDB shell version: 2.6.4 connecting to: 127.0.0.1:27017/test
In order to see databases on mongodb run this command in mongodb shell (by default you will connect with test database):
> show dbs; admin (empty) local 0.078GB example 0.078GB test 0.078GB
Now we are going to create a database for our need. The command to create a new database is ‘use DATA_BASE_NAME’. Here we are creating a database called ‘themukt’.
> use themukt; switched to db themukt
Now we will create a new user, password and connect it to the appropriate database using user function. It will create a user with readwrite(privileges) on themukt database with password.
> db.createUser({"user":"new_user_name","pwd":"new_user_password","roles": [{ role: "readWrite", db: "themukt" }]})
Successfully added user: {
"user" : "admin1",
"roles" : [
{
"role" : "readWrite",
"db" : "themukt"
}
]
}
We need to create collection (known as tables on other databases) for this database.
> db.mukttable.insert({'S_No' : "1", "Name" : "USER_NAME"});
WriteResult({ "nInserted" : 1 })
See the o/p of above command Inserted: 1, it means you have inserted one document in collection.
With the help of find function, you can see the content of collection:
> db.mukttable.find();
{ "_id" : ObjectId("54074551632f07d8ab898c5f"), "S_No" : "1", "Name" : "USER_NAME" }
> exit;