E-learning web site for open source, PHP 5.5, PHP 5, CSS3, MySQL, HTML5, Quiz

Blog -> Bloglist

How to install MongoDB on Ubuntu?

Follow the following steps

Step 1: Adding the public key used by the package management system Run the following command to add/import the MongoDB public GPG Key:

 
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

Step 2. Creating list file for MongoDB Create the /etc/apt/sources.list.d/mongodb.list list file using the following command:


echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

Step 3. Reload local package database Run the following command to reload the local package database:

 
sudo apt-get update

Step 4. Install the MongoDB packages You can install either the latest stable version of MongoDB or a specific version of MongoDB. Install the latest stable version of MongoDB. Run the following command:


sudo apt-get install -y mongodb-org
Install a specific release of MongoDB. Specify each component package individually and append the version number to the package name, as in the following example that installs the 2.6.1 release of MongoDB:
sudo apt-get install -y mongodb-org=2.6.1 mongodb-org-server=2.6.1 mongodb-org-shell=2.6.1 mongodb-org-mongos=2.6.1 mongodb-org-tools=2.6.1

Running MongoDB

The MongoDB instance stores its data files in /var/lib/mongodb and its log files in /var/log/mongodb by default, and runs using the mongodb user account.
If You want to change specific alternate log and data file directories in /etc/mongod.conf.

    Changing specific data path:
mkdir -p /var/www/database-path/dataMongoDb cd /var/www/database-path sudo chown mongodb:mongodb dataMongoDb sudo vim /etc/mongod.conf
Change path:
dbpath=/var/www/database-path/dataMongoDb
save and exit

Steps 1. Start MongoDB.

  
Issue the following command to start mongod:
sudo service mongod start

Steps 2. Verify that MongoDB has started successfully

Verify that the mongod process has started successfully by checking the contents of the log 
file at /var/log/mongodb/mongod.log for a line reading
tail -f /var/log/mongodb/mongod.log or ps -ef|grep mongo

Steps 3. Stop MongoDB.

As needed, you can stop the mongod process by issuing the following command:
sudo service mongod stop

Steps 4. Restart MongoDB.

Issue the following command to restart mongod:
sudo service mongod restart

Steps 5. To start command line MongoDB

 
mongo