MongoDB之复制集(二)搭建

准备工作

环境: CentOS6.5 64位

MongoDB 3.0.1 64位

一、启动mongod服务


[root@localhost data]# /usr/local/mongodb3.0.1/bin/mongod --port 40144 --dbpath=/data/mongodb3.0.1/data/ --logpath=/data/mongodb3.0.1/log/mongodb.log --directoryperdb --maxConns 100 --logappend --replSet repl/192.168.66.89:50144 --fork


[root@localhost data]# /usr/local/mongodb3.0.1/bin/mongod --port 50144 --dbpath=/data/mongodb3.0.1/db_50144/ --logpath=/data/mongodb3.0.1/db_50144/mongodb.log --directoryperdb --maxConns 100 --logappend --replSet repl/192.168.66.89:40144 --fork


[root@localhost data]# /usr/local/mongodb3.0.1/bin/mongod --port 60144 --dbpath=/data/mongodb3.0.1/db_60144/ --logpath=/data/mongodb3.0.1/db_60144/mongodb.log --directoryperdb --maxConns 100 --logappend --replSet repl/192.168.66.89:40144 --fork

参数:

--oplogSize 日志操作文件的大(默认磁盘剩余空间的5%)

--dbpath   数据文件路径

--logpath  日志文件路径

--port        端口号,默认是27017

--replSet   复制集的名字,一个replica sets中的每个节点的这个参数都要用一个复制集名字,这里是test.

--replSet test/  这个后面跟的是其他standard节点的ip和端口

--maxConns   最大连接数

--fork       后台运行

--logappend   日志文件循环使用,如果日志文件已满,那么新日志覆盖最久日志。

任选一个节点登录mongodb数据库

[root@localhost data]# /usr/local/mongodb3.0.1/bin/mongo --port 40144
MongoDB shell version: 3.0.1
connecting to: 127.0.0.1:40144/test
Server has startup warnings: 
2015-05-08T09:06:42.270+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-05-08T09:06:42.270+0800 I CONTROL  [initandlisten] 
2015-05-08T09:06:42.270+0800 I CONTROL  [initandlisten] 
2015-05-08T09:06:42.270+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-05-08T09:06:42.270+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-05-08T09:06:42.270+0800 I CONTROL  [initandlisten] 
2015-05-08T09:06:42.270+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-05-08T09:06:42.270+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-05-08T09:06:42.270+0800 I CONTROL  [initandlisten]

#使用admin数据库

> use admin;
switched to db admin

#定义副本集配置变量并初始化,这里的 _id:”replSet” 和上面命令参数“--replSet repl” 要保持一样
> db.runCommand({"replSetInitiate":{"_id":"repl","members":[{"_id":1,"host":"192.168.66.89:40144"},{"_id":2,"host":"192.168.66.89:50144"},{"_id":3,"host":"192.168.66.89:60144"}]}});
{ "ok" : 1 }

#查看副本集信息
repl:OTHER> db._adminCommand("replSetGetStatus");
{
"set" : "repl",
"date" : ISODate("2015-05-08T01:18:55.619Z"),
"myState" : 1,
"members" : [
{
"_id" : 1,
"name" : "192.168.66.89:40144",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 776,
"optime" : Timestamp(1431047920, 1),
"optimeDate" : ISODate("2015-05-08T01:18:40Z"),
"electionTime" : Timestamp(1431047923, 1),
"electionDate" : ISODate("2015-05-08T01:18:43Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "192.168.66.89:50144",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 15,
"optime" : Timestamp(1431047920, 1),
"optimeDate" : ISODate("2015-05-08T01:18:40Z"),
"lastHeartbeat" : ISODate("2015-05-08T01:18:53.650Z"),
"lastHeartbeatRecv" : ISODate("2015-05-08T01:18:53.650Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "could not find member to sync from",
"configVersion" : 1
},
{
"_id" : 3,
"name" : "192.168.66.89:60144",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 15,
"optime" : Timestamp(1431047920, 1),
"optimeDate" : ISODate("2015-05-08T01:18:40Z"),
"lastHeartbeat" : ISODate("2015-05-08T01:18:53.650Z"),
"lastHeartbeatRecv" : ISODate("2015-05-08T01:18:53.649Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "could not find member to sync from",
"configVersion" : 1
}
],
"ok" : 1
}

二、测试

1、主节点写入数据,其他节点查询

repl:PRIMARY> use mtdamai;
switched to db mtdamai
repl:PRIMARY> db.user.insert({name:"wangxiuli"});
WriteResult({ "nInserted" : 1 })
repl:PRIMARY> db.user.find();
{ "_id" : ObjectId("554c237123db30ab1a233e4b"), "name" : "wangxiuli" }

#备节点查询发现已有数据
repl:SECONDARY> use mtdamai;
switched to db mtdamai
repl:SECONDARY> db.user.find();
{ "_id" : ObjectId("554c237123db30ab1a233e4b"), "name" : "wangxiuli" }


2、停止主节点
repl:SECONDARY> rs.status();
{
"set" : "repl",
"date" : ISODate("2015-05-08T02:51:25.273Z"),
"myState" : 2,
"members" : [
{
"_id" : 1,
"name" : "192.168.66.89:40144",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2015-05-08T02:51:23.984Z"),
"lastHeartbeatRecv" : ISODate("2015-05-08T02:50:55.975Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "Failed attempt to connect to 192.168.66.89:40144; couldn't connect to server 192.168.66.89:40144 (192.168.66.89), connection attempt failed",
"configVersion" : -1
},
{
"_id" : 2,
"name" : "192.168.66.89:50144",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 5563,
"optime" : Timestamp(1431053169, 2),
"optimeDate" : ISODate("2015-05-08T02:46:09Z"),
"lastHeartbeat" : ISODate("2015-05-08T02:51:24.227Z"),
"lastHeartbeatRecv" : ISODate("2015-05-08T02:51:24.120Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1431053458, 1),
"electionDate" : ISODate("2015-05-08T02:50:58Z"),
"configVersion" : 1
},
{
"_id" : 3,
"name" : "192.168.66.89:60144",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 5733,
"optime" : Timestamp(1431053169, 2),
"optimeDate" : ISODate("2015-05-08T02:46:09Z"),
"configVersion" : 1,
"self" : true
}
],
"ok" : 1
}


#发现主节点已经重新被选出
再来插入数据验证
repl:PRIMARY> db.user.insert({name:"chenglong"});
WriteResult({ "nInserted" : 1 })
repl:PRIMARY> db.user.find();
{ "_id" : ObjectId("554c237123db30ab1a233e4b"), "name" : "wangxiuli" }
{ "_id" : ObjectId("554c25202dcdffb154643e56"), "name" : "chenglong" }


#验证备节点不可写
repl:SECONDARY> db.user.insert({name:"liudehua"});
WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })


#查询数据已存在,说明数据已同步到其他节点
repl:SECONDARY> use mtdamai;
switched to db mtdamai
repl:SECONDARY> db.user.find();
Error: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
repl:SECONDARY> rs.slaveOk();
repl:SECONDARY> db.user.find();
{ "_id" : ObjectId("554c237123db30ab1a233e4b"), "name" : "wangxiuli" }
{ "_id" : ObjectId("554c25202dcdffb154643e56"), "name" : "chenglong" }


#再次启动被关闭的节点,该节点变为secondary
repl:SECONDARY> rs.status();
{
"set" : "repl",
"date" : ISODate("2015-05-08T07:54:20.607Z"),
"myState" : 2,
"syncingTo" : "192.168.66.89:50144",
"members" : [
{
"_id" : 1,
"name" : "192.168.66.89:40144",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 19,
"optime" : Timestamp(1431053600, 1),
"optimeDate" : ISODate("2015-05-08T02:53:20Z"),
"lastHeartbeat" : ISODate("2015-05-08T07:54:19.079Z"),
"lastHeartbeatRecv" : ISODate("2015-05-08T07:54:20.557Z"),
"pingMs" : 0,
"syncingTo" : "192.168.66.89:60144",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.66.89:50144",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 23738,
"optime" : Timestamp(1431053600, 1),
"optimeDate" : ISODate("2015-05-08T02:53:20Z"),
"lastHeartbeat" : ISODate("2015-05-08T07:54:20.152Z"),
"lastHeartbeatRecv" : ISODate("2015-05-08T07:54:20.105Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1431053458, 1),
"electionDate" : ISODate("2015-05-08T02:50:58Z"),
"configVersion" : 1
},
{
"_id" : 3,
"name" : "192.168.66.89:60144",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 23908,
"optime" : Timestamp(1431053600, 1),
"optimeDate" : ISODate("2015-05-08T02:53:20Z"),
"syncingTo" : "192.168.66.89:50144",
"configVersion" : 1,
"self" : true
}
],
"ok" : 1
}


#数据也同步过来了
repl:SECONDARY> use mtdamai;
switched to db mtdamai
repl:SECONDARY> db.user.find();
{ "_id" : ObjectId("554c237123db30ab1a233e4b"), "name" : "wangxiuli" }
{ "_id" : ObjectId("554c25202dcdffb154643e56"), "name" : "chenglong" }


你可能感兴趣的:(Redis)