MongoDB Study notes-linux build MongoDB environment _mongodb

Source: Internet
Author: User
Tags eval mkdir mongodb unpack centos iptables port number

1.MongoDB Simple Description

A MongoDB is an open source database system written in C + + language based on Distributed file storage, which aims to provide scalable and high-performance data storage solutions for Web applications.

b MongoDB is a product between relational database and non relational database, which has the richest function and the most like relational database. It supports a very loose data structure that stores data as a document, a data structure made up of key-value pairs (key=>value), and a JSON-like Bson format.

C MongoDB The biggest feature is that it supports the query language is very powerful, its syntax is similar to the object-oriented query language, can almost achieve a similar relational database single table query most of the functions, but also support the indexing of data.

D MongoDB Main features (from the Internet, mainly for our initial understanding)

D.1 MongoDB provides a document-oriented storage that is simpler and easier to operate than a relational database.

D.2 You can set the index of any property in the MongoDB record to achieve a faster sort.

D.3 You can create a data mirror from a local U or network, which makes MongoDB more scalable.

D.4 If the load increases (requiring more storage space and more processing power), it can be distributed across other nodes in the computer network, which is called fragmentation.

D.5 MongoDB supports rich query expressions, and query directives use JSON-form tags to easily query objects and arrays embedded in the document.

D.6 MongoDB Use the update () command to implement a replacement completed document (data) or some specified data field.

The map/reduce in D.7 MongoDB is mainly used for batch processing and aggregation of data, and the map function calls emit (Key,value) to traverse all records in the collection, passing key to the reduce function for processing. In addition, the map function and the reduce function are written using JavaScript, so you can perform mapreduce operations through the Db.runcommand and MapReduce commands.

D.8 Gridfs is a built-in feature in MongoDB that can be used to store a large number of small files.

D.9 MongoDB allows scripts to be executed on the server, can write a function in JavaScript, executes directly on the server, or the definition of the function is stored on the server, and the next call can be made directly.

D.10 MongoDB supports a variety of programming languages: RUBY, PYTHON, JAVA, C + +, PHP, C # and many other languages, and MongoDB installation is also very simple.

e because want to focus on the log management to MongoDB, so a simple study, will learn to share the experience to everyone.

F MongoDB official website:

2. Preparatory work

A after the simple introduction we have learned the basic information of MongoDB, then we first need to build it under Linux and to use it.

b MongoDB Package download address:

C operation requires the following software and systems: Virtual machine (Vmware), virtual machine installed in the CentOS system, XSHELL,XFTP, MongoDB package.

3.MongoDB Installation

A through the simple preparation of the above, we already have the package can be installed and the release of the environment, if not fully prepared, please prepare yourself, below we start to build the environment

b Use the Xshell connection CentOS, after the successful connection, use the command to jump to the local to create your own folder Kencery, under this folder to create the MongoDB folder, the following command:

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) that is downloaded above to the MongoDB folder, and unpack the package, and then name it after the MongoDB, as shown in the figure:

C.1 TAR-ZXVF mongodb-linux-x86_64-3.2.8.tgz

C.2 MV mongodb-linux-x86_64-3.2.8 MongoDB

D after decompression jump to MongoDB under Operation (CD mongodb/), as shown in the figure:

E As shown above, you found two more folders, one is DB, one is logs, these two folders are created by ourselves, the function of the DB folder is to hold the database directory, the role of logs folder is to store MongoDB operation log information, commands are as follows ( The execution of this command jumps to the execution below MONGDB after the package decompression is renamed:

E.1 mkdir db
e.3 mkdir logs

F add complete, start MongoDB, verify that the installation is successful, the database directory option specified here is the db created under MongoDB, the command below, as shown in the figure, prompts for a successful start.


F.2 When you execute the command as shown in the figure above, we find an error, the error message is: (Failed to obtain address information for hostname bogon:temporary failure in name Reso Lution (Get the hostname of the address)), this is because the MongoDB startup needs to resolve hosts, so we open the hosts to the local IP configuration host name Bogon, the command is as follows:

(1) vim/etc/hosts, after opening the editor, place the command ( Bogon) at the end of the editor, save and Close

(2) Reboot MongoDB, found that the error has disappeared, and Mogodb has started.

G Verify MongoDB is started, enter command lsof-i: 27017, the monitoring port is already in use, so said start has been completed.

4.MongoDB Configuration

A through the above MongoDB installation, we have installed it successfully and started, but found that startup is too cumbersome, so we need to give MongoDB configuration profile, because MONGODB developers do not configure the sample, so we need to create and write the content

b First of all, we mongodb unpack the package inside the operation, execute the following command, open the editor.

B.1 Vim bin/mongodb.conf

C After opening the editor, the following image will be written to the editor, guaranteed to be correct, if your installation address and my inconsistent, you need to modify the path, the editor is completed after the save.

d mongdb Please command parameter description:

e Use the following command to restart MongoDB to see if the configuration file works, as follows



F Verify that MongoDB is started, enter the command lsof-i: 27017, the monitoring port is already in use, so the boot has been completed.

5.MongoDB set to system service and set boot up

A with the above simple operation, we have MONGODB configuration file configuration completed, then we will set up the system services for MongoDB.

B. First add the MongoDB system service, the command is as follows: Vim/etc/rc.d/init.d/mongod

C After opening the editor, we paste the following configuration in, and then save

Ulimit-shn 655350
  # chkconfig:-The Description:mongod case is in
  /usr/ Local/kencery/mongodb/mongodb/bin/mongod--maxconns 20000--config/usr/local/kencery/mongodb/mongodb/bin/ mongodb.conf;
  /usr/local/kencery/mongodb/mongodb/bin/mongo "Db.shutdownserver ()"
  /usr/local/kencery/mongodb/mongodb/bin/mongo "db.stats ()"

D After the save is complete, add script execution permissions, as follows: chmod +x/etc/rc.d/init.d/mongod

E start MongoDB, as shown in the following illustration, to indicate successful startup

f you can use the command service Mongod stop to turn off the MongoDB service.

G This service is set to boot, as follows: Chkconfig mongod on, then reboot the machine for testing, and discover that the boot service should start and the port is also in use.

H Verify MongoDB is started, enter command lsof-i: 27017, the monitoring port is already in use, so the boot has been completed.

6.MongoDB Set up remote service (recommended only in intranet or not set, not too high safety factor)

A If your MongoDB run port uses the default 27017, you can use the port number to access the user interface for 28017来 because the MongoDB Web interface access port is 1000 more than the service port

b Set up remote access, both open ports 27017,28017

C First edit iptables file, open Editor, command as follows (vim/etc/sysconfig/iptable), enter the following information at the end of the editor, and finally save.

-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 reboot the Firewall service, commands are as follows: Service iptables restart

E. Finally, the browser accesses the MONGDB Web page, as shown in the illustration, indicating that the configuration was successful.

F. Because access through remote services is not secure, we normally do not need to configure this step, but if configured, we can block its external access through the properties of the configuration file

F.1 Open the Mongodb.config file, and then close the save after #rest=true the property annotation.

F.2 shuts down and restarts MongoDB, and then uses a browser access outside to discover that it has not been accessed.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.