一、將安裝包上傳到服務器並解壓mongodb
二、建立一個配置文件config.conf將配置寫入服務器
master(主)配置:app
dbpath=/home/sczbbx/mongodb/data/master logpath=/home/sczbbx/mongodb/data/log/master.log pidfilepath=/home/sczbbx/mongodb/master.pid directoryperdb=true logappend=false replSet=testrs bind_ip=192.168.0.125 port=27017 oplogSize=10000 fork=true noprealloc=true
slaver(從)配置:ui
dbpath=/home/sczbbx/mongodb/data/slaver logpath=/home/sczbbx/mongodb/data/log/slaver.log pidfilepath=/home/sczbbx/mongodb/slaver.pid directoryperdb=true logappend=true replSet=testrs #bind_ip=192.168.0.126 port=27017 oplogSize=10000 fork=true noprealloc=true
arbiter(仲裁)配置:spa
dbpath=/home/sczbbx/mongodb/data/arbiter logpath=/home/sczbbx/mongodb/data/log/arbiter.log pidfilepath=/home/sczbbx/mongodb/arbiter.pid directoryperdb=true logappend=true replSet=testrs #bind_ip=192.168.0.127 port=27017 oplogSize=10000 fork=true noprealloc=true
配置參數說明:dbpath---數據儲存位置,logpath--日誌儲存位置,pidfilepath---pid儲存位置及名字,replSet---集羣名字日誌
三、使用命令啓動code
/home/sczbbx/mongodb/bin/mongod -f /home/sczbbx/mongodb/data/master/master.conf /home/sczbbx/mongodb/bin/mongod -f /home/sczbbx/mongodb/data/slaver/slaver.conf /home/sczbbx/mongodb/bin/mongod -f /home/sczbbx/mongodb/data/arbiter/arbiter.conf
四、在bin下運行./mongo 192.168.0.125:27017命令進入mongo裏面blog
五、執行use admin 切換到admin角色,再執行下面的代碼ip
config={_id:'testrs',members:[{_id :0,host:'192.168.0.125:27017',arbiterOnly:false,buildIndexes:true,hidden:false,priority:3,tags:{},slaveDelay:0,votes:1}, {_id:1,host:'192.168.0.126:27017',arbiterOnly:false,buildIndexes:true,hidden:false,priority: 1,tags: {},slaveDelay:0,votes:1}, {_id:2,host:'192.168.0.127:27017',arbiterOnly:true,buildIndexes:true,hidden: false,tags: {},slaveDelay: 0,votes: 1},], "settings":{chainingAllowed:true,heartbeatTimeoutSecs:10,getLastErrorModes: {},getLastErrorDefaults: {w:1,wtimeout:0}}};
須要注意的是:_id必需要和配置中的replSet名字相同才行。ci
四、而後執行rs.initiate(config);
五、執行完成後能夠經過rs.status();命令查看
{ "set" : "testrs", "date" : ISODate("2018-09-12T07:38:16.640Z"), "myState" : 1, "term" : NumberLong(13), "heartbeatIntervalMillis" : NumberLong(2000), "members" : [ { "_id" : 0, "name" : "192.168.0.125:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 1043171, "optime" : { "ts" : Timestamp(1536737884, 1), "t" : NumberLong(13) }, "optimeDate" : ISODate("2018-09-12T07:38:04Z"), "electionTime" : Timestamp(1535694736, 1), "electionDate" : ISODate("2018-08-31T05:52:16Z"), "configVersion" : 1, "self" : true }, { "_id" : 1, "name" : "192.168.0.126:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1041790, "optime" : { "ts" : Timestamp(1536737884, 1), "t" : NumberLong(13) }, "optimeDate" : ISODate("2018-09-12T07:38:04Z"), "lastHeartbeat" : ISODate("2018-09-12T07:38:15.014Z"), "lastHeartbeatRecv" : ISODate("2018-09-12T07:38:15.020Z"), "pingMs" : NumberLong(0), "syncingTo" : "192.168.0.125:27017", "configVersion" : 1 }, { "_id" : 2, "name" : "192.168.0.127:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 1036300, "lastHeartbeat" : ISODate("2018-09-12T07:38:14.999Z"), "lastHeartbeatRecv" : ISODate("2018-09-12T07:38:15.011Z"), "pingMs" : NumberLong(0), "configVersion" : 1 } ], "ok" : 1 }
這樣顯示就說明成功了