二,mongo 副本集
副本集也是一个主从的同步,不过他支持主从的自动切换
这里的master不是固定的,因为副本集会根据选举来决定谁是master,而且,当一个master挂掉以后,会进行重新选举,选出新master,踢掉挂掉的机器
配置步骤:
hostname rac1.example.com 192.168.78.130
hostname rac2.example.com 192.168.78.131
首先每个机器的/etc/hosts 文件中,都有服务器ip对应的主机名
步骤一:
在130上
启动mongo:
. [root@rac1 bin]# ./mongod --dbpath=/root/data --logpath=/root/data/mongo.log --logappend --fork --port=10001 --replSet test/rac1.example.com:10001
forked process: 6661
all output going to: /root/data/mongo.log
child process started successfully, parent exiting
在131上:
[root@rac2 bin]# ./mongod --dbpath=/root/data --logpath=/root/data/mongo.log --logappend --fork --port=10002 --replSet test/rac2.example.com:10002
forked process: 5617
all output going to: /root/data/mongo.log
child process started successfully, parent exiting
下面进行初始化
> use admin
switched to db admin
> db.runCommand({"replSetInitiate":{"_id":"test","member":[{"_id":1,"host":"rac1.example.com:10001"},{"_id":2,"host":"rac2.example.com:10002"}]}})
如果在这里报错了,是因为没有初始化成功,我们需要使用rs.initiate() 进行初始化
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "rac1.example.com:10001",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
成功后,我们查看一下,会发现提示符已经变化了,提示符的前面test表示这个副本集的名字,后面表示primary
test:PRIMARY> use local
switched to db local
test:PRIMARY> show dbs
local 0.140625GB
pincer 0.0625GB
test:PRIMARY> show collections
oplog.$main
oplog.rs
slaves
system.indexes
system.replset
通过system.replset去查看状态
test:PRIMARY> db.system.replset.find()
{ "_id" : "test", "version" : 1, "members" : [ { "_id" : 0, "host" : "rac1.example.com:10001" } ] }
这里可以看到,这里只有一个member rac1 但是rac2不见了
我们去rac2 机器上,发现这里还是没有加入,
通过rs命令,我们去rac1上执行
新加,则用rs.add("rac2.example.com:10002"))
}
test:PRIMARY> rs.add("rac2.example.com:10002")
然后过一会,发现rac2上的状态变了
test:STARTUP2>
test:SECONDARY>
通过提示符,可以看到状态从startup 变成secondary了
然后再查看一下状态
test:SECONDARY> rs.status()
{
"set" : "test",
"date" : ISODate("2013-08-08T02:24:07Z"),
"myState" : 2,
"syncingTo" : "rac1.example.com:10001",
"members" : [
{
"_id" : 0,
"name" : "rac1.example.com:10001",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 54,
"optime" : Timestamp(1376785130000, 1),
"optimeDate" : ISODate("2013-08-18T00:18:50Z"),
"lastHeartbeat" : ISODate("2013-08-08T02:24:05Z"),
"pingMs" : 1
},
{
"_id" : 1,
"name" : "rac2.example.com:10002",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1514,
"optime" : Timestamp(1376785130000, 1),
"optimeDate" : ISODate("2013-08-18T00:18:50Z"),
"self" : true
}
],
"ok" : 1
}
也可以使用一下方法初始化
cfg={"_id":1,members:[{"_id":1,"host":"rac1.example.com:10001"},{"_id":2,"host":"rac2.example.com:10002"}]}
rs.initiate(cfg)
新加,则用rs.add("rac2.example.com:10002"))
下面做个自动切换,到130 上去关闭mongo
test:PRIMARY> use admin
switched to db admin
test:PRIMARY> db.shutdownServer()
Sun Aug 18 08:46:45 DBClientCursor::init call() failed
Sun Aug 18 08:46:45 query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1:10001
server should be down...
Sun Aug 18 08:46:45 trying reconnect to 127.0.0.1:10001
Sun Aug 18 08:46:45 reconnect 127.0.0.1:10001 failed couldn't connect to server 127.0.0.1:10001
> exit
bye
然后到131上,过1分总登陆上去
[root@rac2 bin]# ./mongo rac2.example.com:10002/admin
MongoDB shell version: 2.2.2
connecting to: rac2.example.com:10002/admin
test:PRIMARY>
这时提示符都已经变成primary了,可以在这里插入和查询了
从下面可以看到,rac1的statestr变成了"SECONDARY"
test:PRIMARY> rs.status()
{
"set" : "test",
"date" : ISODate("2013-08-08T02:58:19Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "rac1.example.com:10001",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 374,
"optime" : Timestamp(1376786436000, 1),
"optimeDate" : ISODate("2013-08-18T00:40:36Z"),
"lastHeartbeat" : ISODate("2013-08-08T02:58:18Z"),
"pingMs" : 1
},
{
"_id" : 1,
"name" : "rac2.example.com:10002",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 3566,
"optime" : Timestamp(1376786436000, 1),
"optimeDate" : ISODate("2013-08-18T00:40:36Z"),
"self" : true
}
],
"ok" : 1
}
也可以使用一下方法初始化
cfg={"_id":1,members:[{"_id":1,"host":"rac1.example.com:10001"},{"_id":2,"host":"rac2.example.com:10002"}]}
rs.initiate(cfg)