mongodb复制集配置
目的:在192.168.1.14上测试配置mongodb复制集
1 在192.168.1.14上面起三个节点的mongodb服务
cd /home/mongodb-linux-x86_64-3.0.6
mkdir {data1,data2,data3}
touch {logs1,logs2,logs3}
vi ./conf/mongdb1.conf 27017
vi ./conf/mongdb2.conf 27018
vi ./conf/mongdb3.conf 27019
port=27017 #端口号()
fork=true #以守护进程的方式运行,创建服务器进程
logpath=/home/mongodb-linux-x86_64-3.0.6/logs1 #日志输出文件路径
logappend=true #日志输出方式
dbpath=/home/mongodb-linux-x86_64-3.0.6/data1 #数据库路径
maxConns=600 #数据库的最大连接数
oplogSize=2048
replSet=fzj
起服务:
./bin/mongod -f ./conf/mongodb1.conf
./bin/mongod -f ./conf/mongodb2.conf
./bin/mongod -f ./conf/mongodb3.conf
2 进入mongodb命令行初始化
../bin/mongo 192.168.1.14:27017
( ../bin/mongo --port 27017)
rs.status();查看状态
rs.status()
{
"info" : "run rs.initiate(...) if not yet done for the set",
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94
}
> cfg = {_id:"fzj",members:[
... ... {_id:0,host:"192.168.1.14:27017"},
... ... {_id:1,host:"192.168.1.14:27018"},
... ... {_id:2,host:"192.168.1.14:27019"}]
... ... }
{
"_id" : "fzj",
"members" : [
{
"_id" : 0,
"host" : "192.168.1.14:27017"
},
{
"_id" : 1,
"host" : "192.168.1.14:27018"
},
{
"_id" : 2,
"host" : "192.168.1.14:27019"
}
]
}
> rs.initiate(cfg);
{ "ok" : 1 }
3 如何扩容节点和删除节点
(1)增加节点:
新增配置文件mongodb4.conf
启动
rs.add(“192.168.1.14:27020”)
(2)删除
Rs.remove(“192.168.1.14:27020”)
4 模拟主从切换
(1)kill掉主数据库的进程
(2) 查看状态
rs.status()
5 手动主从切换
(1)在线冷冻
rs.freeze(35)
(2) 在线降级
rs.stepDown(35,30)
(3)再验证
rs.status()
6 增加一个仲裁节点,只负责仲裁,不做数据存储。
用仲裁者主要是因为MongoDB副本集需要奇数成员,而又没有足够服务器的情况。在服务器充足的情况下,不应该使用仲裁者节点。
新增配置文件mongodb4.conf
fzj:PRIMARY> rs.addArb("192.168.1.14:27020")
{ "ok" : 1 }
fzj:PRIMARY> rs.status();
{
"set" : "fzj",
"date" : ISODate("2017-07-21T22:38:34.246Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.168.1.14:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2059,
"optime" : Timestamp(1500676703, 1),
"optimeDate" : ISODate("2017-07-21T22:38:23Z"),
"lastHeartbeat" : ISODate("2017-07-21T22:38:33.530Z"),
"lastHeartbeatRecv" : ISODate("2017-07-21T22:38:33.387Z"),
"pingMs" : 0,
"syncingTo" : "192.168.1.14:27019",
"configVersion" : 2
},
{
"_id" : 1,
"name" : "192.168.1.14:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2920,
"optime" : Timestamp(1500676703, 1),
"optimeDate" : ISODate("2017-07-21T22:38:23Z"),
"lastHeartbeat" : ISODate("2017-07-21T22:38:33.530Z"),
"lastHeartbeatRecv" : ISODate("2017-07-21T22:38:32.296Z"),
"pingMs" : 0,
"syncingTo" : "192.168.1.14:27019",
"configVersion" : 2
},
{
"_id" : 2,
"name" : "192.168.1.14:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 3669,
"optime" : Timestamp(1500676703, 1),
"optimeDate" : ISODate("2017-07-21T22:38:23Z"),
"electionTime" : Timestamp(1500674592, 1),
"electionDate" : ISODate("2017-07-21T22:03:12Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 3,
"name" : "192.168.1.14:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 10,
"lastHeartbeat" : ISODate("2017-07-21T22:38:33.530Z"),
"lastHeartbeatRecv" : ISODate("2017-07-21T22:38:33.544Z"),
"pingMs" : 1,
"configVersion" : 2
}
],
"ok" : 1
}
转载于:https://blog.51cto.com/10676568/1949894