MONGODDB Study Notes (ii) (copy)

Source: Internet
Author: User
Tags db2

MongoDB replication is very easy to configure, in fact, there are two ways, one is a master-slave replication, one is a copy set, the former benefits are easy to configure, but if the primary server is down, the entire system crashes, the latter advantage is that if the primary server is down, the other server will vote to select a master server, The difference between the so-called master-slave server is that the primary server can be written to, and the default connection from the server is not supported unless Slaveok is set.

1, master-slave replication

1) environment

Role Data location Port
Primary server D:\mongodb_data\db1 27017
From the server D:\mongodb_data\db2 27018
From the server D:\mongodb_data\db3 27019

2) Start (empty the directory where the corresponding data is located)

D:\mongodb_data>mongod--dbpath d:\mongodb_data\db1--port=27017--master

D:\mongodb_data>mongod--dbpath=d:\mongodb_data\db2--port 27018--slave--source 192.168.1.223:27017

The parameters here are easy to understand,--master represents the primary server,--slave represents from the server,--source represents the source of replication

3) master server

D:\mongodb_data>mongo 192.168.1.223:27017
MongoDB Shell version:3.0.6
Connecting To:192.168.1.223:27017/test
> for (var i=0;i<10000;i++) {
... db.students.insert ({name: ' ss ' +i,code: ' A ' +i,id:i});
... }
Writeresult ({"ninserted": 1})
> Db.students.count ()
10000

4) from the server

D:\mongodb_data>mongo 192.168.1.223:27018
MongoDB Shell version:3.0.6
Connecting To:192.168.1.223:27018/test
> Db.students.count ()
10000

You can see that replication is successful.

5) Add one from the server, but do not specify the source

D:\mongodb_data>mongod--dbpath d:\mongodb_data\db3--port 27019--slave

You can see in the log that you did not specify a replication source

D:\mongodb_data>mongo 192.168.1.223:27019
MongoDB Shell version:3.0.6
Connecting To:192.168.1.223:27019/test
> Db.students.count ()
0
> Use Local
Switched to DB Local
> Db.sources.insert ({host: ' 192.168.1.223:27017 '});
Writeresult ({"ninserted": 1})
> Use test
Switched to DB test
> Db.students.count ()
10000

You can see that after joining the source of replication, it soon syncs.

This kind of replication is very simple, but not strong enough, if you turn off the primary server, the server will receive the following information

2015-09-30t17:13:39.073+0800 W NETWORK [Replslave] Failed to connect to 192.168.1.223:27017, reason:errno:10061 due to the target computer product Very rejected, unable to connect.
2015-09-30t17:13:39.074+0800 I REPL [replslave] repl:couldn ' t connect to server 192.168.1.223:27017 (192.168.1.223), con Nection Attempt failed

2. Copy set

1) environment

Role Data location Port
Server 1 D:\mongodb_data\db1 27017
Server 2 D:\mongodb_data\db2 27018
Server 3 D:\mongodb_data\db3 27019
Arbiter Server D:\mongodb_data\db4 27020

2) Start the server (the name of the replica set is Testset, clear the DB1,DB2,DB3 directory)

D:\mongodb_data>mongod--dbpath db1--port 27017--replset testset

D:\mongodb_data>mongod--dbpath DB2--port 27018--replset Testset

D:\mongodb_data>mongod--dbpath db3--port 27019--replset testset

3) connection and configuration

D:\mongodb_data>mongo 192.168.1.223:27017MongoDB Shell version:3.0.6Connecting to:192.168.1.223:27017/test>rs.initiate () {"Info2": "No configuration explicitly specified--making one",        "Me": "desktop-3b9n9sb:27017",        "OK": 1}testset:secondary>Rs.status () {"Set": "Testset",        "Date": Isodate ("2015-09-30t09:25:42.449z"),        "MyState": 1,        "Members" : [                {                        "_id": 0,                        "Name": "desktop-3b9n9sb:27017",                        "Health": 1,                        "State": 1,                        "Statestr": "PRIMARY",                        "Uptime": 162,                        "Optime": Timestamp (1443605129, 1),                        "Optimedate": Isodate ("2015-09-30t09:25:29z"),                        "Electiontime": Timestamp (1443605129, 2),                        "Electiondate": Isodate ("2015-09-30t09:25:29z"),                        "ConfigVersion": 1,                        "Self":true                }        ],        "OK": 1}testset:primary> Rs.add (' 192.168.1.223:27018 '); { "OK": 1}testset:primary> Rs.add (' 192.168.1.223:27019 '); { "OK": 1}testset:primary>Rs.status () {"Set": "Testset",        "Date": Isodate ("2015-09-30t09:26:12.584z"),        "MyState": 1,        "Members" : [                {                        "_id": 0,                        "Name": "desktop-3b9n9sb:27017",                        "Health": 1,                        "State": 1,                        "Statestr": "PRIMARY",                        "Uptime": 192,                        "Optime": Timestamp (1443605169, 1),                        "Optimedate": Isodate ("2015-09-30t09:26:09z"),                        "Electiontime": Timestamp (1443605129, 2),                        "Electiondate": Isodate ("2015-09-30t09:25:29z"),                        "ConfigVersion": 3,                        "Self":true                },                {                        "_ID": 1,                        "Name": "192.168.1.223:27018",                        "Health": 1,                        "State": 2,                        "Statestr": "Secondary",                        "Uptime": 6,                        "Optime": Timestamp (1443605169, 1),                        "Optimedate": Isodate ("2015-09-30t09:26:09z"),                        "Lastheartbeat": Isodate ("2015-09-30t09:26:11.742z"),                        "Lastheartbeatrecv": Isodate ("2015-09-30t09:26:12.447z"),                        "Pingms": 0,                        "Syncingto": "desktop-3b9n9sb:27017",                        "ConfigVersion": 3                },                {                        "_ID": 2,                        "Name": "192.168.1.223:27019",                        "Health": 1,                        "State": 5,                        "Statestr": "STARTUP2",                        "Uptime": 2,                        "Optime": Timestamp (0, 0),                        "Optimedate": Isodate ("1970-01-01t00:00:00z"),                        "Lastheartbeat": Isodate ("2015-09-30t09:26:11.755z"),                        "Lastheartbeatrecv": Isodate ("2015-09-30t09:26:11.782z"),                        "Pingms": 12,                        "ConfigVersion": 3                }        ],        "OK": 1}

Note that the first to run Rs.initiate (), and then to run Rs.add () to increase the replication server, with Rs.status () can see the state, can be seen 27017 is primary, the others are secondary

3) Verification

Insert in 27017

 for (var i=0;i<10000;i++) {... db.students.insert ({name:' ss ' +i,code: ' AA ' +' ninserted ': 1 }) testset:primary> db.students.count (); 10000
 d:\mongodb_data>mongo 192.168.1.223:27018 MongoDB Shell version:  3.0.6connecting to:  192.168.1.223:27018/ Testtestset:secondary> Db.students.count ()  2015-09-30t17:31:11.883+0800 E QUERY Error:count failed: {"note": "from execcommand ", "OK": 0, "errmsg": "No" T master "" at Error ( <anonymous>/mongo/shell/query.js:326:11) at Dbcollection.count (src/mongo/shell/collection.js:1046:27) at ( Shell): 1:13 at Src/mongo/shell/query.js:326testset:secondary> Rs.slaveok () Testset:secondary  > Db.students.count ()  10000  
D:\mongodb_data>mongo 192.168.1.223:270193.0.6192.168.1.223:27019/testtestset:secondary > rs.slaveok () testset:secondary> db.students.count ()10000

MONGODDB Study Notes (ii) (copy)

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.