In-depth understanding of how to configure MongoDB in MongoDB (1) Linux

Source: Internet
Author: User
Tags failover mongodb server try catch pkill

In-depth understanding of how to configure MongoDB in MongoDB (1) Linux
I MongoDB Introduction


MongoDB is a high-performance, open-source, and non-pattern document-based database. It is a popular NoSql database. It can be used in many scenarios to replace traditional relational databases or key/value storage methods. Mongo is developed using C ++. Mongo's official website address is the reader can provide more detailed information here.



It features high performance, ease of deployment, and ease of use, making it easy to store data. Features:
  • It is designed for centralized storage and is easy to store object-type data.
  • Free mode.
  • Supports dynamic query.
  • Supports full indexing, including internal objects.
  • Query is supported.
  • Supports replication and fault recovery.
  • Use efficient binary data storage, including large objects (such as videos ).
  • Automatic fragment processing to support scalability at the cloud computing level.
  • Supports RUBY, PYTHON, JAVA, C ++, PHP, C #, and other languages.
  • The file storage format is BSON (a json extension ).
  • It can be accessed through the network.



  • Set-oriented storage: Suitable for storing objects and JSON data.
  • Dynamic query: Mongo supports multiple Query expressions. Query commands use JSON tags to easily query embedded objects and arrays in a document.
  • Complete indexing support: Includes embedded object and array of documents. The Query Optimizer of Mongo analyzes the query expression and generates an efficient query plan.
  • Query monitoring: Mongo provides a monitoring tool to analyze the performance of database operations.
  • Replication and automatic failover: The Mongo Database supports data replication between servers and master-slave mode and mutual replication between servers. The primary objective of replication is to provide redundancy and automatic failover.
  • Efficient traditional storage: supports binary data and large objects (such as photos or images)
  • Automatic sharding to support cloud-level Scalability: the automatic sharding function supports horizontal database clusters and allows you to dynamically add additional machines.


Applicable scenarios:

  • Real-time website data processing. It is ideal for real-time insertion, update, and query, as well as the replication and high scalability required for real-time website data storage.
  • Cache. Because of its high performance, it is suitable for serving as the cache layer of the information infrastructure. After the system is restarted, the persistent cache layer built by the system can avoid data source overload at the lower layer.
  • High scalability scenarios. It is ideal for databases composed of dozens or hundreds of servers. Its Roadmap includes built-in support for the MapReduce engine.

Unsuitable scenarios:

  • A highly transactional system is required.
  • Traditional Business Intelligence applications.
  • Complex cross-document (table) cascade queries.


2. MongoDB Configuration Overview


Version description:

RedHat: 6.1 x86_64

MongoDB: 2.6.3


First, we go to the official website ( to download the 64-bit Linux version of MongoDB;


Then, make preparations before configuring MongoDB;

-- Create the MongoDB home directory # mkdir/usr/local/mongodb/-- decompress the MongoDB package to the MongoDB home directory # tar-xvf mongodb-linux-x86_64-2.6.3.tgz-C/usr/local/mongodb/-- create the MongoDB data directory, it can be stored in other locations, such as RAID and LVM # mkdir/usr/local/mongodb/data/-- create the MongoDB log directory, put it in the var directory # mkdir/usr/local/mongodb/log/

Next, run mongod to start MongoDB, then open another terminal, and use mongo to connect to MongoDB;

# cd /usr/local/mongodb/bin/# ./mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/log/mongo.log# ./mongoMongoDB shell version: 2.6.3connecting to: test>

Then, configure the environment variables;

# pwd/usr/local/mongodb/bin# vim ~/.bash_profile # tail -n3 !$tail -n3 ~/.bash_profilePATH=$PATH:$HOME/bin:/usr/local/mongodb/binexport PATH# source !$source ~/.bash_profile

To enable and disable MongoDB more conveniently, you can use Shell to write scripts, or add them to the service;

# Cp ssh mongodb # vim mongodb # cat mongodb #! /Bin/bash # mongodStart up the MongoDB server daemon # source function library. /etc/rc. d/init. d/functions # define command CMD =/usr/local/mongodb/bin/mongod # define data directory DBPATH =/usr/local/mongodb/data # define log directory LOGPATH =/usr /local/mongodb/log/mongo. logstart () {# fork indicates running $ CMD -- dbpath = $ DBPATH -- logpath = $ LOGPATH -- forkecho "MongoDB is running background... "} stop () {pkill shortdecho" MongoDB is stopped. "} case" $1 "instart) start; stop) stop; *) echo $" Usage: $0 {start | stop} "esac

We can test the correctness of the script writing;

# /etc/init.d/mongodb startabout to fork child process, waiting until server is ready for connections.forked process: 1347child process started successfully, parent exitingMongoDB is running background...# /etc/init.d/mongodb stopTerminated

Of course, a better way is to use the configuration file, write the parameters required by MongoDB to the configuration file, and then reference them in the script;

$ Vim mongodb. conf $ cat mongodb. conf # indicates the port number, if this parameter is not specified, the default value is 27017 # port = 27027 # MongoDB data file directory dbpath =/usr/local/mongodb/data # MongoDB log file directory logpath =/usr/local/mongodb/ log/mongo. log # automatically accumulate logappend logs = true

After compiling the configuration file, we need to modify the startup script;

# Vim mongodb # cat mongodb #! /Bin/bash # mongodStart up the MongoDB server daemon # source function library. /etc/rc. d/init. d/functions # define command CMD =/usr/local/mongodb/bin/mongod # define the configuration file path INITFILE =/usr/local/mongodb. confstart () {# & indicates that the background is started. You can also use the fork parameter $ CMD-f $ INITFILE & echo "MongoDB is running background... "} stop () {pkill shortdecho" MongoDB is stopped. "} case" $1 "instart) start; stop) stop; *) echo $" Usage: $0 {start | stop} "esac

After writing the script, test the correctness of the script again.

# /etc/init.d/mongodb startMongoDB is running background...# /etc/init.d/mongodb stopTerminated

3. Postscript

This is the first article to deeply understand MongoDB. This article briefly introduces MongoDB and presents the complete configuration process in Linux to readers.


Iv. References


Good Luck!


August 2, 2014

Why is there no configuration file for installing mongodb on linux? The official mongodb website says it is the installation package directly decompressed by/etc/mongodbconf by default.

For source code installation, mongod. conf seems to need to compile one by yourself.
Maybe I did not find it. I went to the official website and did not know where it was. I just got it myself.
For more information, see.

In a 64-bit Linux system, is there a limit on the size of a single file uploaded using program files?

On the first floor, does MongoDB have a configuration file? Where? This is my first hearing.

We recommend that you use any mainstream driver for upload, such as the java API, which contains the GridFS class, which is suitable for managing file systems. This makes it easy for me to handle errors, for example, try catch and writeconcern.
You cannot Troubleshoot any errors.

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: 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.