Install and start MongoDB in Linux

Source: Internet
Author: User
Tags mongodb server mongodump mongorestore

1. Download MongoDB, here the download version is: mongodb-linux-i686-1.8.1.tgz.tar.

Http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.8.1.tgz

Related reading:

Install and start MongoDB in Linux

Advanced query example in MongoDB

MongoDB Java API for insert and single collection basic query examples

Query operations under MongoDB (corresponding to Java API query operations)

2. Extract the file to a directory and rename it:

  1. [Root@ LocalhostSrc] # tar-xzvf mongodb-linux-i686-1.8.1.Tgz.tar
  2. [Root@ LocalhostSrc] # mv mongodb-linux-i686-1.8.1/Usr/local/mongodb/

3. view the installed files:

  1. [Root@ LocalhostSrc] # cd/usr/local/mongodb/
  2. [Root@ LocalhostMongodb] # ls
  3. Bin GNU-AGPL-3.0Readme third-PARTY-NOTICES
  4. [Root@ LocalhostMongodb] # cd bin/
  5. [Root@ LocalhostBin] # ls
  6. Bsondump dbbak mongo mongod mongodump unzip Export Program program import mongorestore mongos mongosniff unzip stat

Mongod in bin is the MongoDB server process, mongo is its client, and other commands are used for other purposes of MongoDB, such as MongoDB File Export.

4. Start MongoDB.

You must first create a directory for MongoDB to store data files and log files. Here, the directory is created under/data:

  1. [Root@ LocalhostEtc] # cd/data/
  2. [Root@ LocalhostData] # ls
  3. Mongodb_data mongodb_log

Use mongod in the bin directory of the MongoDB installation directory to start MongoDB,

  1. ./Mongod -- dbpath =/data/mongodb_data/-- logpath =/data/mongodb_log/mongodb. log -- logappend &

After the startup is successful, you can check whether the startup is successful. The default port number is 27017. Of course, you can also specify unused ports at startup.

Check the port number to see if MongoDB is started.

  1. [Root@ LocalhostData] # netstat-lanp | grep"27017"
  2. Tcp00 0.0.0.0:270170.0.0.0: * LISTEN1573/Mongod
  3. Unix2[ACC] STREAM LISTENING58741573/Mongod/tmp/mongodb-27017. Sock

You can see that the database has been started successfully. Now you can use the mongo client to access the database.

  1. [Root@ LocalhostBin] # cd/usr/local/mongodb/bin/
  2. [Root@ LocalhostBin] #./mongo
  3. MongoDB shell version:1.8.1
  4. Connecting to: test
  5. >

This step indicates that the installation is successful.

5. Additional work.

Note: Previously we started MongoDB manually using mongod to start it. In this way, after the computer is shut down, it will not start again when we come in again, so we have to start it manually. Therefore, to avoid this tedious work, you can place the idea d in the Service self-startup Item, so that the computer will start as soon as the idea D service is enabled.

Edit/etc/rc. local, add the following code and save it.

 
  1. # Add mongonDB service
  2. Rm-rf/data/mongodb_data/* &/usr/local/mongodb/bin/mongod -- dbpath =/data/mongodb_data/-- logpath =/data/mongodb_log/mongodb. log -- logappend &

Restart the computer and check whether MongoDB is started. After the restart, you can directly use the mongo command to log on. The final result is successful.

In addition, if we use the mongo command to log on to MongoDB, we need to go to the directory where the mongo command is located and then execute./mongo. Is this a bit of trouble? Therefore, we can simplify this by copying the command file to/usr/bin, so that you can use the mongo command in any directory.

  1. [Root@ LocalhostBin] # ls
  2. Bsondump dbbak mongo mongod mongodump unzip Export Program program import mongorestore mongos mongosniff unzip stat
  3. [Root@ LocalhostBin] # cp mongo/usr/bin/

Go to any directory and try the mongo command:

  1. [Root@ LocalhostBin] # cd/
  2. [Root@ Localhost/] # Mongo
  3. MongoDB shell version:1.8.1
  4. Connecting to: test
  5. >

We can see that the logon is successful, which means we can use the mongo command like using the ls command.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.