1、将安装包上传到服务器并解压
2、创建一个配置文件config.conf将配置写入
master(主)配置:
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(从)配置:
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(仲裁)配置:
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---集群名字
3、使用命令启动
/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
4、在bin下运行./mongo 192.168.0.125:27017命令进入mongo里面
5、执行use admin 切换到admin角色,再执行下面的代码
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名字相同才行。
4、然后执行rs.initiate(config);
5、执行完成后可以通过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 }
这样显示就说明成功了