MongoDB database detailed, and MongoDB4.0 version of the installation

Source: Internet
Author: User
Tags auth chmod create mongodb db2 mkdir mongodb commands mongodb monitoring server port

About MongoDB

Mongdb is a cross-platform, document-oriented database that enables high performance, high availability, and easy scalability, and is an open source database system based on distributed file storage. In the case of high load, adding more nodes can guarantee the performance of the server.
MongoDB is also a product between a relational database and a non-relational database, the most versatile of the non-relational databases, most like relational databases. Instead of using relational models primarily for better extensibility, MongoDB no longer has the concept of "row", which operates primarily on two concepts: Collections (collection) and documents.

Features of MongoDB
  • MongoDB features include collection-oriented storage, schema freedom, rich query statements and multilevel indexes, replication set mechanism, easy to level expansion, pluggable storage engine, cross-platform multi-language support, etc.
  • MongoDB is simple to install and provides a document-oriented storage function, which is easier to operate.
  • MongoDB provides replication, high availability, and automatic sharding capabilities. If the load increases (requiring more storage space and greater processing power), it can be distributed across other nodes in the computer network, which is called sharding.
  • The Mongo supports rich query expressions. Query directives use a JSON-style tag to easily query objects and arrays embedded in the document.
  • MongoDB supports a variety of programming languages: Ruby, Python, Java, C + +, PHP, C #, and more.
MongoDB Areas of Application

MongoDB can provide scalable, high-performance data storage solutions for WEB applications. MongoDB's main areas of application are Web site data, distributed scenarios, data caches, and JSON document format storage. For Internet applications with large data volumes, high concurrency, and weak transactions, the built-in horizontal expansion mechanism provides data processing capabilities from millions of to 1 billion levels that can be well suited to the requirements of Web2.0 and mobile Internet application datastores.

Installation of MongoDB 4.0

MongoDB provides the installation package on the Linux platform, which can be downloaded from the official website Http:// This time we choose to use the latest version of MongoDB4.0 to install and experiment.

    • Download MongoDB4.0 Package

      tar zxvf mongodb-linux-x86_64-4.0.0.tgz -C /opt
      mv /opt/mongodb-linux-x86_64-4.0.0/ /usr/local/mongodb
    • Create MongoDB data store directory, log storage directory, configuration file
      # mkdir /data/mongodb1
      # mkdir /data/logs/mongodb
      # touch /data/logs/mongodb/mongodb1.log
      # chmod -R 777 /data/logs/mongodb/mongodb1.log
      # vim /usr/local/mongodb/mongodb1.conf //Add the following line
      Dbpath=/data/mongodb1 #data storage directory
      Logpath=/data/logs/mongodb/mongodb1.log #log file
      Port=27017 #default server port
      Logappend=true #Write the log using append mode
      Fork=true #background running
      maxConns=5000 #Maximum number of simultaneous connections, default 2000
      storageEngine=mmapv1 #Specify the storage engine as a memory mapped file

Setting Kernel parameters

Echo 0 > /proc/sys/vm/zone_reclaim_mode
Sysctl -w vm.zone_reclaim_mode=0 #permanent setting
Echo never > /sys/kernel/mm/transparent_hugepage/enabled
Echo never > /sys/kernel/mm/transparent_hugepage/defrag
  • Set system environment variables for easy use

    echo ‘export MONGODB_HOME=/usr/local/mongodb‘ >> /etc/profile
    echo ‘export PATH=$PATH:$MONGODB_HOME/bin‘ >> /etc/profile
    source /etc/profile
  • Start the MongoDB service process and view the port (default 27017)

    Mongod --config /usr/local/mongodb/mongodb1.conf #Open MongoDB
    Mongod --config /usrlocal/mongodb/mongodb1.conf --shutdown #stop MongoDB
    Netstat -ntap | grep mongod
    Mongo --port 27017
    # Enter the mongo database, if you do not specify the port default into the 27017 port
  • Create multi-instance
    In the case of a single server resource, multiple instances can be used to fully use the server resources (only need to modify the data storage directory, log files and port numbers and create the appropriate directory)
    Cd /usr/local/mongodb/
    Cp mongodb1.conf mongodb2.conf
    Vim mongodb2.conf #Modify as follows
    # dbpath=/data/mongodb2 #Data storage directory
    # logpath=/data/logs/mongodb/mongodb2.log #log file
    # port=27018 #Default server port
    # logappend=true #Write the log using append mode
    # fork=true #background running
    # maxConns=5000 #Maximum number of simultaneous connections, default 2000
    # storageEngine=mmapv1 #Specify the storage engine as a memory mapped file
    Mkdir /data/mongodb2
    Touch /data/logs/mongodb/mongodb2.log
    Chmod -R 777 /data/logs/mongodb/mongodb2.log
    Mongod --config /usr/local/mongodb/mongodb2.conf #Open multiple instances
MongoDB Logical Storage structure

The logical structure of MongoDB consists of three parts: Document, Collection (collection) and database. The document is the core concept of MongoDB, it is the smallest unit of MongoDB logical storage, the equivalent of a row of records in a relational database, a collection of multiple documents, a collection equivalent to the concept of tables in a relational database, several constituent databases.

SQL Terminology/Concepts MongoDB Terminology/Concepts Explanation/Description
Database Database Database
Table Collection Database Tables/Collections
Row Document Data record lines/documents
Column Field Data fields/Fields
Index Index Index
Table joins Table connection, MongoDB not supported
Primary key Primary key Primary key, MongoDB automatically sets the _id field as the primary key
MongoDB Basic Operations
  • Login, exit

    #Local login (default instance port number: --port=27017, you can not write)
    > mongo
    #Login an instance of a remote host
    > mongo --host --port =27017
    #Exit MongoDB
    > exit
  • Database operations

    #Create a database (create a database if the database does not exist, otherwise switch to the specified database)
    > use school
    #View all databases
    > show dbs
    #delete school database
    > use school
    > db.dropDatabase()
    #Display database operation command
  • Collection
    MongoDB data is stored in the collection, and all data stored in the collection is in Binary JSON format, referred to as BSON. BSON is a binary form of storage format similar to JSON.

    #Create info collection
    > db.createcollection(‘info‘)
    #View collection
    method one:
    > show tabels
    Method Two:
    > show colletctions
    #display info collection operation command
  • Documents (Add, delete, change, check)
    #Insert a record
    # Insert a document data into the specified collection
    > db.collection.insertOne()
    #Insert multiple document data into the specified collection
    > db.collection.insertMany()
    # insert data in batches by loop
    > for(var i=1;i<100;i++){"id":i,"name":"jack"+i})


    # Delete the document with id=1 in the info collection


    #Modify the name of the info collection id=1 as the "zhangsan" document.{"id":"1"},{$set:{"name":"wzn"}})


    #Query info collection all documents
    #Query the document whose info collection id is 1.
    #Statistic record number
Backing up, recovering databases
    • Import Export
    • Export: Mongoexport
    • Import: Mongoimport
    • Option:-d Specifies the name of the database;-c Specifies the name of the collection;-f specifies which columns to export;-o Specifies the file name to export;-Q: Specifies the filter criteria for exporting data. Specific commands are viewed through--help.
    • Backup and Recovery
    • Backup: Mongodump
    • Recovery: Mongorestore
    • Options:
      1:-h Specifies the address of the server to which MongoDB is located or a port can be specified. (Example:-H
      2:-D: The database instance that needs to be backed up.
      3:-O: The directory in which the backup data resides is to be created in advance.

    • Copy Database

      > db.copyDatabase("db1","db2") //Copy database db1 to db2
    • Clone collection
    • Clone the Info collection of the database DB1 to the instance port: 27018
MongoDB Security Management

MongoDB security management mainly includes MongoDB's secure access control and user rights assignment.

    • Restrict listening to specific IP and ports

      # vim /usr/local/mongodb/mongodb1.conf
      # Bind only the internal network card address
      # Listen only to the specified port
    • Authorize startup
    • Built-in database user: Read, readWrite
    • Database management roles: DbAdmin, Dbowner, useradmin
    • Super User role: Root

      #Create super user root in db1 database, password: 123123
      > use db1
      > db.createUser({"user":"root","pwd":"123123","roles":["root"]})
      > exit
      #关闭 mongodb service
      Mongod -f /usr/local/mongodb/mongodb1.conf --shutdown
      # Start the mongodb service with authentication parameters
      Mongod -f /usr/local/mongodb/mongodb1.conf --auth
      # At this time, the query data does not display the content, and authorization authentication is required.
      > use db1
      > db.auth("root":"123123")
      # Of course, in the actual situation, we can modify the configuration file.
      # Such that when someone visits our MongoDB, we can assign a user with the appropriate permissions to log in and operate
      Vim /usr/local/mongodb/mongodb1.conf
      Auth=true //add
    • Process Management
    • View the currently running process (get high-resource-consuming process Opid)
      > db.currentOp()
    • Terminates the process of a running high-consumption resource (opid value obtained in parentheses with the above)
      > db.killOp(opid)
MongoDB Monitoring
    • View status information for a DB instance
      > db.serverStatus()
    • View statistics for the current database
      > db.status()
    • Viewing collection statistics
      > db.users.status()
    • View collection Size
      > db.users.dataSize()
    • In addition, we can view the system monitoring information through the Web interface, only need to modify the configuration file

      # vim /usr/local/mongodb/mongodb1.confhttpinterface=true    //add

      From the Web page you can see:
      1) All connections to the current MongoDB
      2) Access statistics for each database and collection, including: Reads, writes, Queries, Getmores, inserts, Updates, removes
      3) Status of the write lock
      4) The last hundreds of lines of the log file
      5) All MongoDB commands

    • Third-party monitoring tools
      We can use the MongoDB plugin to monitor the MongoDB database through Nagios configuration.

MongoDB database detailed, and MongoDB4.0 version of the installation

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.