Mongo副本集搭建
解压mongodb-linux-x86_64-rhel70-3.2.0.tgz
将解压后的bin路径添加到系统环境变量,保证mongo、mongod等命令可用
创建副本集目录mongo/27017、27018、27019
27017、27018、27019下在创建mogo.conf:
logpath=/data/local/mongo/27017/log/mongo.log dbpath=/data/local/mongo/27017/data bind_ip=127.0.0.1 #若允许外网访问,需删掉此项 logappend=true fork=true port=27017
创建配置文件中logpath和dbpath目录。
启动服务
mongod --config 27017/mongo.conf --replSet set1
mongod --config 27018/mongo.conf --replSet set1
mongod --config 27019/mongo.conf --replSet set1
配置副本集:
连接27017
mongo --port 27017
定义变量:
>conf = {_id:"set1",members:[{_id:0,host:"127.0.0.1:27017"}]}
rs.initiate(conf)
显示:
<"ok":1>
将本实例设置成master:
>db.isMaster()
将27018、27019添加到副本集:
>rs.add("127.0.0.1:27018") >rs.add("127.0.0.1:27019")
登录任一台查看副本集状态:
set1:PRIMARY> rs.status() { "set" : "set1", "date" : ISODate("2017-04-22T06:20:43.260Z"), "myState" : 1, "term" : NumberLong(1), "heartbeatIntervalMillis" : NumberLong(2000), "members" : [ { "_id" : 0, "name" : "127.0.0.1:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 321, "optime" : { "ts" : Timestamp(1492842009, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2017-04-22T06:20:09Z"), "electionTime" : Timestamp(1492841919, 2), "electionDate" : ISODate("2017-04-22T06:18:39Z"), "configVersion" : 3, "self" : true }, { "_id" : 1, "name" : "127.0.0.1:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 37, "optime" : { "ts" : Timestamp(1492842009, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2017-04-22T06:20:09Z"), "lastHeartbeat" : ISODate("2017-04-22T06:20:43.216Z"), "lastHeartbeatRecv" : ISODate("2017-04-22T06:20:42.216Z"), "pingMs" : NumberLong(0), "syncingTo" : "127.0.0.1:27017", "configVersion" : 3 }, { "_id" : 2, "name" : "127.0.0.1:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 34, "optime" : { "ts" : Timestamp(1492842009, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2017-04-22T06:20:09Z"), "lastHeartbeat" : ISODate("2017-04-22T06:20:43.221Z"), "lastHeartbeatRecv" : ISODate("2017-04-22T06:20:41.228Z"), "pingMs" : NumberLong(0), "configVersion" : 3 } ], "ok" : 1 }
修改默认数据引擎:
mongo3.2默认引擎为WiredTiger,若需要使用其他引擎,可在启动时指定:
mongod --storageEngine mmapv1 --config mongo.conf --replSet set1
强制kill掉mongo,再启动时会报错:
ERROR: child process failed, exited with error number 100
需先修复:
mongod -f mongo.conf --repair
然后再启动