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

Replica sets Architecture replication set (2) detailed explanation

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Following the above, we have successfully built it. Here is a detailed interpretation of the log and so on.

We can view some basic information of the operation log, such as size, log startup time, etc., and view the meta-information of primary's oplog.rs table by executing the "db.printCollectionStats ()" command. The code is as follows:

PRIMARY > db.printCollectionStats ()

Oplog.rs

{

"ns": "local.oplog.rs"

"count": 1

"size": 80

"avgObjSize": 80

"storageSize": 990003200

"numExtents": 1

"nindexes": 0

"lastExtentSize": 990003200

"paddingFactor": 1

"flags": 0

"totalIndexSize": 0

"indexSizes": {

}

"capped": 1

"max": 2147483647

"ok": 1

}

-

System.replset

{

"ns": "local.system.replset"

"count": 1

"size": 176

"avgObjSize": 176

"storageSize": 12288

"numExtents": 1

"nindexes": 0

"lastExtentSize": 12288

"paddingFactor": 1

"flags": 0

"totalIndexSize": 0

"indexSizes": {

}

"ok": 1

}

-

PRIMARY >

After the replication set is set up, the first thing you need to do is to check the synchronization status of the replication set, because if the replication set data is not synchronized, the replication set of mongodb will not have any effect. Check all slave latency by executing the "db.printSlaveReplicationInfo ()" command, such as the synchronization status of slave on port 28011. The code is as follows:

PRIMARY > db.printSlaveReplicationInfo ()

Source: localhost:28011

SyncedTo: Fri Jul 25 2014 10:03:17 GMT+0800 (CST)

= 3437 secs ago (0.95hrs)

Source: localhost:28012

SyncedTo: Fri Jul 25 2014 10:03:17 GMT+0800 (CST)

= 3437 secs ago (0.95hrs)

PRIMARY >

Field description:

1, source: IP and port of slave library

2SyncedTo: information such as the current synchronization situation and the time of the last synchronization.

Master-slave configuration information

PRIMARY > db

Local

PRIMARY > show collections

Oplog.rs

System.replset

PRIMARY > db.system.replset.find ()

{"_ id": "rs1", "version": 1, "members": [

{"_ id": 0, "host": "localhost:28010"}

{"_ id": 1, "host": "localhost:28011"}

{"_ id": 2, "host": "localhost:28012"}]

}

PRIMARY >

In this case, the field ID stores the name of the replication set, the value in this case is rs1, and the field "members" stores the IP and port information of the members of the replication set.

Manage Replica Sets

1, master-slave switch

Mongodb replication set test environment

Port current member role target member role

28010 Master and Slave

28011 from

28012 from the master

In this example, port 28010 corresponds to the main library. Now you need to put the main library on port 28012. Let's guide you to complete the configuration step by step.

Step 1: except for the existing primary instance (port 28010) and the target primary instance (port 28012), all the other instance groups are set to "freeze" state, that is, (non-primary state)

In this example, port 28011 is "frozen". The code is as follows

PRIMARY > exit-quit and switch to our 27011

Bye

[root@dota] # / usr/local/mongodb/bin/mongo-- port 28011

MongoDB shell version: 2.0.4

Connecting to: 127.0.0.1:28011/test

SECONDARY > rs.freeze (30)

{"ok": 1}

SECONDARY >

By executing the "rs.freeze (30)" command, the instance with port on 28011 is "frozen", where 30 is in seconds, indicating that the instance will not participate in the internal election of primary within 30 seconds, that is, the instance will not become a primary role within 30 seconds, then make full use of the 30 seconds to complete the switching work.

Step 2, downgrade the instance of the current main library

Downgrade the main library instance with port 28010 as follows:

[root@dota] # / usr/local/mongodb/bin/mongo-- port 28010

MongoDB shell version: 2.0.4

Connecting to: 127.0.0.1:28010/test

PRIMARY > rs.stepDown ()

Fri Jul 25 11:25:20 DBClientCursor::init call () failed

Fri Jul 25 11:25:20 query failed: admin.$cmd {replSetStepDown: 60.0} to: 127.0.0.1:28010

Fri Jul 25 11:25:20 Error: error doing query: failed shell/collection.js:151

Fri Jul 25 11:25:20 trying reconnect to 127.0.0.1:28010

Fri Jul 25 11:25:20 reconnect 127.0.0.1:28010 ok

Step 3, check the status after the operation

Check the status after the operation through rs.status ()

SECONDARY > rs.status ()

{

"set": "rs1"

Date: ISODate ("2014-07-25T03:29:03Z")

"myState": 2

"syncingTo": "localhost:28012"

"members": [

{

"_ id": 0

"name": "localhost:28010"

"health": 1

"state": 2

"stateStr": "SECONDARY",-downgrade implementation here

"optime": {

"t": 1406253797000

"I": 1

}

OptimeDate: ISODate ("2014-07-25T02:03:17Z")

"self": true

}

{

"_ id": 1

"name": "localhost:28011"

"health": 1

"state": 2

"stateStr": "SECONDARY"

Uptime: 219

"optime": {

"t": 1406253797000

"I": 1

}

OptimeDate: ISODate ("2014-07-25T02:03:17Z")

LastHeartbeat: ISODate ("2014-07-25T03:29:02Z")

"pingMs": 0

}

{

"_ id": 2

"name": "localhost:28012"

"health": 1

"state": 1

"stateStr": "PRIMARY".-look, this product has been upgraded.

Uptime: 219

"optime": {

"t": 1406253797000

"I": 1

}

OptimeDate: ISODate ("2014-07-25T02:03:17Z")

LastHeartbeat: ISODate ("2014-07-25T03:29:02Z")

"pingMs": 0

}

]

"ok": 1

}

SECONDARY >

Separation of reading and writing

Step 1: first insert a piece of test data into the main library, the code is as follows

-We made a master-slave switch before. 28010 is not the master, but 28012 is the master. Figure out the experimental environment.

[root@dota] # / usr/local/mongodb/bin/mongo-- port 28012

MongoDB shell version: 2.0.4

Connecting to: 127.0.0.1:28012/test

PRIMARY > db

Test

PRIMARY > db.c1.insert ({age:30})

PRIMARY > db.c1.find ()

{"_ id": ObjectId ("53d1ed7cb017538f84368f95"), "age": 30}

Step 2: execute the query operation from the slave library as follows

-switch to slave library

[root@dota] # / usr/local/mongodb/bin/mongo-- port 28011 MongoDB shell version: 2.0.4

Connecting to: 127.0.0.1:28011/test

SECONDARY > show dbs

Admin (empty)

Local 1.203125GB

Test 0.203125GB

SECONDARY > db

Test

SECONDARY > show collections-an error while executing this line indicates that the slave library cannot perform the query operation for the time being

Fri Jul 25 13:40:36 uncaught exception: error: {"$err": "not master and slaveok=false", "code": 13435}

Step 3, make the slave library readable to share the pressure on the master library, the code is as follows

SECONDARY > db.getMongo (). SetSlaveOk ()

Not master and slaveok=false

SECONDARY > show dbs

Admin (empty)

Local 1.203125GB

Test 0.203125GB

SECONDARY > show collections

C1

System.indexes

SECONDARY > db.c1.find ()

{"_ id": ObjectId ("53d1ed7cb017538f84368f95"), "age": 30}

SECONDARY >

Fail-over

Kill the mongodb process on port 28012 of the main library to see the status of the replication set

Step 1: kill the mongodb process on port 28012, with the following code

[root@dota ~] # ps aux | grep mongod

Root 1523 0.4 4.8 1791408 49420? Sl Jul20 29:36 / usr/local/mongodb/bin/mongod-dbpath=/usr/local/mongodb/data-logpath=/usr/local/mongodb/logs/mongo.logs-logappend-fork

Root 2737 0.00.0 103248 pts/0 S+ 14:15 0:00 grep mongod

Root 27933 1.5 4.2 3455684 42988? Sl 09:50 4:01 / usr/local/mongodb/bin/mongod-replSet rs1-- keyFile / data/key/r0-- fork-- port 28010-- dbpath / data/data/r0-- logpath=/data/log/r0.log-- logappend

Root 27950 0.3 3.6 3441068 36752? Sl 09:52 0:55 / usr/local/mongodb/bin/mongod-replSet rs1-- keyFile / data/key/r1-- fork-- port 28011-- dbpath / data/data/r1-- logpath=/data/log/r1.log-- logappend

Root 27967 3.0 3.7 3453368 38072? Sl 09:53 7:54 / usr/local/mongodb/bin/mongod-replSet rs1-- keyFile / data/key/r2-- fork-- port 28012-- dbpath / data/data/r2-- logpath=/data/log/r2.log-- logappend

[root@dota] # kill-9 27967

[root@dota ~] #

Step 2, check the status of the replication set as follows:

[root@dota] # / usr/local/mongodb/bin/mongo-- port 28011

MongoDB shell version: 2.0.4

Connecting to: 127.0.0.1:28011/test

SECONDARY > rs.status ()

{

"set": "rs1"

Date: ISODate ("2014-07-25T06:19:24Z")

"myState": 2

"syncingTo": "localhost:28010"

"members": [

{

"_ id": 0

"name": "localhost:28010"

"health": 1

"state": 1

"stateStr": "PRIMARY"

"uptime": 15356

"optime": {

"t": 1406266748000

"I": 1

}

OptimeDate: ISODate ("2014-07-25T05:39:08Z")

LastHeartbeat: ISODate ("2014-07-25T06:19:23Z")

"pingMs": 0

}

{

"_ id": 1

"name": "localhost:28011"

"health": 1

"state": 2

"stateStr": "SECONDARY"

"optime": {

"t": 1406266748000

"I": 1

}

OptimeDate: ISODate ("2014-07-25T05:39:08Z")

"self": true

}

{

"_ id": 2

"name": "localhost:28012"

"health": 0

"state": 8

"stateStr": "(not reachable/healthy)

"uptime": 0

"optime": {

"t": 1406266748000

"I": 1

}

OptimeDate: ISODate ("2014-07-25T05:39:08Z")

LastHeartbeat: ISODate ("2014-07-25T06:17:17Z")

"pingMs": 0

"errmsg": "socket exception"

}

]

"ok": 1

}

SECONDARY >

As you can see, by executing the rs.status command to check the status of the replication set, port 28012 mongodb has an exception, and the status information is not reaschable/healthy, indicating that he is already an invalid member of the replication set. After automatic election, the system chooses port 28010 as the master library and port 28011 as the slave library, then there are fault handling mechanisms that can greatly improve the stability and continuity of the system and solve the problem of single point of failure of the hidden sword.

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