1. Configure a three-node configuration file (/etc/28001.conf)
28002.conf
#bind_ip =192.168.20.144
port=28001
Logpath=/data/db/mongodb_log/28001.log
Logappend=true
Pidfilepath=/data/db/mongodb_data28001/28001.pid
oplogsize=500
dbpath=/data/db/mongodb_data28001
Replset=imooc
Fork=true
28002.conf
#bind_ip =192.168.20.144
port=28002
Logpath=/data/db/mongodb_log/28002.log
Logappend=true
Pidfilepath=/data/db/mongodb_data28002/28002.pid
oplogsize=500
dbpath=/data/db/28002
Replset=imooc
Fork=true
28003.conf
#bind_ip =192.168.20.144
port=28003
Logpath=/data/db/mongodb_log/28003.log
Logappend=true
Pidfilepath=/data/db/mongodb_data28003/28003.pid
oplogsize=500
dbpath=/data/db/28003
Replset=imooc
Fork=true
2. Start the Mongod service with a configuration file
/usr/local/mongodb/bin/mongod-f/usr/local/mongodb/conf/28001.conf
/usr/local/mongodb/bin/mongod-f/usr/local/mongodb/conf/28002.conf
/usr/local/mongodb/bin/mongod-f/usr/local/mongodb/conf/28003.conf
3. Log in to the running monogd machine
/usr/local/mongodb/bin/mongo 127.0.0.1:28001/admin
4. Configure the replica set node configuration file
config={_id: "Imooc", Members: [{_id:1, Host: "127.0.0.1:28001"},{_id:2, Host: "127.0.0.1:28002"},{_id:3, Host: "127.0.0.1:28003"}]}
5. View node members
Config.members
6. Set the 3 node as the quorum node
CONFIG.MEMBERS[3] ={"_id": 3, "host": "127.0.0.1:28003", arbiteronly:true}
Or enter in the main node:
Rs.addarb ("127.0.0.1:28003")
7. initializing a replica set
Rs.initiate (config) (if the initialization has been reported to have been initialized, you need to back up the data, re-create the Master node.) )
8. Reload the config parameter ( reconfig once every time the config is modified , but reconfig causes The connection of the replica set to break briefly , to avoid any execution in a production environment, a maintenance time window is required for execution)
Rs.reconfig (Cnf,{force:true})
(Run this command must be in the primary node, if not display primary, possibly other, you need to add Force parameters)
9. View the replication set status
Rs.status ()
The front- End Reference program for MongoDB is to differentiate the master-slave node through the IsMaster command (the hidden node can see its information through Rs.status (), while the Rs.ismaster is impossible to see)
Rs.ismaster ()
Maintenance:
To add a replica, under Log on to the master node, enter:
Rs.add ("Ip:port")
Deleting replicas
Rs.remove ("Ip:port")
(if the ip:port must be a MongoDB instance that uses the same relpset name )
Import command after import of existing data
Mongorestore-h 127.0.0.1:12345-d SYT--directoryperdb/mnt/mongo_data/
where /mnt/mongo_data is the JSON file to be imported , it is discovered that the slave node becomes the recovering state.
cause of the problem
The main reason for this problem is that the speed of secondary node synchronization oplog not catch up with primary a few times, resulting in recovering state.
Workaround:
first stop the slave nodeMongodprocess, and then delete the directory(RS)all of the following data, and then rebootMongodprocess, here's one thing to note, if there areArbiterof theMongodThe process also needs to be stopped, and when it starts, it starts first.Replsetof theMongodprocess, and then startArbiterof theMongodprocess, after startup,is automatically determined byRecoveringStatus Switch toStartup2state, and finally switch toSecondaryStatus
The second option is to firstRecoveringnode under theDatadirectory is deleted, and thenPrimaryin theDatacopy it to the node and restart it! Before you operate, be sure to stop allMongodbDatabase!
As you can see, the show tables error from the node shows "This node is not the primary node, the OK State from the node is failed". If you need to read data from a node, you need to set slave:
Rs.slaveok (True)
then execute show tables to succeed.
Simulate downtime:
on the master node:db.shutdownserver ()
error "errmsg": "Shutdown must run from localhost when running db without Auth",
This error indicates that you need to execute under localhost without enabling authentication , and if BIN_IP is specified in the boot configuration file ,it can only be executed via bin_ip , where we cannot log in. LocalHost executes, only by means of kill.
find the corresponding process number by viewing ps–ef|grep * * *
using kill-2 (not the last resort to use kill to close a process instance, even if it is to be used, it should be closed in a milder way, Kill-2 {SIGINT} . Kill-2 The process of all queues in the instance is closed before closing the instance)
Test after closing:
Check the original main node, hit enter has been error
looking at the original slave node and tapping enter from the two nodes , you can see that one of the nodes has become PRIMARY
The switch of the node can be seen through show log Rs.
MongoDB Enterprise imooc:primary> Show Log rs
2017-10-30t01:53:15.617-0700 I REPL [replExecDBWorker-0] New replica set config in use: {_id: "Imooc", Version:1, PR Otocolversion:1, Members: [{_id:1, Host: "127.0.0.1:28001", Arbiteronly:false, Buildindexes:true, Hidden:false, PRI ority:1.0, Tags: {}, slavedelay:0, votes:1}, {_id:2, Host: "127.0.0.1:28002", Arbiteronly:false, Buildindexes:true , Hidden:false, priority:1.0, tags: {}, slavedelay:0, votes:1}, {_id:3, Host: "127.0.0.1:28003", Arbiteronly:false , Buildindexes:true, H
2017-10-30t01:53:15.617-0700 I REPL [replExecDBWorker-0] transition to STARTUP2
2017-10-30t01:53:15.618-0700 I REPL [Rssync] transition to RECOVERING
2017-10-30t01:53:15.619-0700 I REPL [Rssync] transition to secondary
2017-10-30t01:53:15.621-0700 I REPL [replicationexecutor] Member 127.0.0.1:28001 is now on state secondary
2017-10-30t01:53:20.624-0700 I REPL [replicationexecutor] Member 127.0.0.1:28003 is now on state secondary
2017-10-30t01:53:25.625-0700 I REPL [replicationexecutor] Member 127.0.0.1:28001 is now on state PRIMARY
2017-10-30t02:13:53.329-0700 I REPL [Rsbackgroundsync] could not find member to sync from
2017-10-30t02:14:01.816-0700 I REPL [replicationexecutor] transition to PRIMARY
2017-10-30t02:14:03.033-0700 I REPL [Rssync] transition to primary complete; Database writes is now permitted
(Pending verification)
If there is a node that cannot find Oplog(as seen through the log), you can copy the oplog of the master node to the slave node
1. back up:
./mongodump--port 28011-d local-c oplog.rs-o/opt/backup/0706local/
2. Restore to another single-node MONGODB Server
./mongorestore--port 28011-d temp_local-c shard1_oplog--dir/opt/backup/0706local/local/oplog.rs.bson
If an error occurs while recovering Failed:error connecting to DB Server:no reachable servers
................
..............
If there is a problem with a node, remove monogd.lock, add-repair at startup , startor start, and just as a quorum node that does not store data, The simplest and most brutal approach is to start a success by removing the data from the node and then restarting it .
MongoDB Replica Set configuration steps