NoSQL--Mongodb installation using replica set backup recovery

Source: Internet
Author: User
Tags mongodb collection mongodb server mongodump mongo shell mongorestore

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/8A/98/wKiom1g0gDfTgtVlAADLIcDzeSA294.png "title=" 1.png " alt= "Wkiom1g0gdftgtvlaadlicdzesa294.png"/>

Mongodb is a distributed file storage based open source database system written by C + +, between relational database and non-relational database, and support query language is very powerful. Add multiple nodes to ensure server performance in high-load situations, designed to provide scalable, high-performance storage for Web applications; MongoDB stores data as a document, consisting of key-value pairs (key=>value), similar to JSON objects.

Official website: https://www.mongodb.com/

Application:

CERN, the well-known Institute of Particle Physics, CERN data for the Large Hadron Collider using MongoDB;

Craiglist, on the use of MongoDB archive billions of records;

Shutterfly, an Internet-based social and personal Publishing Service that uses MongoDB's various persistent data storage requirements;

Intuit Company, a software and service provider for small businesses and individuals, uses MongoDB to track users ' data for small businesses;

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/8A/98/wKiom1g0g5LybJrGAAGX2nzBNAw214.png "title=" 2.png " Style= "WIDTH:650PX;HEIGHT:224PX;" alt= "Wkiom1g0g5lybjrgaagx2nzbnaw214.png" width= "650" vspace= "0" hspace= "0" Height = "224" border= "0"/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/8A/95/wKioL1g0g7HyOJmKAAFzRTS--g4083.png "title=" 3.png " Style= "width:700px;height:366px", "alt=" Wkiol1g0g7hyojmkaafzrts--g4083.png "width=" "vspace=" 0 "hspace=" 0 "Height = "366" border= "0"/>

MongoDB Installation

Www.apelearn.com/bbs/data/download/mongodb-org-3.0.tar.gz//rpm bag, everyone can use

The official source only supports 64-bit system, installed Epel extension source, can install mongodb2.4 version, this time we install version 3.0

Vim/etc/yum.repos.d/mongodb-org-3.0.repo//Add the following:
[mongodb-org-3.0]
Name=mongodb Repository
baseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/
Gpgcheck=0
Enabled=1
Installation: Yum install-y mongodb-org
Editor: vim/etc/mongod.conf
Fork:true
Pidfilepath:/var/run/mongodb/mongod.pid

bind_ip:192.168.1.1,192.168.1.2,192.168.1.3 # blank words mean listening to all

Note: Delete The comments at the beginning of these two lines, or there will be a problem when restarting
echo Never >/sys/kernel/mm/transparent_hugepage/enabled
echo Never >/sys/kernel/mm/transparent_hugepage/defrag
vim/etc/security/limits.conf//Join
Mongod Soft Nofile 64000
Mongod Hard Nofile 64000
Mongod Soft Nproc 32000
Mongod Hard Nproc 32000
Start: Service Mongod Start # process will be slow, this is it in writing data/var/lib/mongo

If the boot fails, it can be started in the following way: Mongod-f/etc/mongod.conf

Use of MongoDB
MongoDB Connection:

MONGO//directly into the MONGO shell

MONGO--port 27018//Specify the listening port to enter

MONGO--host 127.0.0.1//Connect remote MONGO

MONGO-UUSERNAME-PPASSWD//Set user name password when validating, similar to MySQL

MongoDB User management:

Use admin//switch to Admin library, Admin and test is the default own two libraries

Use TENG12//switch to TENG12 Library, does not exist automatically created

Show Users//view all user under current library

Db.system.users.find ()//list all users, need to switch to admin library

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

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

User role: Http://bbs.51cto.com/thread-1146654-1.html

Mongod B Library Management:

Db.stats ()//view information for the current library

Db.version ()//view version

Show Teng12//view library, the library is empty, so no UserDB, create a collection to see

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

Db.dropdatabase ()//delete the current library, to delete a library, you must switch to that library

Db.serverstatus ()//view the status of the MongoDB server

MongoDB Collection:

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

Syntax: Db.createcollection (name,options)

Name: The names of the collections. Options, which are used to configure the parameters of the collection, are as follows

Autoindexid True/false (optional) If true, the default value for automatically creating an index _id field is false;

Capped True/false (optional) If true, the CAP collection is enabled. Up to its maximum value will overwrite the earliest entries;

A size (optional) specifies a maximum size byte cap set. Cap If true, you also need to specify this field. Unit B

Max (optional) specifies the maximum number of files allowed in the capping collection

MongoDB Data Management:

Db. Account.insert ({accountid:1,username: "123", Password: "123456"})//Create Collection

Db. Account.update ({accountid:1},{"$set": {"Age": 20}})//update

Show Collections//view collection, or use show tables

Db. Account.find ()//View all Documents

Db. Account.drop ()//Delete all documents, that is, delete the collection

Db.printcollectionstats ()//Then view the collection status

Db. Account.find ({accountid:1})//query by criteria

Db. Account.remove ({accountid:1})//delete by condition

Replica set
Earlier versions used a master one from, similar to MySQL, but slave in this architecture as read-only, after the main outage, from cannot automatically cut for the master;

Currently changed from master to replica set, for one master (primary) multi-slave (secondary, read-only), support with the weight switch from the main, this architecture can establish a quorum (arbiter) role, is only responsible for the decision, do not store data, this architecture read and write data in the Lord, If you implement load balancing, you need to manually specify the target server from the library.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/8A/98/wKiom1g0joniPC2rAAEzXeM1lBw627.png "title=" 4.png " Style= "width:750px;height:271px", "alt=" Wkiom1g0jonipc2raaezxem1lbw627.png "width=" vspace= "0" hspace= "0" Height = "271" border= "0"/>

Primary
Secondary
Secondary
192.168.1.1
192.168.1.2
192.168.1.3

Edit: vim/etc/mongod.conf # After restarting three machines

replication:# #oplog大小 oplogsizemb:20# #复制集名称 Replsetname:teng

Connect the Lord and run the command MONGO
> Use admin
> config={_id: "Teng", Members:[{_id:0,host: "192.168.1.1:27017"},{_id:1,host: "192.168.1.2:27017"},{_id:2,host: "192.168.1.3:27017"}]}
> rs.initiate (config)
> Rs.add ("192.168.1.2")
> Rs.add ("192.168.1.3")
Rs.status ()//View status
If two from the status of "Statestr": "STARTUP", you need to do the following
> var config=config={_id: "Teng", Members:[{_id:0,host: "192.168.1.1:27017"},{_id:1,host: "192.168.1.2:27017"},{_ Id:2,host: "192.168.1.3:27017"}]}

> rs.reconfig (config)

viewing Rs.status () again will find the state changed from secondary

The Lord built the library, built the collection
> Use MyDB
> Db.acc.insert ({accountid:1,username: "123", Password: "123456"})
> Show DBS
View from top
> Show DBS
If there is an error error:listdatabases failed:{"Note": "From ExecCommand", "OK": 0, "errmsg": "Not Master"}, you need to execute
> Rs.slaveok ()

Change the weight to mimic the main outage:

The default three machine weights are 1, if any one weight is set to higher than the other, then the machine immediately switch to primary role, so we preset three machine weights are: 1:3,2:2,3: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)
In this case, the second node will become the candidate Master node.
Lord Execute: Iptables-i input-p tcp--dport 27017-j DROP

MongoDB Backup Recovery

Backup specified library: mongodump-h ip-d dbname-o dir//-o after specifying where to back up, it is a directory

Back up all libraries: mongodump-h ip-o dir//Without database dbname, back up all libraries

Backup specified collection: mongodump-d mydb-c testc-o/tmp/testc//-c Specify collection name

Export the specified collection: mongoexport-d mydb-c testc-o/tmp/testc.json//-o followed by a file name.

Restore the specified library: mongorestore-d mydb dir///-d The name of the recovery library, dir is the directory where the library was backed up

Recover all libraries: Mongorestore--drop dir///--drop Optional, refers to the previous data before recovery, not recommended

Recovery collection: mongorestore-d mydb-c TESTC Dir/mydb/testc.bson//-C collection name, path to Bson file

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

-------------------------------------------------------------------------------------------------




This article is from the "North Ice--q" blog, please be sure to keep this source http://beibing.blog.51cto.com/10693373/1876007

NoSQL--Mongodb installation using replica set backup 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.