Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

[Mongodb] automatic failover of Replica set

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Replica set provides us with automatic failover, a mechanism operated by mongodb itself, which selects primary according to the priority of the slave library or the freshness of the data (that is, the latest node to synchronize data from the master database). When the previous primary is up, it will become a secondary and accept the log of the new primary.

Complete replica sets

Primary crashes

Mongodb will choose the next main database according to the freshness of the data.

Follow up the previous article, set up the replica set, and check the status of the two services with port 27018 27020:

[mongodb@rac4 bin] $. / mongo 127.0.0.1 purl 27018

MongoDB shell version: 2.0.1

Connecting to: 127.0.0.1:27018/test

PRIMARY > db.isMaster ()

{

"setName": "myset"

"ismaster": true,-- main library

"secondary": false

"hosts": [

"10.250.7.220 purl 27018"

"10.250.7.220 purl 27020"

"10.250.7.220 purl 27019"

]

"primary": "10.250.7.220 purl 27018"

"me": "10.250.7.220 purl 27018"

"maxBsonObjectSize": 16777216

"ok": 1

}

PRIMARY > exit

Bye

[mongodb@rac4 bin] $. / mongo 127.0.0.1 purl 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,-- slave library

"hosts": [

"10.250.7.220 purl 27020"

"10.250.7.220 purl 27019"

"10.250.7.220 purl 27018"

]

"primary": "10.250.7.220 purl 27018"

"me": "10.250.7.220 purl 27020"

"maxBsonObjectSize": 16777216

"ok": 1

}

PRIMARY > kill primary by hand

[root@rac4 ~] # ps-ef | grep 27018

Mongodb 14826 14794 1 20:24 pts/4 00:00:05 / mongod-- dbpath / opt/mongodata/r1-- port 27018-- replSet myset-- rest

Mongodb 14999 14430 0 20:28 pts/2 00:00:00. / mongo 127.0.0.1:27018

[root@rac4] # kill-9 14826 14794

[root@rac4 ~] # ps-ef | grep mongodb | grep-v root

Mongodb 14883 14853 1 20:26 pts/7 00:00:05 / mongod-- dbpath / opt/mongodata/r2-- port 27019-- replSet myset-- rest

Mongodb 14901 14548 1 20:27 pts/6 00:00:07 / mongod-- dbpath / opt/mongodata/r3-- port 27020-- replSet myset-- rest

Mongodb 14999 14430 0 20:28 pts/2 00:00:00. / mongo 127.0.0.1:27018

Mongodb 15102 15072 0 20:30 pts/5 00:00:00. / mongo 127.0.0.1:27019

Mongodb 15136 15106 020: 30 pts/8 00:00:00. / mongo 127.0.0.1:27020

[root@rac4 ~] #

The mongodb output log of port 27019 shows that 10.250.7.220 is selected as the log record of the main library.

Mon Oct 31 20:27:59 [FileAllocator] allocating new datafile / opt/mongodata/r2/local.2, filling with zeroes...

Mon Oct 31 20:27:59 [rsHealthPoll] replSet info member 10.250.7.220:27018 is up

Mon Oct 31 20:27:59 [rsHealthPoll] replSet member 10.250.7.220:27018 is now in state SECONDARY

Mon Oct 31 20:27:59 [rsHealthPoll] replSet info 10.250.7.220 is down (or slow to respond): still initializing

Mon Oct 31 20:27:59 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state DOWN

Mon Oct 31 20:28:01 [initandlisten] connection accepted from 10.250.7.220:10857 # 3

Mon Oct 31 20:28:05 [conn2] replSet RECOVERING

Mon Oct 31 20:28:05 [conn2] replSet info voting yea for 10.250.7.220 purl 27018 (0)

Mon Oct 31 20:28:07 [rsHealthPoll] replSet member 10.250.7.220:27018 is now in state PRIMARY

Mon Oct 31 20:28:09 [FileAllocator] done allocating datafile / opt/mongodata/r2/local.2, size: 1024MB, took 10.89 secs

Mon Oct 31 20:28:10 [rsSync] *

Mon Oct 31 20:28:10 [rsSync] replSet initial sync pending

Mon Oct 31 20:28:10 [rsSync] replSet syncing to: 10.250.7.220:27018

Mon Oct 31 20:28:10 [rsSync] build index local.me {_ id: 1}

Mon Oct 31 20:28:10 [rsSync] build index done 0 records 0.001 secs

Mon Oct 31 20:28:10 [rsSync] replSet initial sync drop all databases

Mon Oct 31 20:28:10 [rsSync] dropAllDatabasesExceptLocal 1

Mon Oct 31 20:28:10 [rsSync] replSet initial sync clone all databases

Mon Oct 31 20:28:10 [rsSync] replSet initial sync query minValid

Mon Oct 31 20:28:10 [rsSync] replSet initial oplog application from 10.250.7.220:27018 starting at Oct 31 20:27:53:1 to Oct 31 20:27:53:1

Mon Oct 31 20:28:13 [rsHealthPoll] replSet info member 10.250.7.220:27020 is up

Mon Oct 31 20:28:13 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state STARTUP2

Mon Oct 31 20:28:14 [rsSync] replSet initial sync finishing up

Mon Oct 31 20:28:14 [rsSync] replSet set minValid=4eae9449:1

Mon Oct 31 20:28:14 [rsSync] build index local.replset.minvalid {_ id: 1}

Mon Oct 31 20:28:14 [rsSync] build index done 0 records 0.005 secs

Mon Oct 31 20:28:14 [rsSync] replSet initial sync done

Mon Oct 31 20:28:15 [rsSync] replSet syncing to: 10.250.7.220:27018

Mon Oct 31 20:28:15 [rsSync] replSet SECONDARY

Mon Oct 31 20:28:15 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state RECOVERING

Mon Oct 31 20:28:26 [clientcursormon] mem (MB) res:16 virt:2677 mapped:1232

Mon Oct 31 20:28:52 [initandlisten] connection accepted from 10.250.7.220:10872 # 4

Mon Oct 31 20:28:52 [initandlisten] connection accepted from 10.250.7.220:10873 # 5

Mon Oct 31 20:28:52 [rsGhostSync] handshake between 2 and 10.250.7.220:27018

Mon Oct 31 20:28:53 [slaveTracking] build index local.slaves {_ id: 1}

Mon Oct 31 20:28:53 [slaveTracking] build index done 0 records 0.003 secs

Mon Oct 31 20:28:55 [conn5] end connection 10.250.7.220:10873

Mon Oct 31 20:28:55 [conn4] end connection 10.250.7.220:10872

Mon Oct 31 20:28:57 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state SECONDARY

Mon Oct 31 20:29:27 [clientcursormon] mem (MB) res:19 virt:2693 mapped:1232

Mon Oct 31 20:30:21 [initandlisten] connection accepted from 127.0.0.1:44672 # 6

Mon Oct 31 20:33:35 [conn2] end connection 10.250.7.220:42493

Mon Oct 31 20:33:35 [rsSync] replSet syncThread: 10278 dbclient error communicating with server: 10.250.7.220:27018

Mon Oct 31 20:33:35 [rsHealthPoll] DBClientCursor::init call () failed

Mon Oct 31 20:33:35 [rsHealthPoll] replSet info 10.250.7.220 replSet info 27018 is down (or slow to respond): DBClientBase::findN: transport error: 10.250.7.220 replSet info 27018 query: {replSetHeartbeat: "myset", v: 1, pv: 1, checkEmpty: false, from: "10.250.7.220frog 27019"}

Mon Oct 31 20:33:35 [rsHealthPoll] replSet member 10.250.7.220:27018 is now in state DOWN

Mon Oct 31 20:33:35 [rsMgr] not electing self, 10.250.7.220:27020 would veto

Mon Oct 31 20:33:36 [conn3] replSet info voting yea for 10.250.7.220 purl 27020 (2)

Mon Oct 31 20:33:37 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state PRIMARY

Mon Oct 31 20:33:46 [rsSync] replSet syncing to: 10.250.7.220:27020

Mon Oct 31 20:34:27 [clientcursormon] mem (MB) res:19 virt:2693 mapped:1232

The mongodb output log of port 27020 shows that 10.250.7.220 is selected as the log record of the main library.

Mon Oct 31 20:33:35 [rsSync] replSet syncThread: 10278 dbclient error communicating with server: 10.250.7.220:27018

Mon Oct 31 20:33:36 [rsHealthPoll] DBClientCursor::init call () failed

Mon Oct 31 20:33:36 [rsHealthPoll] replSet info 10.250.7.220 replSet info 27018 is down (or slow to respond): DBClientBase::findN: transport error: 10.250.7.220 replSet info 27018 query: {replSetHeartbeat: "myset", v: 1, pv: 1, checkEmpty: false, from: "10.250.7.22020 replSet info 27020"}

Mon Oct 31 20:33:36 [rsHealthPoll] replSet member 10.250.7.220:27018 is now in state DOWN

Mon Oct 31 20:33:36 [rsMgr] replSet info electSelf 2

Mon Oct 31 20:33:36 [rsMgr] replSet PRIMARY

Mon Oct 31 20:33:46 [initandlisten] connection accepted from 10.250.7.220:37261 # 5

Mon Oct 31 20:33:47 [slaveTracking] build index local.slaves {_ id: 1}

Mon Oct 31 20:33:47 [slaveTracking] build index done 0 records 0.001 secs

Mon Oct 31 20:33:48 [clientcursormon] mem (MB) res:19 virt:2692 mapped:1232

Mon Oct 31 20:34:35 [conn4] end connection 127.0.0.1:17500

Mon Oct 31 20:34:37 [initandlisten] connection accepted from 127.0.0.1:36525 # 6

Enter the database to view:

[mongodb@rac4 bin] $. / mongo 127.0.0.1 purl 27020

MongoDB shell version: 2.0.1

Connecting to: 127.0.0.1:27020/test

PRIMARY >

PRIMARY >

PRIMARY > db.isMaster ()

{

"setName": "myset"

"ismaster": true,-- becomes the main library master

"secondary": false

"hosts": [

"10.250.7.220 purl 27020"

"10.250.7.220 purl 27019"

"10.250.7.220 purl 27018"

]

"primary": "10.250.7.220 purl 27020"

"me": "10.250.7.220 purl 27020"

"maxBsonObjectSize": 16777216

"ok": 1

}

PRIMARY >

Restart the database service of mongodb at port 27018: the record of its recovery operation can be seen from the log

[mongodb@rac4 bin] $. / mongod-dbpath / opt/mongodata/r1-port 27018-rest-replSet myset &

[1] 16290

[mongodb@rac4 bin] $Mon Oct 31 20:48:32 [initandlisten] MongoDB starting: pid=16290 port=27018 dbpath=/opt/mongodata/r1 64-bit host=rac4

Mon Oct 31 20:48:32 [initandlisten] db version v2.0.1, pdfile version 4.5

Mon Oct 31 20:48:32 [initandlisten] git version: 3a5cf0e2134a830d38d2d1aae7e88cac31bdd684

Mon Oct 31 20:48:32 [initandlisten] build info: Linux bs-linux64.10gen.cc 2.6.21.7-2.ec2.v1.2.fc8xen # 1 SMP Fri Nov 20 17:48:28 EST 2009 x86 million 64 BOOST_LIB_VERSION=1_41

Mon Oct 31 20:48:32 [initandlisten] options: {dbpath: "/ opt/mongodata/r1", port: 27018, replSet: "myset", rest: true}

Mon Oct 31 20:48:32 [initandlisten] journal dir=/opt/mongodata/r1/journal

Mon Oct 31 20:48:32 [initandlisten] recover begin

Mon Oct 31 20:48:32 [initandlisten] recover lsn: 231055

Mon Oct 31 20:48:32 [initandlisten] recover / opt/mongodata/r1/journal/j._0

Mon Oct 31 20:48:32 [initandlisten] recover skipping application of section seq:198962

< lsn:231055 Mon Oct 31 20:48:32 [initandlisten] recover cleaning up Mon Oct 31 20:48:32 [initandlisten] removeJournalFiles Mon Oct 31 20:48:32 [initandlisten] recover done Mon Oct 31 20:48:32 [initandlisten] waiting for connections on port 27018 Mon Oct 31 20:48:32 [websvr] admin web console waiting for connections on port 28018 Mon Oct 31 20:48:32 [initandlisten] connection accepted from 127.0.0.1:11930 #1 Mon Oct 31 20:48:32 [rsStart] replSet STARTUP2 Mon Oct 31 20:48:32 [rsHealthPoll] replSet info member 10.250.7.220:27019 is up Mon Oct 31 20:48:32 [rsHealthPoll] replSet member 10.250.7.220:27019 is now in state SECONDARY Mon Oct 31 20:48:32 [rsHealthPoll] replSet info member 10.250.7.220:27020 is up Mon Oct 31 20:48:32 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state PRIMARY Mon Oct 31 20:48:32 [rsSync] replSet SECONDARY Mon Oct 31 20:48:33 [initandlisten] connection accepted from 10.250.7.220:35971 #2 Mon Oct 31 20:48:34 [initandlisten] connection accepted from 10.250.7.220:35972 #3 Mon Oct 31 20:48:36 [rsSync] replSet syncing to: 10.250.7.220:27020 Mon Oct 31 20:48:36 [rsSync] build index local.me { _id: 1 } Mon Oct 31 20:48:36 [rsSync] build index done 0 records 0 secs [mongodb@rac4 bin]$ ./mongo 127.0.0.1:27018 MongoDB shell version: 2.0.1 connecting to: 127.0.0.1:27018/test SECONDARY>

SECONDARY > db.isMaster ()

{

"setName": "myset"

"ismaster": false,-- the database service with port 27018 becomes a slave library

"secondary": true

"hosts": [

"10.250.7.220 purl 27018"

"10.250.7.220 purl 27020"

"10.250.7.220 purl 27019"

]

"primary": "10.250.7.220 purl 27020"

"me": "10.250.7.220 purl 27018"

"maxBsonObjectSize": 16777216

"ok": 1

}

SECONDARY >

2.JPG

3.JPG

4.JPG

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report