MongoDB operation Command Detailed and backup recovery, management, monitoring "verbose"

Source: Internet
Author: User
Tags mongodb monitoring mongodump mongorestore

  • MONGDB Introduction
    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. There is no longer a concept of the line, which operates primarily on two concepts: The collection (collection), and the document.
  • It is characterized by high performance, easy to deploy, easy to use, and easy to store data. The main features are:
  • For collection storage, easy to store data for object types.
    Free mode, support query, support dynamic query.
    Supports full indexes, including internal objects.
    Supports replication and recovery.
    Use efficient binary data storage, including large objects such as video.
    Automatically handles fragmentation to support scalability at the cloud level.
    Supports multiple languages such as ruby,python,java,c++,php,c#.
    The file storage format is Bson (an extension of JSON).
    can be accessed over the network.

    • Installation configuration

    • Create a Yum source

      # vim /etc/yum.repos.d/mongodb-org.repo

      [Mongodb-org]
      Name=mongodb Repository
      baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
      Gpgcheck=1
      Enabled=1
      Gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

      # yum  install -y mongodb-org   //安装MongDB# vi /etc/mongod.conf    bindIp: 0.0.0.0             #监听地址    port: 27017                 #监听端口# systemctl start mongod.service# netstat -anpt | grep 27017
    • Start a MongoDB multi-instance

      # cp -p /etc/mongod.conf /etc/mongod2.conf# vim /etc/mongod2.conf  //修改下面     path: /data/mongodb/mongod2.log    //日志文件目录     dbPath: /data/mongodb/mongo         //数据存储目录     port: 27018                          //服务器端口
    • Create the directory and log files for the instance

      # mkdir -p  /data/mongodb/# cd /data/mongodb/# mkdir mongo# touch mongod2.log# chmod 777 mongod2.log
    • Launch instance

      # mongod -f /etc/mongod2.conf    # mongo --port 27018    //指定实例的端口# netstat -ntap
    • The underlying operation of MongoDB
    • Documents: rows that correspond to relational databases (multiple document collections)
    • Collection: A table that is equivalent to a relational database (multiple collections make up a database)
    • Some database names are reserved and have special effects
      1): admin: Add user to this library, this user inherits all permissions of database
      2): Local: This library is never copied and can be stored in any collection that is limited to a local single server
      3): Config: When MONGO is used for sharding settings, this library is used internally, saving information about shards

      # mongo   //进入数据库> db.version()     //查看版本> show dbs;        // 查看数据库> db.getMongo() //查看当前数据库机器的连接地址> db.集合名.help   //显示集合操作命令> show users  //显示用户
    • Adding and deleting changes

        > Use school; Open the school database, does not exist will be created, does not set up the collection will be deleted > db.createcollection (' info ')//Create Collection > Db.info.insert ({"id": 1, "name": "Zha           Ngsan "," hobby ": [" Game "," Talk "]})//Add Data > Db.info.find ()//View all documents in Info collection > show collections View all collections in the database > db.info.update ({"id": 10},{$set: {"name": "Tom"}})//change data > Db.info.remove ({"id": 2})//move      ID2 data in addition to info collection > Db.info.drop ()//delete Info collection > db.dropdatabase ()//delete database first use to deleted database in Delete > Db.info.count () Statistics on how many data  
      • mongodb data type

        String: String, most commonly used, must be Utf-8
        Boole An: Boolean, True or False
        Integer: integer
        Double: Floating-point number
        Arrays: Array or list, multiple values stored to a key
        Object: Used to embed a document, a value of one document
        NULL: Store null value
        Binary data: Binary for storing binary data
        Date: Stores the current date or time Unix time format

    • View data types

      > a=db.info.findOne({"id":1})    //查找指定记录并赋予别名a> typeof(a.id)   //查看属性类型
    • Backup and Recovery Management
    • Import Export
    • Export: Mongoexport
    • Import: Mongoimport
    • Option:-D Database-C collection-O directory file. JSON end-Q Export Data filter condition-f export which columns

      # for(var i=1;i<=100;i++)db.info.insert({"id":i,"name":"jack"+i})    //循环写入100条数据# mongoexport -d school -c info -o /opt/info.json   //导出# mongoimport -d school -c info1 --file /opt/info.json  //导入到info1集合# mongoexport -d school -c info1 -q ‘{"id":{"$eq":10}}‘ -o /opt/top10.json    //条件导出指定第10行
    • Backup and Recovery
    • Backup: Mongodump
    • Recovery: Mongorestore
    • Options:
      1):-h Specifies the address of the server where MongoDB is located can also specify the port (example:-H 127.0.0.1:27017)
      2):-D: Database to be backed up
      3):-O: The directory where the backup data is stored this directory needs to be created in advance

      # mkdir /backup   //创建存放目录# mongodump -d school -o /backup/       //备份school数据库# mongorestore -d school1 --dir=/backup/school    //恢复到school1数据库
    • Copy Database

      > db.copyDatabase("school","school2")     //复制数据库school 到school2中
    • Cloning a Database
    • Cloning Database School Info collection to instance 2
    • First enter the MongoDB of instance 2

    • Security management
    • Create an administrative user
    • Boot is required to specify Auth=true
    • You can assign users to roles
    • Built-in database user role: Read, ReadWrite
    • Database Administrator role: Dbamin, Dbowner, useradmin
    • Super User role: Root

      # vi /etc/mongod.conf    auth=true   //添加# mongo> use admin> db.createUser({"zkc":"root","pwd":"123","roles":["root"]})//创建用户zkc  密码123  分配到root角色> db.auth("root","123")     //验证用户
    • Process Management
    • To view the currently running process: Db.currentop ()
    • Terminating a running process: Db.killop (opid value)

      > db.currentOp()       //查看

      ......
      "Opid": 4872,

      Db.killop (4872)//Terminate 4872 process

    • MongoDB Monitoring
    • MongoDB provides some built-in tools to monitor the status information of a database
    • To view the status information for a DB instance: Db.serverstatus ()
    • To view statistics for a database: Db.stats ()
    • It is also possible to view system monitoring information through the Web interface by adding vim/etc/mongod.conf to the configuration file
         httpinterface=true
    • Then the browser accesses: http://localhost:28017

    MongoDB operation Command Detailed and backup recovery, management, monitoring "verbose"

    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.