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 Replica Set replica set

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

Share

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

1. Introduction

MongoDB replication is the process of synchronizing data on multiple servers, similar to MySQL Replication, which is an asynchronous replication

Replication provides redundant backup of data, and stores copies of data on multiple servers, which improves the availability of data and ensures the security of data.

2. Characteristics of MongoDB replication

(1) the node of mongodb has three roles: primary,seconfary,arbiter

Primary is responsible for processing client requests, reading and writing data

Secondary replicates the data on the primary node through oplog and can also read the data

Arbiter nodes do not store data and only vote, so no separate hardware configuration is required

(2) if primary is not available, secondary will be promoted to the new primary; controlled by the parameter electionTimeoutMillis=10s.

3. Experimental environment

MongoDB-3.0.6 version

Three MongoDB instances on one physical machine, each occupying three nodes

172.16.101.54 27017

172.16.101.54 27018

172.16.101.54 27019

4. Create data and log directories

[root@sht-sgmhadoopcm-01 mongodb] # mkdir-p / usr/local/mongodb/data/ {db27017,db27018,db27019}

[root@sht-sgmhadoopcm-01 mongodb] # mkdir-p / usr/local/mongodb/log

5. Configuration file / etc/mongod.conf

The three nodes create configuration files respectively. The difference in the content of the file is that the nodes are different, but the others are all the same. The parameter replSetname is used to specify that the replica set name is rs0.

[root@sht-sgmhadoopcm-01 mongodb] # ll / etc/mongod2701*.conf

-rw-r--r-- 1 root root 405 Nov 2 20:21 / etc/mongod27017.conf

-rw-r--r-- 1 root root 405 Nov 2 20:13 / etc/mongod27018.conf

-rw-r--r-- 1 root root 405 Nov 2 20:13 / etc/mongod27019.conf

Mongodb configuration files should be followed by a space after each colon, otherwise an error will be reported

[root@sht-sgmhadoopcm-01 mongodb] # cat / etc/mongod27017.confsystemLog: destination: file path: "/ usr/local/mongodb/log/mongod27017.log" logAppend: dbPath: / usr/local/mongodb/data/db27017 journal: enabled: trueprocessManagement: fork: true pidFilePath: / usr/local/mongodb/data/db27017/mongod27017.pidnet: port: 27017 bindIp: 0.0.0.0setParameter: enableLocalhostAuthBypass: falsereplication: replSetName: rs0

6. Start MongoDB

Start the MongoDB service for each of the three nodes and specify the configuration file

[root@sht-sgmhadoopcm-01 mongodb] # bin/mongod-- config / etc/mongod27017.conf

[root@sht-sgmhadoopcm-01 mongodb] # bin/mongod-- config / etc/mongod27018.conf

[root@sht-sgmhadoopcm-01 mongodb] # bin/mongod-- config / etc/mongod27019.conf

7. Initialize the replica set

Log in to the 27017 node

[root@sht-sgmhadoopcm-01 mongodb] # bin/mongo 172.16.101.54:27017/admin

> rs.initiate ()

{

"info2": "no configuration explicitly specified-making one"

"me": "sht-sgmhadoopcm-01:27017"

"ok": 1

}

If you add a member to the replica set, the node with large priority is more likely to receive a commission.

Rs0:PRIMARY > rs.add ('172.16.101.54 virtual 27018')

{"ok": 1}

Rs0:PRIMARY > rs.add ('172.16.101.54) 27019 priority5)

{"ok": 1}

View replica set configuration

Rs0:PRIMARY > rs.conf () {"_ id": "rs0", "version": 3, "members": [{"_ id": 0, "host": "sht-sgmhadoopcm-01:27017", "arbiterOnly": false, "buildIndexes": true, "hidden": false "priority": 1, "tags": {}, "slaveDelay": 0, "votes": 1}, {"_ id": 1, "host": "172.16.101.54 slaveDelay 27018", "arbiterOnly": false "buildIndexes": true, "hidden": false, "priority": 1, "tags": {}, "slaveDelay": 0, "votes": 1}, {"_ id": 2 "host": "172.16.101.54 true", "arbiterOnly": false, "buildIndexes": true, "hidden": false, "priority": 1, "tags": {}, "slaveDelay": 0 "votes": 1}], "settings": {"chainingAllowed": true, "heartbeatTimeoutSecs": 10, "getLastErrorModes": {}, "getLastErrorDefaults": {"w": 1, "wtimeout": 0}

View the status of each role

Rs0:PRIMARY > rs.status () {"set": "rs0", "date": ISODate ("2018-11-02T12:27:44.570Z"), "myState": 1, "members": [{"_ id": 0, "name": "sht-sgmhadoopcm-01:27017", "health": 1, "state": 1 "stateStr": "PRIMARY", "uptime": 626, "optime": Timestamp (1541161589, 1), "optimeDate": ISODate ("2018-11-02T12:26:29Z"), "electionTime": Timestamp (1541161465, 2), "electionDate": ISODate ("2018-11-02T12:24:25Z"), "configVersion": 3 "self": true}, {"_ id": 1, "name": "172.16.101.54 name", "health": 1, "state": 2, "stateStr": "SECONDARY", "uptime": 161, "optime": Timestamp (1541161589 1), "optimeDate": ISODate ("2018-11-02T12:26:29Z"), "lastHeartbeat": ISODate ("2018-11-02T12:27:43.430Z"), "lastHeartbeatRecv": ISODate ("2018-11-02T12:27:43.311Z"), "pingMs": 0, "syncingTo": "sht-sgmhadoopcm-01:27017" "configVersion": 3}, {"_ id": 2, "name": "172.16.101.54 health", "health": 1, "state": 2, "stateStr": "SECONDARY", "uptime": 75, "optime": Timestamp (1541161589) 1), "optimeDate": ISODate ("2018-11-02T12:26:29Z"), "lastHeartbeat": ISODate ("2018-11-02T12:27:43.430Z"), "lastHeartbeatRecv": ISODate ("2018-11-02T12:27:43.430Z"), "pingMs": 0, "lastHeartbeatMessage": "could not find member to sync from" "configVersion": 3}], "ok": 1}

8. Data synchronization test

Insert data into 27017 nodes

[root@sht-sgmhadoopcm-01 mongodb] # bin/mongo 172.16.101.54:27017/admin

Rs0:PRIMARY > db.user.insert ({"username": "test", "age": 12, "sex": "m"})

WriteResult ({"nInserted": 1})

Rs0:PRIMARY > db.user.find ()

{"_ id": ObjectId ("5bdc65ff048de313da6c425a"), "username": "test", "age": 12, "sex": "m"}

Data successfully synchronized to 27018 and 27019 nodes

[root@sht-sgmhadoopcm-01 mongodb] # bin/mongo 172.16.101.54:27018/admin

Rs0:SECONDARY > db.getMongo (). SetReadPref ('secondaryPreferred')

Rs0:SECONDARY > db.user.find ()

{"_ id": ObjectId ("5bdc65ff048de313da6c425a"), "username": "test", "age": 12, "sex": "m"}

[root@sht-sgmhadoopcm-01 mongodb] # bin/mongo 172.16.101.54:27019/admin

Rs0:SECONDARY > db.getMongo (). SetReadPref ('secondaryPreferred')

Rs0:SECONDARY > db.user.find ()

{"_ id": ObjectId ("5bdc65ff048de313da6c425a"), "username": "test", "age": 12, "sex": "m"}

9. AutoFailover test

Kill's current primary27017 node

[root@sht-sgmhadoopcm-01 mongodb] # ps-ef | grep mongod

Root 5703 1 0 20:17? 00:00:52 bin/mongod-- config / etc/mongod27017.conf

Root 5747 1 0 20:17? 00:00:51 bin/mongod-- config / etc/mongod27018.conf

Root 5979 1 0 20:20? 00:00:52 bin/mongod-- config / etc/mongod27019.conf

[root@sht-sgmhadoopcm-01 mongodb] # kill-9 5703

You can see that 27019 becomes primary and 27017 is not available at this time

[root@sht-sgmhadoopcm-01 mongodb] # bin/mongo 172.16.101.54:27018/admin

Rs0:SECONDARY > rs.status () {"set": "rs0", "date": ISODate ("2018-11-02T15:10:18.746Z"), "myState": 2, "members": [{"_ id": 0, "name": "sht-sgmhadoopcm-01:27017", "health": 0, "state": 8 "stateStr": (not reachable/healthy), "uptime": 0, "optime": Timestamp (0,0), "optimeDate": ISODate ("1970-01-01T00:00:00Z"), "lastHeartbeat": ISODate ("2018-11-02T15:10:18.631Z") "lastHeartbeatRecv": ISODate ("2018-11-02T15:09:28.583Z"), "pingMs": 0, "lastHeartbeatMessage": "Failed attempt to connect to sht-sgmhadoopcm-01:27017 Couldn't connect to server sht-sgmhadoopcm-01:27017 (172.16.101.54), connection attempt failed "," configVersion ":-1}, {" _ id ": 1," name ":" 172.16.101.54 name "," health ": 1," state ": 2 "stateStr": "SECONDARY", "uptime": 10349, "optime": Timestamp (1541170688, 2), "optimeDate": ISODate ("2018-11-02T14:58:08Z"), "configVersion": 3, "self": true}, {"_ id": 2 "name": "172.16.101.54 PRIMARY", "health": 1, "state": 1, "stateStr": "PRIMARY", "uptime": 9827, "optime": Timestamp (1541170688, 2), "optimeDate": ISODate ("2018-11-02T14:58:08Z") LastHeartbeat: ISODate ("2018-11-02T15:10:18.611Z"), "lastHeartbeatRecv": ISODate ("2018-11-02T15:10:18.592Z"), "pingMs": 0, "electionTime": Timestamp (1541171370, 1), "electionDate": ISODate ("2018-11-02T15:09:30Z") "configVersion": 3}], "ok": 1}

If you restart the 27017 node, it will automatically become a 27019 standby library, indicating that auto failover is successful.

[root@sht-sgmhadoopcm-01 mongodb] # bin/mongod-- config / etc/mongod27017.conf

Rs0:PRIMARY > rs.status () {"set": "rs0", "date": ISODate ("2018-11-02T15:13:27.035Z"), "myState": 1, "members": [{"_ id": 0, "name": "sht-sgmhadoopcm-01:27017", "health": 1, "state": 2 StateStr: "SECONDARY", "uptime": 42, "optime": Timestamp (1541170688, 2), "optimeDate": ISODate ("2018-11-02T14:58:08Z"), "lastHeartbeat": ISODate ("2018-11-02T15:13:26.764Z"), "lastHeartbeatRecv": ISODate ("2018-11-02T15:13:25.055Z") "pingMs": 0, "configVersion": 3}, {"_ id": 1, "name": "172.16.101.54 name", "health": 1, "state": 2, "stateStr": "SECONDARY", "uptime": 10017 Optime: Timestamp (1541170688, 2), optimeDate: ISODate ("2018-11-02T14:58:08Z"), "lastHeartbeat": ISODate ("2018-11-02T15:13:26.662Z"), "lastHeartbeatRecv": ISODate ("2018-11-02T15:13:26.678Z"), "pingMs": 0 "lastHeartbeatMessage": "could not find member to sync from", "configVersion": 3}, {"_ id": 2, "name": "172.16.101.54 id 27019", "health": 1, "state": 1, "stateStr": "PRIMARY" "uptime": 10349, "optime": Timestamp (1541170688, 2), "optimeDate": ISODate ("2018-11-02T14:58:08Z"), "electionTime": Timestamp (1541171370, 1), "electionDate": ISODate ("1541171370-11-02T15:09:30Z"), "configVersion": 3 "self": true}], "ok": 1}

FAQ

[root@sht-sgmhadoopcm-01 mongodb] # bin/mongod-config / etc/mongod27017.conf &

[1] 4885

[root@sht-sgmhadoopcm-01 mongodb] # Error parsing YAML config file: yaml-cpp: error at line 8, column 11: illegal map value

Try 'bin/mongod-- help' for more information

[1] + Exit 2 bin/mongod-- config / etc/mongod27017.conf

The reason is that the configuration file / etc/mongod.conf colon is not followed by a space.

Reference link

MongoDB Replication

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