MongoDB Replica Set Configuration
MongoDB replication is the process of synchronizing data across multiple servers.
Replication provides redundant backups of data, stores copies of data on multiple servers, improves data availability, and guarantees data security.
Replication also allows you to recover data from hardware failures and service outages.
Note When you create a replica set
- Version: The MongoDB version of each replica set server is consistent or supports the same Replset functionality
- Network: Each member within a replica set must be able to connect to other members (including itself) and be aware of Bind_ip--bind_ip_all at startup.
Configuring replica Sets
- 1 Setting up the configuration file
mongodb.conf
1 # Start MongoDB as a daemon on port 27017
2
3 port = 27017
4 fork = true
5 replSet = test_replica_set
6 dbpath = / datatest / db
7 logpath = /datatest/db/test.log
8 logappend = true
9
mongodb2.conf
2
3 port = 27017
4 fork = true
5 replSet = test_replica_set
6 dbpath = / datatest / db
7 logpath = /datatest/db/test.log
8 logappend = true
9
mongodb3.conf
2
3 port = 27017
4 fork = true
5 replSet = test_replica_set
6 dbpath = / datatest / db
7 logpath = /datatest/db/test.log
8 logappend = true
9
2 Start the database
mac-abeen: bin abeen $ sudo ./mongod -f mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 37181
child process started successfully, parent exiting
mac-abeen: bin abeen $ sudo ./mongod -f mongodb2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 37185
child process started successfully, parent exiting
mac-abeen: bin abeen $ sudo ./mongod -f mongodb3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 37189
child process started successfully, parent exiting
3 Initialize the replica set
mac-abeen: bin abeen $ ./mongo --nodb
MongoDB shell version: 3.2.8
> config = {}
{}
> config = {"_id": "test_replica_set", "members": [
{"_id": 0, "host": "192.168.0.10:27017"},
{"_id": 1, "host": "192.168.0.20:27017"},
{"_id": 2, "host": "192.168.0.30:27017"}]}
{
"_id": "test_replica_set",
"members": [
{
"_id": 0,
"host": "192.168.0.10:27017"
},
{
"_id": 1,
"host": "192.168.0.20:27017"
},
{
"_id": 2,
"host": "192.168.0.30:27017"
}
]
}
> db = (new Mongo ("192.168.0.10:27017")). getDB ("test")
test
> rs.initiate (config)
{"ok": 1}
test_replica_set: OTHER>
test_replica_set: PRIMARY> rs.config ()
{
"_id": "test_replica_set",
"version": 1,
"protocolVersion": NumberLong (1),
"members": [
{
"_id": 0,
"host": "192.168.0.10:27017",
"arbiterOnly": false,
"buildIndexes": true,
"hidden": false,
"priority": 1,
"tags": {
},
"slaveDelay": NumberLong (0),
"votes": 1
},
{
"_id": 1,
"host": "192.168.0.20:27017",
"arbiterOnly": false,
"buildIndexes": true,
"hidden": false,
"priority": 1,
"tags": {
},
"slaveDelay": NumberLong (0),
"votes": 1
},
{
"_id": 2,
"host": "192.168.0.30:27017",
"arbiterOnly": false,
"buildIndexes": true,
"hidden": false,
"priority": 1,
"tags": {
},
"slaveDelay": NumberLong (0),
"votes": 1
}
],
"settings": {
"chainingAllowed": true,
"heartbeatIntervalMillis": 2000,
"heartbeatTimeoutSecs": 10,
"electionTimeoutMillis": 10000,
"getLastErrorModes": {
},
"getLastErrorDefaults": {
"w": 1,
"wtimeout": 0
},
"replicaSetId": ObjectId ("5850f445c8cacd70496883b0")
}
}
4 Write data and view copy set data
test_replica_set: PRIMARY>
test_replica_set: PRIMARY> for (i = 0; i <100; i ++) {db.coll.insert ({"count": i})}
WriteResult ({"nInserted": 1})
test_replica_set: PRIMARY> db.coll.count ()
100
test_replica_set: PRIMARY> db.coll.find (). limit (5)
{"_id": ObjectId ("5850f5f35f1a7d82c0b45b51"), "count": 0}
{"_id": ObjectId ("5850f5f35f1a7d82c0b45b52"), "count": 1}
{"_id": ObjectId ("5850f5f35f1a7d82c0b45b53"), "count": 2}
{"_id": ObjectId ("5850f5f35f1a7d82c0b45b54"), "count": 3}
{"_id": ObjectId ("5850f5f35f1a7d82c0b45b55"), "count": 4}
5 Check if there is data in the replica set
mac-abeen: bin abeen $ ./mongo --port 27017 --host 192.168.0.20
MongoDB shell version: 3.2.8
connecting to: 192.168.0.20:27017/test
test_replica_set: SECONDARY> db.coll.find (). limit (5)
Error: error: {"ok": 0, "errmsg": "not master and slaveOk = false", "code": 13435}
Set connection to read data
test_replica_set: SECONDARY> db.setSlaveOk ()
Data already in copy
test_replica_set: SECONDARY> db.coll.find (). limit (5)
{"_id": ObjectId ("5850f5f35f1a7d82c0b45b53"), "count": 2}
{"_id": ObjectId ("5850f5f35f1a7d82c0b45b55"), "count": 4}
{"_id": ObjectId ("5850f5f35f1a7d82c0b45b52"), "count": 1}
{"_id": ObjectId ("5850f5f35f1a7d82c0b45b54"), "count": 3}
{"_id": ObjectId ("5850f5f35f1a7d82c0b45b51"), "count": 0}
Modify copy set
rs.add ("server-4: 27017") // Add
rs.addArb ("server-4: 27017") // Add election arbitrator
rs.add ({"_ id": 4, "host": "server-4: 27017", "arbiterOnly": true) // Add election Arbitrator
rs.add ({"_ id": 5, "host": "server-5: 27017", "priority": 0, "hidden": true) // Add hidden members
rs.remove ("server-4: 27017") // Remove
Modify the copy set, through rs.reconfig
rs.reconfig Restrictions when modifying replica set members
Can't modify member's _id field
The priority of the members (usually the master node) receiving the rs.reconfig command cannot be set to 0
A member who cannot be an arbitrator becomes a non-arbiter member, and vice versa.
Cannot modify members of buildIndexes: false to buildIndexes: true
Can modify other, such as host
var config = rs.config ()
config.members [1] .host = "newsserver: 27017" // Modify host
rs.reconfig (config)
[Database] MongoDB replica set configuration