程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> DB2數據庫 >> DB2教程 >> MongoDB之復制集(二)搭建

MongoDB之復制集(二)搭建

編輯:DB2教程

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" }



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved