Install and configure MongoDB in Linux

Source: Internet
Author: User
Tags install mongodb

Install and configure MongoDB in Linux
1. MongoDB simple description

A MongoDB is an open-source database system written in C ++ Based on Distributed File storage. It aims to provide scalable and high-performance data storage solutions for WEB applications.
B MongoDB is a product between a relational database and a non-relational database. It has the most abundant functions and is most like a relational database. It supports a loose data structure and stores data as a document. The data structure consists of key-value pairs (key => value), which are similar to the json bson format, field Values can contain other documents, arrays, and document arrays, so they can store complex data types.
The biggest feature of MongoDB c is that it supports a very powerful query language. Its syntax is a bit similar to an Object-Oriented Query Language, and almost implements most of the functions similar to single-table queries in relational databases, it also supports data indexing.
Main features of d MongoDB (from the Internet, mainly for our preliminary understanding)
D.1 MongoDB provides a non-relational database that is simple and easy to operate and oriented to document storage.
D.2 You can set indexes of any attribute in the MongoDB record to achieve faster sorting.
D.3 You can create a data image through a local u or network, which makes MongoDB more scalable.
D.4 if the load increases (more storage space and better processing capabilities are required), it can be distributed on other nodes in the computer network. This is called a shard.
D.5 MongoDB supports a wide range of Query expressions. query commands use JSON tags to easily query embedded objects and arrays in documents. D.6 MongoDB can use the update () command to replace documents (data) or specified data fields. D.7 Map/Reduce in MongoDB is mainly used for batch processing and aggregation of data. Map function calls emit (key, value) to traverse all records in the set, pass the key on value to the Reduce function for processing. In addition, Map and Reduce functions are written in JavaScript, so you can execute mapreduce operations through db. runCommand and MapReduce commands. D.8 GridFS is a built-in function in MongoDB that can be used to store a large number of small files.
D.9 MongoDB allows you to execute scripts on the server. You can use JavaScript to write a function and directly execute the function on the server. You can also store the function definition on the server and directly call it next time.
D.10 MongoDB supports various programming languages: RUBY, PYTHON, JAVA, C ++, PHP, C #, and other languages. MongoDB installation is also very simple.
E. Because I want to centralize log management in MongoDB, I just learned a little and shared my learning experience with you.
Official site of f MongoDB:
G MongoDB learning site:
2. Preparations
After a brief introduction above, we have learned the basic information about MongoDB. Next we need to build it in Linux and use it.
B MongoDB package:
The software and system required for c operations are as follows: Virtual Machine (Vmware), CentOS system installed in virtual machine, Xshell, Xftp, and MongoDB package.
3. MongoDB Installation
A after completing the simple preparations above, we already have a package to be installed and a release environment. If you have not prepared enough, please prepare it yourself. Next we will start to build the environment.
B. Use XShell to connect to Centos. After the connection is successful, run the command to jump to the local directory to create your own folder, and create the mongoDB folder under this folder. The command is as follows:
B .1 (1): cd usr/local/(2): mkdir kencery (3): cd kencery/(4): mkdir mongoDB (5): cd mongoDB/
C then use Xftp to copy the mongoDB package (mongodb-linux-x86_64-3.2.8.tgz) downloaded above to the mongoDB folder, and decompress the package, decompress it and name it mongoDB ,:
    c.1 tar -zxvf mongodb-linux-x86_64-3.2.8.tgz 
    c.2 mv mongodb-linux-x86_64-3.2.8 mongodb
D. decompress the package and go to mongodb for operations (cd mongodb /),:
E. As shown in, you find two more folders: db and logs. These two folders are created by ourselves. The db folder stores the database directory, the logs folder is used to store mongoDB operation log information. The command is as follows (the command is executed below mongdb after the package is decompressed and renamed ):
    e.1 mkdir db
    e.3 mkdir logs
F. Start mongoDB to verify whether the installation is successful. The database directory option specified here is the database created in mongodb. If the command is as follows, the system prompts that the installation is successful.
    f.1 ./bin/mongod --dbpath /usr/local/kencery/mongoDB/mongodb/db
F.2 an error occurred when executing the command, as shown in figure. The error message is: (Failed to obtain address information for hostname bogon: temporary failure in name resolution (the host name corresponding to the address cannot be obtained). This is because the mongoDB startup needs to parse hosts, So we open hosts to configure the host name bogon for the local IP address. The command is as follows:
(1) vim/etc/hosts, open the editor, place the ( bogon) command at the end of the editor, save and close
(2) Restart mongoDB and find that the error has disappeared and mogodb has been started.
G. Verify whether mongoDB is started. Enter the command lsof-I: 27017. The monitoring port is in use. Therefore, the startup is complete.
4. MongoDB Configuration
A through the above MongoDB installation, we have already installed and started it, but it is too troublesome to start, so we need to configure the MongoDB configuration file by ourselves, because MongoDB developers do not have configuration samples, we need to create and write the content ourselves.
B. Run the following command in the decompressed mongoDB package to open the editor.
    b.1 vim bin/mongodb.conf
C. Open the editor and write the content shown in to the editor. Make sure that it is correct. If your installation address is different from mine, modify the path by yourself, save the edited content.
D MongDB please command parameter description:
E. Run the following command to restart mongoDB to check whether the configuration file works:
    e.1 ./bin/mongod --config /usr/local/kencery/mongoDB/mongodb/bin/mongodb.conf 
F. Check whether mongoDB is started. Enter the command lsof-I: 27017. The monitoring port is in use. Therefore, the startup is complete.
5. Set MongoDB as a system service and set it to boot
A through the simple operation above, we have configured the MongoDB configuration file, so next we will set up system services for MongoDB.
B. First add the MongoDB System Service. The command is as follows: vim/etc/rc. d/init. d/mongod
C. Open the editor, paste the following configuration, and save it.
 1         ulimit -SHn 655350 2         #!/bin/sh 3         # chkconfig: - 64 36 4         # description:mongod 5         case $1 in 6         start) 7         /usr/local/kencery/mongoDB/mongodb/bin/mongod  --maxConns 20000  --config /usr/local/kencery/mongoDB/mongodb/bin/mongodb.conf 8         ;; 9  10         stop)11         /usr/local/kencery/mongoDB/mongodb/bin/mongo --eval "db.shutdownServer()"12         ;;13  14         status)15         /usr/local/kencery/mongoDB/mongodb/bin/mongo --eval "db.stats()"16         ;;17         esac
D. After saving the file, add the script execution permission. The command is as follows: chmod + x/etc/rc. d/init. d/mongod
E. Start MongoDB, as shown in.

F you can use the Command service mongod stop to disable the MongoDB service.
G. Set the service to start when it is started. Run the command chkconfig restart D on and restart the machine for testing. It is found that the service should be started and the port is also in use.
H. Verify whether mongoDB is started. Enter the command lsof-I: 27017. The monitoring port is in use. Therefore, the startup is complete.
6. Set remote services for MongoDB (it is recommended that this parameter be set only to "inner" or "unsettable". The security factor is too high)
A. If your MongoDB running port uses the default port 27017, you can use the port 28017 to access the user interface. This is because the MongoDB Web interface access port is 1000 more than the service port.
B. Set remote access. Ports and are enabled.
C. Edit the iptables file and open the editor. The command is as follows (vim/etc/sysconfig/iptable). Enter the following information at the end of the editor and save it.
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 27017  -j ACCEPT
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 28017 -j ACCEPT
D. restart the Firewall service. The command is as follows: service iptables restart
E. Finally, access the MongDB web page through a browser. The configuration is successful.
F. Because remote service access is not secure, we do not need to configure this step normally. However, if it is configured, we can block external access through the properties of the configuration file.
F.1 open the mongodb. config file, annotate the # rest = true attribute, and close and save the file.
F.2 shut down and restart mongoDB, and then use a browser to access the mongoDB outside.

For more MongoDB tutorials, see the following:

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

MongoDB beginners must read (both concepts and practices)

MongoDB Installation Guide for Ubunu 14.04

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

Nagios monitoring MongoDB sharded cluster service practice

Build MongoDB Service Based on CentOS 6.5 Operating System

MongoDB details: click here
MongoDB: click here

This article permanently updates the link address:

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.