机器 | ip |
---|---|
node01(master) | 71 |
node02(slave) | 72 |
node03(slave) | 73 |
# vim /etc/mongod.conf
replication:
oplogSizeMB: 20
replSetName: alextest
在master上面操作
[root@node01 ~]# mongo
MongoDB shell version: 3.0.7
connecting to: test
Server has startup warnings:
2017-06-20T02:33:02.858+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-06-20T02:33:02.858+0800 I CONTROL [initandlisten]
> use admin
switched to db admin
> config={_id:"alextest",members:[{_id:0,host:"192.168.6.71:27017"},{_id:1,host:"192.168.6.72:27017"},{_id:2,host:"192.168.6.73:27017"}]}
{
"_id" : "alextest",
"members" : [
{
"_id" : 0,
"host" : "192.168.6.71:27017"
},
{
"_id" : 1,
"host" : "192.168.6.72:27017"
},
{
"_id" : 2,
"host" : "192.168.6.73:27017"
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
alextest:OTHER> rs.status()
{
"set" : "alextest",
"date" : ISODate("2017-06-19T18:36:53.118Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.168.6.71:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 231,
"optime" : Timestamp(1497897400, 1),
"optimeDate" : ISODate("2017-06-19T18:36:40Z"),
"electionTime" : Timestamp(1497897402, 1),
"electionDate" : ISODate("2017-06-19T18:36:42Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.6.72:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 13,
"optime" : Timestamp(1497897400, 1),
"optimeDate" : ISODate("2017-06-19T18:36:40Z"),
"lastHeartbeat" : ISODate("2017-06-19T18:36:52.030Z"),
"lastHeartbeatRecv" : ISODate("2017-06-19T18:36:52.029Z"),
"pingMs" : 1,
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.6.73:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 13,
"optime" : Timestamp(1497897400, 1),
"optimeDate" : ISODate("2017-06-19T18:36:40Z"),
"lastHeartbeat" : ISODate("2017-06-19T18:36:52.017Z"),
"lastHeartbeatRecv" : ISODate("2017-06-19T18:36:52.026Z"),
"pingMs" : 0,
"configVersion" : 1
}
],
"ok" : 1
}
alextest:PRIMARY>
[root@node02 ~]# mongo
MongoDB shell version: 3.0.7
connecting to: test
Server has startup warnings:
2017-06-20T08:24:00.437+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-06-20T08:24:00.437+0800 I CONTROL [initandlisten]
alextest:SECONDARY>
[root@node03 ~]# mongo
MongoDB shell version: 3.0.7
connecting to: test
Server has startup warnings:
2017-06-20T08:24:20.049+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-06-20T08:24:20.049+0800 I CONTROL [initandlisten]
alextest:SECONDARY>
· 测试mongodb副本集
node01主
alextest:PRIMARY> use mydb
switched to db mydb
alextest:PRIMARY> show dbs
admin 0.078GB
alex 0.078GB
local 0.078GB
alextest:PRIMARY> db.C
db.changeUserPassword( db.constructor db.createUser(
db.cloneCollection( db.copyDatabase( db.currentOP(
db.cloneDatabase( db.createCollection( db.currentOp(
db.commandHelp( db.createRole(
alextest:PRIMARY> db.createCollection('testc')
{ "ok" : 1 }
alextest:PRIMARY> show dbs
admin 0.078GB
alex 0.078GB
local 0.078GB
mydb 0.078GB
node02 从
alextest:SECONDARY> rs.slaveOk()
alextest:SECONDARY> show dbs
admin 0.078GB
alex 0.078GB
local 0.078GB
mydb 0.078GB
alextest:SECONDARY> use mydb
switched to db mydb
alextest:SECONDARY> show tables
system.indexes
testc
master上面操作
alextest:PRIMARY> cfg = rs.conf()
{
"_id" : "alextest",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "192.168.6.71:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 1,
"host" : "192.168.6.72:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.6.73:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
alextest:PRIMARY> cfg.members[0].priority = 3
3
alextest:PRIMARY> cfg.members[1].priority = 2
2
alextest:PRIMARY> cfg.members[2].priority = 1
1
alextest:PRIMARY> rs.reconfig(cfg)
{ "ok" : 1 }
alextest:PRIMARY> rs.config()
{
"_id" : "alextest",
"version" : 2,
"members" : [
{
"_id" : 0,
"host" : "192.168.6.71:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 3,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 1,
"host" : "192.168.6.72:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 2,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.6.73:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
alextest:PRIMARY>
现在master挂了可以自动切换了 node02的slave变master
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有