"MongoDB" How to create MongoDB's replica set

Source: Internet
Author: User
Tags create mongodb log log

Replica sets extension on master-slave replication, adding automatic failover and automatic repair of member nodes. The following is a technical introduction to how to build MongoDB replica set (personally feel that building MongoDB itself is not much dry, how important is disaster planning )

1 Setting up a data storage directory for replication cluster nodes

Mkdir-p/OPT/MONGODATA/R1

Mkdir-p/OPT/MONGODATA/R2

Mkdir-p/OPT/MONGODATA/R3

2 perform the following commands in three Windows, respectively:

./mongod--dbpath/opt/mongodata/r1--port 27018--rest--replset MySet

./mongod--dbpath/opt/mongodata/r2--port 27019--rest--replset MySet

./mongod--dbpath/opt/mongodata/r3--port 27020--rest--replset MySet

3 Execute the following command in the fourth window:

[Email protected] bin]$ /mongo 127.0.0.1:27018 init.js

MongoDB Shell version:2.0.1

Connecting To:127.0.0.1:27018/test

Init.js content is as follows:

[email protected] bin]$ cat Init.js

Rs.initiate ({

_id: "MySet",

Members: [

{_id:0, Host: "10.250.7.220:27018"},

{_id:1, Host: "10.250.7.220:27019"},

{_id:2, Host: "10.250.7.220:27020"}

]

})

Start 3 service nodes, from the log log can be seen, three nodes negotiate with each other, select a node with Port 27018 as primary, and two automatically as the secondary node.

Mon Oct 20:27:53 [conn2] Replset info Saving a newer config version to Local.system.replset

Mon Oct to 20:27:53 [conn2] replset saveconfiglocally Done

Mon Oct to 20:27:53 [conn2] replset replsetinitiate config now saved locally. Should come online in about a minute.

Mon Oct to 20:27:53 [conn2] command admin. $cmd command: {replsetinitiate: {_id: "MySet", Members: [{_id:0.0, Host: "1 0.250.7.220:27018 "}, {_id:1.0, Host:" 10.250.7.220:27019 "}, {_id:2.0, Host:" 10.250.7.220:27020 "}]}} ntoreturn:1 reslen:112 12095ms

Mon Oct to 20:27:53 [conn2] End Connection 127.0.0.1:15252

Mon Oct to 20:27:53 [Rsstart] Replset STARTUP2

Mon Oct 20:27:53 [rshealthpoll] replset info 10.250.7.220:27019 is down (or slow to respond): Still initializing

Mon Oct to 20:27:53 [Rshealthpoll] Replset member 10.250.7.220:27019 is now on state down

Mon Oct 20:27:53 [rshealthpoll] replset info 10.250.7.220:27020 is down (or slow to respond): Still initializing

Mon Oct to 20:27:53 [Rshealthpoll] Replset member 10.250.7.220:27020 is now on state down

Mon Oct to 20:27:53 [Rssync] Replset Secondary

Mon Oct to 20:27:55 [Initandlisten] connection accepted from 10.250.7.220:44134 #3

Mon Oct to 20:27:59 [Rshealthpoll] Replset info member 10.250.7.220:27019 is up

Mon Oct to 20:27:59 [Rshealthpoll] Replset member 10.250.7.220:27019 is now on state STARTUP2

Mon Oct to 20:27:59 [rsmgr] not electing self, 10.250.7.220:27019 would veto

Mon Oct to 20:28:01 [Initandlisten] connection accepted from 10.250.7.220:44137 #4

Mon Oct to 20:28:05 [rsmgr] Replset info electself 0

Mon Oct to 20:28:05 [Rsmgr] Replset PRIMARY

Mon Oct to 20:28:07 [Rshealthpoll] Replset member 10.250.7.220:27019 is now on state RECOVERING

Mon Oct to 20:28:10 [Initandlisten] connection accepted from 10.250.7.220:44141 #5

Mon Oct to 20:28:10 [Initandlisten] connection accepted from 10.250.7.220:44142 #6

Mon Oct to 20:28:11 [slavetracking] Build index local.slaves {_id:1}

Mon Oct to 20:28:11 [slavetracking] Build index done 0 records 0.001 secs

Mon Oct to 20:28:13 [Rshealthpoll] Replset info member 10.250.7.220:27020 is up

Mon Oct to 20:28:13 [Rshealthpoll] Replset member 10.250.7.220:27020 is now on state STARTUP2

Mon Oct to 20:28:14 [conn6] End Connection 10.250.7.220:44142

Mon Oct to 20:28:14 [conn5] End Connection 10.250.7.220:44141

Mon Oct to 20:28:15 [Initandlisten] connection accepted from 10.250.7.220:44144 #7

Mon Oct to 20:28:15 [Rshealthpoll] Replset member 10.250.7.220:27019 is now on state secondary

Mon Oct to 20:28:15 [Rshealthpoll] Replset member 10.250.7.220:27020 is now on state RECOVERING

Mon Oct to 20:28:28 [Initandlisten] connection accepted from 127.0.0.1:59232 #8

To enter the database from the client:

[Email protected] bin]$/mongo 127.0.0.1:27018

MongoDB Shell version:2.0.1

Connecting To:127.0.0.1:27018/test

Primary> Rs.status ()--View replica set status command

{

"Set": "MySet",

"Date": Isodate ("2011-10-31t12:29:17z"),

"MyState": 1,

"Members": [

{

"_id": 0,

"Name": "10.250.7.220:27018",

"Health": 1,

"state": 1,

"Statestr": "PRIMARY",

"Optime": {

"T": 1320064073000,

"I": 1

},

"Optimedate": Isodate ("2011-10-31t12:27:53z"),

"Self": true

},

{

"_id": 1,

"Name": "10.250.7.220:27019",

"Health": 1,

"state": 2,

"Statestr": "Secondary",

"Uptime": 78,

"Optime": {

"T": 1320064073000,

"I": 1

},

"Optimedate": Isodate ("2011-10-31t12:27:53z"),

"Lastheartbeat": Isodate ("2011-10-31t12:29:16z"),

"Pingms": 0

},

{

"_id": 2,

"Name": "10.250.7.220:27020",

"Health": 1,

"state": 2,

"Statestr": "Secondary",

"Uptime": 64,

"Optime": {

"T": 1320064073000,

"I": 1

},

"Optimedate": Isodate ("2011-10-31t12:27:53z"),

"Lastheartbeat": Isodate ("2011-10-31t12:29:16z"),

"Pingms": 1

}

],

"OK": 1

}

Key data bits in status

State:1 indicates that the host is currently capable of reading and writing, 2: cannot read and write

Health:1 indicates that the host is currently normal, 0: exception

The following is a read test from the library, error: {"$err": "Not Master and Slaveok=false", "Code": 13435}

Primary> rs.conf ()--View commands for replica set configuration

{

"_id": "MySet",

"Version": 1,

"Members": [

{

"_id": 0,

"Host": "10.250.7.220:27018"

},

{

"_id": 1,

"Host": "10.250.7.220:27019"

},

{

"_id": 2,

"Host": "10.250.7.220:27020"

}

]

}

Primary> Db.ismaster ();--To see if the command is the main library, you can certainly see it from the prompt PRIMARY

{

"SetName": "MySet",

"IsMaster": True, # #是primary

"Secondary": false,

"Hosts": [

"10.250.7.220:27018",

"10.250.7.220:27020",

"10.250.7.220:27019"

],

"PRIMARY": "10.250.7.220:27018",

"Me": "10.250.7.220:27018",

"Maxbsonobjectsize": 16777216,

"OK": 1

}

Log in to the other two MongoDB services separately:

[Email protected] bin]$ /mongo 127.0.0.1:27019

MongoDB Shell version:2.0.1

Connecting To:127.0.0.1:27019/test

Secondary>

Secondary>

Secondary> Db.ismaster ();

{

"SetName": "MySet",

"IsMaster": false,

"Secondary": true,

"Hosts": [

"10.250.7.220:27019",

"10.250.7.220:27020",

"10.250.7.220:27018"

],

"PRIMARY": "10.250.7.220:27018",

"Me": "10.250.7.220:27019",

"Maxbsonobjectsize": 16777216,

"OK": 1

}

Secondary>

[Email protected] bin]$ /mongo 127.0.0.1:27020

MongoDB Shell version:2.0.1

Connecting To:127.0.0.1:27020/test

Secondary>

Secondary> Db.ismaster ();

{

"SetName": "MySet",

"IsMaster": false,

"Secondary": true,

"Hosts": [

"10.250.7.220:27020",

"10.250.7.220:27019",

"10.250.7.220:27018"

],

"PRIMARY": "10.250.7.220:27018",

"Me": "10.250.7.220:27020",

"Maxbsonobjectsize": 16777216,

"OK": 1

}

This is the building of success.

Write to the main library and view it from the library:

[Email protected] bin]$ /mongo 127.0.0.1:27020

MongoDB Shell version:2.0.1

Connecting To:127.0.0.1:27020/test

primary> Use test

Switched to DB test

Primary>

Primary> Db.yql.insert ({val: "This was a message on 27020 PRIMARY!"});

Primary>

Main Library log:

Mon Oct to 21:03:46 [Fileallocator] allocating new datafile/opt/mongodata/r3/test.ns, filling with zeroes ...

Mon Oct to 21:03:46 [Fileallocator] Done allocating datafile/opt/mongodata/r3/test.ns, SIZE:16MB, took 0.256 secs

Mon Oct to 21:03:46 [Fileallocator] allocating new datafile/opt/mongodata/r3/test.0, filling with zeroes ...

Mon Oct to 21:03:48 [Clientcursormon] Mem (MB) res:35 virt:2726 mapped:1248

Mon Oct to 21:03:50 [Fileallocator] Done allocating datafile/opt/mongodata/r3/test.0, SIZE:64MB, took 4.488 secs

Mon Oct to 21:03:50 [conn6] Build index TEST.YQL {_id:1}

Mon Oct to 21:03:50 [conn6] Build index done 0 records 0 secs

Mon Oct to 21:03:50 [conn6] Insert TEST.YQL 4759ms

Mon Oct to 21:03:50 [Fileallocator] allocating new datafile/opt/mongodata/r3/test.1, filling with zeroes ...

Mon Oct to 21:03:51 [CONN8] getmore local.oplog.rs query: {ts: {$gte: New Date (5669632022159556609)}} cursorid:6257712 144272734285 nreturned:1 reslen:146 5031ms

Mon Oct to 21:03:51 [conn5] getmore local.oplog.rs query: {ts: {$gte: New Date (5669632022159556609)}} cursorid:4238780 80662643430 nreturned:1 reslen:146 5631ms

Mon Oct to 21:03:54 [Fileallocator] Done allocating datafile/opt/mongodata/r3/test.1, SIZE:128MB, took 3.818 secs

From the library log, you can see the process of applying logs from the library, copying the data files from the main library.

Mon Oct to 20:49:27 [Clientcursormon] Mem (MB) res:19 virt:2693 mapped:1232

Mon Oct to 20:54:27 [Clientcursormon] Mem (MB) res:19 virt:2693 mapped:1232

Mon Oct to 20:59:27 [Clientcursormon] Mem (MB) res:19 virt:2693 mapped:1232

Mon Oct to 21:03:51 [Fileallocator] allocating new datafile/opt/mongodata/r2/test.ns, filling with zeroes ...

Mon Oct to 21:03:54 [Fileallocator] Done allocating datafile/opt/mongodata/r2/test.ns, SIZE:16MB, took 3.396 secs

Mon Oct to 21:03:54 [Fileallocator] allocating new datafile/opt/mongodata/r2/test.0, filling with zeroes ...

Mon Oct to 21:04:00 [Fileallocator] Done allocating datafile/opt/mongodata/r2/test.0, SIZE:64MB, took 5.79 secs

Mon Oct to 21:04:00 [Rssync] Build index TEST.YQL {_id:1}

Mon Oct to 21:04:00 [Rssync] Build index done 0 records 0 secs

Mon Oct to 21:04:00 [Fileallocator] allocating new datafile/opt/mongodata/r2/test.1, filling with zeroes ...

Mon Oct to 21:04:03 [Fileallocator] Done allocating datafile/opt/mongodata/r2/test.1, SIZE:128MB, took 2.965 secs

Mon Oct to 21:04:37 [Clientcursormon] Mem (MB) res:17 virt:2853 mapped:1312

Mon Oct to 21:04:41 [conn6] End Connection 127.0.0.1:44672

As described earlier in Rs.status (), state:1 indicates that the host is currently writable, 2: Cannot read and write

{

"_id": 1,

"Name": "10.250.7.220:27019",

"Health": 1,

"state": 2,--from the state of the library to 2, at this time is not read and write.

"Statestr": "Secondary",

"Uptime": 78,

"Optime": {

"T": 1320064073000,

"I": 1

},

When reading from the library, an error will be made.

[Email protected] bin]$/mongo 127.0.0.1:27019

MongoDB Shell version:2.0.1

Connecting To:127.0.0.1:27019/test

secondary> Use test

Switched to DB test

Secondary> Db.yql.find ();

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



This article is from the "Dream to Reality" blog, please be sure to keep this source http://lookingdream.blog.51cto.com/5177800/1795016

"MongoDB" How to create MongoDB's 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.