MongoDB-Introduction, installation, library management, backup, and recovery

Source: Internet
Author: User
Tags mongodump mongo shell mongorestore

Mongodb Introduction

Document Type Database

The C + + language, Distributed file storage open source database system, belongs to NoSQL

In the case of high load, more nodes can be added to ensure the performance of the server

Provide scalable, high-performance data storage solutions for Web applications.

Stores data as a document, data structures consist of key-value pairs, and MongoDB documents resemble JSON objects. Field values can contain other documents, arrays, and an array of documents.

used by default starting from 3.2 Wiredtiger storage engine.

NoSQL MongoDB Installation

Vim/etc/yum.repos.d/mongodb-org-3.4.repo
[mongodb-org-3.4]
name=mongodb Repository
baseurl=
Gpgcheck=1
enabled=1
gpgkey=


yum install-y mongodb-org
#sed-i "s%bindip:127.0.0.1% #bindIp: 127.0.0.1%g "/etc/mongod.conf listen to all IPs, listen to multiple 192.168.1.1,192.168.1.2 with commas separated



Unloading

Yum Erase $ (rpm-qa | grep mongodb-org)
rm-r/var/log/mongodb
rm-r/var/lib/mongo


Prompt when entering MONGO shell

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/8E/90/wKioL1jFcsDDiR6zAAK-9iKwiSU256.png-wh_500x0-wm_ 3-wmp_4-s_3470903486.png "title=" Clipboard.png "alt=" Wkiol1jfcsddir6zaak-9ikwisu256.png-wh_50 "/>


For the authentication part of the read/write (first warning), warning is temporarily unresolved, and this is also the issue in the official documentation operation.

Modifying the kernel

Echo never >/sys/kernel/mm/transparent_hugepage/enabled
Echo never >/sys/kernel/mm/transparent_hugepage/ Defrag


systemctl start Mongod.service
chkconfig mongod on set boot up

You can start with mongodb-f/etc/mongodb.conf when you cannot start, you can view the error


MONGODB Connectivity and user management

MONGO into the MongoDB shell (--port port 27017 specified by default port)

Connecting a remote host MONGO--host 127.0.0.1

mongo-uusername-ppasswd

User management (for the library), the establishment of the user must first enter into the corresponding library

Use test (switch library, not automatically created)

DB//The database where the display resides

Db.createuser ({User: "admin", pwd: "123456", Roles:[{role: ' Dbowner ', db: ' UserDB '}]})

Use admin

Db.system.users.find ()//Must be in the Admin library to find all users, list all users, need to switch to the admin library

Show Users; View all users of the current library

Db.dropuser (' admin ')//delete user

MongoDB Library Management

Db.version ()//view version number

Use test (switch library, not automatically created)

Show DBS//view library, when UserDB does not appear, because the library is empty and there are no collections, just create a collection to see

Db.createcollection (' clo1 ')//Create collection Clo1, create under current library

Db.dropdatabase ()//delete the current library, you must switch to the library and then delete

Db.stats ()//view current library information

Db.serverstatus ()

Db.createcollection (' Clo1 ', {capped:true,autoindexid:true,size:6142800,max:10000})

Capped:true enables capping collections, fixed-size collections, which automatically overwrite the oldest entries when they reach the maximum value. If true, the dimension size needs to be specified.

, Autoindexid:true automatically creates an index _id field is false by default

SIZE Specifies the cap set for the maximum byte, in unit B

MAX Specifies the maximum number of capped collections allowed in the file.

Show Collections//View all collections can be used with show tables

Db.clo1.insert ({clo1id:1,username: "123", Password: "123456"})//If the collection does not exist, the collection is created automatically

Db. Account.find ()//View all documents for account

Db. Account.find ({accountid:2})//View the contents of AccountID

Db.clo1.update ({clo1id:1},{"$set": {"Age": 20}})

Db.clo1.remove ({clo1id:1})//delete by condition

Db.clo1.drop ()//delete all documents, i.e. delete combinations

Db.printcollectionstats ()//view collection status


MongoDB replica set (REPL set)

Replica set, this mode has a master (primary), multiple from (secondary), read-only. Support for them to set the weight, when the main outage, the highest weight from the switch-based (the spare weight needs higher than the other). In this architecture, it is possible to establish a quorum role that is only responsible for adjudication, without storing data (also a MongoDB database), and reading and writing data in the Lord, the goal of load balancing needs to be manually specified for the Read library target server.

Modifying a configuration file/etc/mongod.conf

Replication

test01

Mongod

Use admin

> config={_id: "test01", Members:[{_id:0,host: "192.168.1.88:27017"},{_id:1,host: "192.168.1.89:27017"},{_id:2, Host: "192.168.1.90:27017"}]}

> rs.initiate (config)//view status Rs.status () may not need to perform the following steps

> Rs.add ("192.168.1.89")

> Rs.add ("192.168.1.90")

> Rs.status () View status

If the status of two from above is "statestr": "STARTUP", the following actions are required:

> var config={_id: "test01", Members:[{_id:0,host: "192.168.1.88:27017"},{_id:1,host: "192.168.1.89:27017"},{_id : 2,host: "192.168.1.90:27017"}]}

> rs.reconfig (config)

Check the Rs.status () status again secondary

Weight settings: The default three weights are 1, if any one of the weight set higher than the other, then the machine immediately switch to the primary role, so 88:3 89:2 90:1

In the Lord's execution

Cfg=rs.conf ()

Cfg.members[0].priority = 3

Cfg.members[1].priority = 2

cfg.members[2].priority = 1

Rs.reconfig (CFG)//89 nodes become candidate master nodes


MongoDB Backup and Recovery

Backup Library

Mongodump-h ip-d dbname-o dir//-h back with service ip-p port-d database name, do not add backup all databases,-o backup directory

Back up the specified collection

mongodump-d mydb-c testc-o/tmp/test//-c The name of the specified collection

Export Collection as JSON file

mongoexport-d mydb-c testc-o/tmp/testc.json//-O followed by the name of the file

Recover all the libraries

Mongorestore--drop/tmp/123//--drop Delete the original library and then restore/tmp/123 back up the names of all the library directories

Restores the specified library

Mongorestore-d mydb dir///-d to restore the name of the library dir the directory where the library was backed up

Recovering collections

mongorestore-d mydb-c test Dir/mydb/testc.bson//-c need to restore the name of the collection dir backup when the path required to specify the Bson file to be restored

Import Collection

mongoimport-d mydb-c TESTC--file/tmp/testc.json


This article is from the "Share,open source" blog, so be sure to keep this source http://liqilong2010.blog.51cto.com/3029053/1905724

MongoDB-Introduction, installation, library management, backup, and recovery

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.