返回首页
当前位置: 主页 > 编程语言 > PHP教程 >

Mongodb Replica Set Deployment

时间:2014-11-27 12:49来源:电脑教程学习网 www.etwiki.cn 编辑:admin

在一台虚拟机里面安装了三个mongod实例,一主二从,另外还安装了一个arbiter。


创建数据总目录
root@ubuntu:/# mkdir /data/mongodb -p
创建日志目录
root@ubuntu:/# mkdir /data/mongodb_log -p

创建三个mongod实例的数据目录
root@ubuntu:/# cd /data/mongodb
root@ubuntu:/data/mongodb# mkdir rs0-0
root@ubuntu:/data/mongodb# mkdir rs0-1
root@ubuntu:/data/mongodb# mkdir rs0-2



启动三个mongod.
replSet名为 rs0

mongod --port 27020 --dbpath /data/mongodb/rs0-0 --logpath /data/mongodb_log/rs0-0.log --replSet rs0 --logappend &
mongod --port 27021 --dbpath /data/mongodb/rs0-1 --logpath /data/mongodb_log/rs0-1.log --replSet rs0 --logappend &
mongod --port 27022 --dbpath /data/mongodb/rs0-2 --logpath /data/mongodb_log/rs0-2.log --replSet rs0 --logappend &



连接到你想让其成为主服务器的mongod:

root@ubuntu:/data/mongodb_log# mongo --port 27021
MongoDB shell version: 2.2.4
connecting to: 127.0.0.1:27021/test
>
>
>
> show dbs;
local (empty)
>
初始化replSet
> rs.initiate();
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "ubuntu:27021",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
获取配置信息
> rs.conf();
{
"_id" : "rs0",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "ubuntu:27021"
}
]
}
获取replSet的状态
rs0:PRIMARY> rs.status();
{
"set" : "rs0",
"date" : ISODate("2014-06-30T07:59:09Z"),
"myState" : 1, 表示当前登录的为主服务器
"members" : [
{
"_id" : 0,
"name" : "ubuntu:27021",
"health" : 1,
"state" : 1, 1表示主服务器
"stateStr" : "PRIMARY",
"uptime" : 132,
"optime" : Timestamp(1404115084000, 1),
"optimeDate" : ISODate("2014-06-30T07:58:04Z"),
"self" : true
}
],
"ok" : 1
}
rs0:PRIMARY>
rs0:PRIMARY>
添加从服务器, localhost不能乱用
rs0:PRIMARY> rs.add("localhost:27021");
{
"errmsg" : "exception: can't use localhost in repl set member names except when using it for all members",
"code" : 13393,
"ok" : 0
}
尼玛写错端口了...
rs0:PRIMARY> rs.add("ubuntu:27021");
{
"errmsg" : "exception: bad replset config -- duplicate hosts in the config object?",
"code" : 13108,
"ok" : 0
}
rs0:PRIMARY> rs.add("ubuntu:27020");
{ "ok" : 1 }
rs0:PRIMARY>
rs0:PRIMARY> rs.add("ubuntu:27022");
{ "ok" : 1 }
rs0:PRIMARY> rs.conf();
{
"_id" : "rs0",
"version" : 3,
"members" : [
{
"_id" : 0,
"host" : "ubuntu:27021"
},
{
"_id" : 1,
"host" : "ubuntu:27020"
},
{
"_id" : 2,
"host" : "ubuntu:27022"
}
]
}
rs0:PRIMARY> rs.status();
{
"set" : "rs0",
"date" : ISODate("2014-06-30T08:01:11Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "ubuntu:27021",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 254,
"optime" : Timestamp(1404115259000, 1),
"optimeDate" : ISODate("2014-06-30T08:00:59Z"),
"self" : true
},
{
"_id" : 1,
"name" : "ubuntu:27020",
"health" : 1,
"state" : 5, 从服务器正在初始化
"stateStr" : "STARTUP2",
"uptime" : 15,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2014-06-30T08:01:10Z"),
"pingMs" : 0,
"errmsg" : "initial sync need a member to be primary or secondary to do our initial sync"
},
{
"_id" : 2,
"name" : "ubuntu:27022",

------分隔线----------------------------
标签(Tag):php php实例教程 php5 php源代码 php基础教程 php技巧 php6 php教程
------分隔线----------------------------
推荐内容
猜你感兴趣