Mongodb Master-Slave replication and deployment of replica sets
1. Introduction to MongoDB configuration parameters:
[root@host11 tmp] # cat / etc/mongod.conf
# run as a daemon
Fork=true
# set the path of db
Dbpath=/usr/local/mongodb/data/db
# set log path
Logpath=/usr/local/mongodb/data/log/mongod.log
# logappend: log writing mode: set to true and append. The default is override. If this setting is not specified, the existing log file will be overwritten by MongoDB at startup.
Logappend=true
# set port number
Port=27017
# start a web server on the port number of the service plus 1000
Rest=true
# Open the permission control system
# auth=true
two。 Deploy mongodb master-slave replication:
1. Create configuration files for master and slave:
[root@host11 tmp] # cat / etc/mongod_master.conf
Logpath=/usr/local/mongodb/data/log/mongod_master.log
Logappend=true
Fork=true
Dbpath=/usr/local/mongodb/data/master
Port=10000
Rest=true
Master=true
# auth=true
# slaveok=true
[root@host11 tmp] #
[root@host11 tmp] # cat / etc/mongod_slave.conf
Logpath=/usr/local/mongodb/data/log/mongod_slave.log
Logappend=true
Fork=true
Dbpath=/usr/local/mongodb/data/slave
Port=10001
Rest=true
Slave=true
# auth=true
Source=127.0.0.1:10000
two。 Start the master-slave process
[root@host11 tmp] # mkdir-pv / usr/local/mongodb/data/master
[root@host11 tmp] # mkdir-pv / usr/local/mongodb/data/slave
[root@host11 tmp] mongod-f / etc/mongod_master.conf
[root@host11 tmp] mongod-f / etc/mongod_slave.conf
3. Check the master-slave status:
[root@host11 tmp] # mongo localhost:10000
> db.isMaster ()
{
"ismaster": true
"maxBsonObjectSize": 16777216
"maxMessageSizeBytes": 48000000
"maxWriteBatchSize": 1000
LocalTime: ISODate ("2016-02-28T11:46:54.199Z")
"maxWireVersion": 4
"minWireVersion": 0
"ok": 1
}
>
> db.printReplicationInfo ()
Configured oplog size: 990MB
Log length start to end: 23821secs (6.62hrs)
Oplog first event time: Sun Feb 28 2016 13:05:17 GMT+0800 (CST)
Oplog last event time: Sun Feb 28 2016 19:42:18 GMT+0800 (CST)
Now: Sun Feb 28 2016 19:42:20 GMT+0800 (CST)
> db.printSlaveReplicationInfo ()
Local.sources is empty; is this db a-- slave?
> db.getReplicationInfo ()
{
LogSizeMB: 990
"usedMB": 0.14
"timeDiff": 23871
"timeDiffHours": 6.63
TFirst: "Sun Feb 28 2016 13:05:17 GMT+0800 (CST)"
TLast: "Sun Feb 28 2016 19:43:08 GMT+0800 (CST)"
Now: "Sun Feb 28 2016 19:43:18 GMT+0800 (CST)"
}
[root@host11 tmp] # mongo localhost:10001
> db.printReplicationInfo ()
This is a slave, printing slave replication info.
Source: 127.0.0.1:10000
SyncedTo: Sun Feb 28 2016 19:44:18 GMT+0800 (CST)
4 secs (0 hrs) behind the freshest member (no primary available at the moment)
three。 Deployment of replica sets:
1. Create a profile for each node node:
[root@host11 tmp] # cat / etc/mongod_node1.conf
Logpath=/usr/local/mongodb/data/log/mongod_node1.log
Logappend=true
Fork=true
Dbpath=/usr/local/mongodb/data/node1
Port=20001
Rest=true
# auth=true
# slaveok=true
ReplSet=myrepl
[root@host11 tmp] # cat / etc/mongod_node2.conf
Logpath=/usr/local/mongodb/data/log/mongod_node2.log
Logappend=true
Fork=true
Dbpath=/usr/local/mongodb/data/node2
Port=20002
Rest=true
# auth=true
# slaveok=true
ReplSet=myrepl
[root@host11 tmp] # cat / etc/mongod_node3.conf
Logpath=/usr/local/mongodb/data/log/mongod_node3.log
Logappend=true
Fork=true
Dbpath=/usr/local/mongodb/data/node3
Port=20003
Rest=true
# auth=true
# slaveok=true
ReplSet=myrepl
[root@host11 tmp] #
two。 Configure and start each node service
[root@host11 tmp] # mkdir-pv / usr/local/mongodb/data/node1
[root@host11 tmp] # mkdir-pv / usr/local/mongodb/data/node2
[root@host11 tmp] # mkdir-pv / usr/local/mongodb/data/node3
[root@host11 tmp] # mongod-f / etc/mongod_node1.conf
[root@host11 tmp] # mongod-f / etc/mongod_node2.conf
[root@host11 tmp] # mongod-f / etc/mongod_node2.conf
Log in to mongod:
# set the replica set variable:
> config = {_ id: "myrepl", members: [{_ id:0,host: "127.0.0.1 id:0,host 20001"}, {_ id:1,host: "127.0.0.1 members 20002"}, {_ id:2,host: "127.0.0.1 myrepl"}]}
# initialize replica set configuration
> rs.initiate (config)
3. Check the status of each node node
# View the status of the replica set
> rs.status ()
# Delete a node:
Rs.remove ("127.0.0.1 purl 20001")
Rs.status ()
# add nodes:
Rs.add ("127.0.0.1 purl 20001")
Rs.status ()
four。 At this point, the deployment of mongo master-slave and replica cluster is complete.