MongoDB master-slave copy and replica set

Source: Internet
Author: User

MongoDB Two features master-slave replication and replica set for synchronous backup of data

First, master-slave replication

Master-slave replication is a simple database synchronous backup of the cluster technology . For example, the primary server is down, can be used directly from the server, the master server after the recovery in sync, to ensure the continuity of business

Note the point:

    • The primary server has only one
    • From the server needs to know its own data source, that is, the corresponding master server
    • --master determine the primary server,--slave, and--source to control the slave server

1. For example:

1). The port of the primary server is 60000

2). The port from the server is 60001

How do I configure Master-slave replication?

Configuration of the primary server:

DBPath = D:\MongoDBMSCopy\Master Primary Database address

Port = 60000 Primary Database port number

BIND_IP = 127.0.0.1 server where the primary database resides

Master = True to make sure I am the primary server

Configuration from the server

DBPath = D:\MongoDBMSCopy\Slave from the database address

Port = 60001 from the database port number

Bind_ip = 127.0.0.1 from the server where the database resides

Source = 127.0.0.1:60000 Determine my database port

Slave = True to determine that you are from the server

After the configuration file is written, you can start the master-slave server by command.

As follows:

Start the primary server

C:\windows\system32>mongod--config D:\MongoDBMSCopy\Master\Master.conf

Start from server

C:\windows\system32>mongod--config D:\MongoDBMSCopy\Slave\slave.conf

Verify that replication is in progress

① open cmd, connect to the master server, and build a students database, insert a document named students

Look at the data

> Db.students.find ()

{"_id": 1, "name": "Bigorange", "Age": 25}

② Open cmd connection from server

As shown, the proof has been replicated

2. Other settings for master-slave replication

--only from node → Specify to replicate a database , by default, all databases are replicated

--slavedelay The delay (in seconds ) of synchronizing data from the node → setting the primary database

--fastsync from node → node snapshot of primary database for node start from database

--autoresync from node → new sync database if out of sync

--oplogsize main node → set the size of the Oplog ( master operation record stored in local oplog )

3. dynamically add and remove slave nodes using the shell

There is one more sources document in the local database from the database , which holds the information of the master server

How to add dynamically from a node, as with normal add data, insert Remove directly through insert

To hook up the master node :

Db.sources.insert ({"Host": "127.0.0.1:60000"})

To delete a master node that has been hooked up :

Db.sources.remove ({"Host": "127.0.0.1:60000"})

Second, replica set

The first chart, a is active B and C is for backup

In the second picture , when a fails , the cluster elects B as an active database based on the weighting algorithm .

The third picture when A is restored, he automatically becomes the backup database .

Example: How to configure a replica set server cluster

1. If there are three servers above ABC, how to configure it, also need to modify the configuration file

For A server:

DBPath = D:\MongoDBCopy\A\MongoDB_A.conf #A的配置

port=11111 #端口号

bind_ip=127.0.0.1 #IP地址

Replset = child/127.0.0.1:22222 #设定同伴, this is B.

For b servers:

DBPath = D:\MongoDBCopy\B\MongoDB_B.conf #A的配置

port=22222 #端口号

bind_ip=127.0.0.1 #IP地址

Replset = child/127.0.0.1:33333 #设定同伴, here is C

For C servers:

DBPath = D:\MongoDBCopy\C\MongoDB_C.conf #A的配置

port=33333 #端口号

bind_ip=127.0.0.1 #IP地址

Replset = child/127.0.0.1:11111 #设定同伴, this is a

2. initializing a replica set

Use the shell to enter a server, and then set the initialization replica set

Use admin db.runcommand ({"replsetinitiate":
{ "_id":' Child', " Members":[{ "_id":1, "Host":"127.0.0.1:11111" },{ "_id":2, "Host":"127.0.0.1:22222" },{ "_id":3, "Host":"127.0.0.1:33333" }] }
})

Which node becomes the active node, this time is random, if you want more accurate settings, you need to set the weight

3. view replica set status

Rs.status ()

As you can see, the server with Port 11111 is selected as the Active server

Attention:

Backup points cannot be queried, only active points for query operations

① add a database to the Active server and add a document

Child:primary> Show DBS

Admin (empty)

Local 4.076GB

Child:primary> Use Students

Switched to DB Students

Child:primary> Db.student.insert ({name: "Bigorange", age:25})

Writeresult ({"ninserted": 1})

Child:primary> Db.student.find ()

{"_id": ObjectId ("56fa6b9159b5751566c1c37f"), "name": "Bigorange", "Age": 25

}

Child:primary>

② Switch to one of the backup servers and find that the query cannot be made

C:\windows\system32>mongo 127.0.0.1:22222/test

MongoDB Shell version:2.6.5

Connecting To:127.0.0.1:22222/test

Child:secondary> Show DBS

Students 0.078GB

Admin (empty)

Local 2.077GB

Child:secondary> Use Students

Switched to DB Students

Child:secondary> Show Collections

2016-03-29t19:50:09.064+0800 error: {"$err": "Not Master and Slaveok=false", "

Code ": 13435} at src/mongo/shell/query.js:131

Child:secondary> Db.student.find ()

Error: {"$err": "Not Master and Slaveok=false", "Code": 13435}

Child:secondary>

Add:

node and initialization advanced parameters

Standard Regular node : participating in a poll can become an active node

Passive replica node : participates in voting , but cannot be an active node

Arbiter Quorum node : Just participate in the poll do not replicate nodes and cannot become active nodes

Advanced parameters

Priority 0 to 0 represents a replica node , and 1 to + is a regular node

Arbiteronly:true Quorum node

Usage

Set the above a to the quorum node

The rest of the same, when initialized, is written as follows

Use Admindb.runcommand ({"replsetinitiate":{       "_id":' Child',       " Members":[{        "_id":1,        "Host":"127.0.0.1:11111", Arbiteronly:true    },{        "_id":2,        "Host":"127.0.0.1:22222",    },{        "_id":3,        "Host":"127.0.0.1:33333"    }]  }})

Other uses can refer to the following

http://blog.itpub.net/29254281/viewspace-1176553/

4. read/write detach operation → Extended read

Setting up read and write separations

Slaveokay:true

MongoDB master-slave replication and replica set

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.