mongodb高可用集羣副本集方式搭建

下載

wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel70-v4.2-latest.tgz

解壓縮

tar zxvf mongodb-linux-x86_64-rhel70-v4.2-latest.tgz -C /opt/module/

重命名

mv /opt/module/mongodb-linux-x86_64-rhel70-4.2.0-81-g7897a0a /opt/module/mongodb

增加環境變量

## MONGODB_HOME
export MONGODB_HOME= /opt/module/mongodb
export PATH=$PATH:$MONGODB_HOME/bin

啓動

[hadoop@hadoop11 mongodb]$ mkdir data
[hadoop@hadoop11 mongodb]$ mongod --dbpath=./data --logpath=./data/mongo.log --bind_ip=hadoop11 --fork --replSet=xiechuan
about to fork child process, waiting until server is ready for connections.
forked process: 11614
child process started successfully, parent exiting

配置

cfg = {
   _id: "xiechuan",
   members: [
   {
     _id: 0,
     host: 'hadoop11:27017',
     priority: 3
   },
   {
     _id: 1,
     host: 'hadoop12:27017',
     priority: 2
   },
   {
     _id: 2,
     host: 'hadoop13:27017',
     priority: 1
   },
   {
     _id: 3,
     host: 'hadoop14:27017',
     arbiterOnly: true
   }
  ]
 };
[hadoop@hadoop11 mongodb]$ mongo hadoop11:27017
> use admin
switched to db admin
> cfg = {
... _id: "xiechuan",
... members: [
...   {
...     _id: 0,
...     host: 'hadoop11:27017',
...     priority: 3
...   },
...   {
...     _id: 1,
...     host: 'hadoop12:27017',
...     priority: 2
...   },
...   {
...     _id: 2,
...     host: 'hadoop13:27017',
...     priority: 1
...   },
...   {
...     _id: 3,
...     host: 'hadoop14:27017',
...     arbiterOnly: true
...   }
...  ]
... };
{
	"_id" : "xiechuan",
	"members" : [
		{
			"_id" : 0,
			"host" : "hadoop11:27017",
			"priority" : 3
		},
		{
			"_id" : 1,
			"host" : "hadoop12:27017",
			"priority" : 2
		},
		{
			"_id" : 2,
			"host" : "hadoop13:27017",
			"priority" : 1
		},
		{
			"_id" : 3,
			"host" : "hadoop14:27017",
			"arbiterOnly" : true
		}
	]
}
> rs.initiate(cfg)
{
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1566798881, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1566798881, 1)
}

檢驗

xiechuan:SECONDARY> rs.status()
{
	"set" : "xiechuan",
	"date" : ISODate("2019-08-26T05:55:32.711Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1566798924, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2019-08-26T05:55:24.140Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1566798924, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2019-08-26T05:55:24.140Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1566798924, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1566798924, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2019-08-26T05:55:24.140Z"),
		"lastDurableWallTime" : ISODate("2019-08-26T05:55:24.140Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1566798894, 2),
	"lastStableCheckpointTimestamp" : Timestamp(1566798894, 2),
	"members" : [
		{
			"_id" : 0,
			"name" : "hadoop11:27017",
			"ip" : "192.168.17.11",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 393,
			"optime" : {
				"ts" : Timestamp(1566798924, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2019-08-26T05:55:24Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1566798892, 1),
			"electionDate" : ISODate("2019-08-26T05:54:52Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "hadoop12:27017",
			"ip" : "192.168.17.12",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 50,
			"optime" : {
				"ts" : Timestamp(1566798924, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1566798924, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2019-08-26T05:55:24Z"),
			"optimeDurableDate" : ISODate("2019-08-26T05:55:24Z"),
			"lastHeartbeat" : ISODate("2019-08-26T05:55:31.049Z"),
			"lastHeartbeatRecv" : ISODate("2019-08-26T05:55:30.847Z"),
			"pingMs" : NumberLong(1),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "hadoop11:27017",
			"syncSourceHost" : "hadoop11:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "hadoop13:27017",
			"ip" : "192.168.17.13",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 50,
			"optime" : {
				"ts" : Timestamp(1566798924, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1566798924, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2019-08-26T05:55:24Z"),
			"optimeDurableDate" : ISODate("2019-08-26T05:55:24Z"),
			"lastHeartbeat" : ISODate("2019-08-26T05:55:31.049Z"),
			"lastHeartbeatRecv" : ISODate("2019-08-26T05:55:31.047Z"),
			"pingMs" : NumberLong(1),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "hadoop11:27017",
			"syncSourceHost" : "hadoop11:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		},
		{
			"_id" : 3,
			"name" : "hadoop14:27017",
			"ip" : "192.168.17.14",
			"health" : 1,
			"state" : 7,
			"stateStr" : "ARBITER",
			"uptime" : 50,
			"lastHeartbeat" : ISODate("2019-08-26T05:55:31.049Z"),
			"lastHeartbeatRecv" : ISODate("2019-08-26T05:55:32.577Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1566798924, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1566798924, 1)
}

切換

kill 進程
xiechuan:SECONDARY> rs.status()
{
	"set" : "xiechuan",
	"date" : ISODate("2019-08-26T06:04:18.905Z"),
	"myState" : 1,
	"term" : NumberLong(4),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1566799414, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2019-08-26T06:03:34.228Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1566799414, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2019-08-26T06:03:34.228Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1566799457, 1),
			"t" : NumberLong(4)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1566799457, 1),
			"t" : NumberLong(4)
		},
		"lastAppliedWallTime" : ISODate("2019-08-26T06:04:17.364Z"),
		"lastDurableWallTime" : ISODate("2019-08-26T06:04:17.364Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1566799414, 1),
	"lastStableCheckpointTimestamp" : Timestamp(1566799414, 1),
	"members" : [
		{
			"_id" : 0,
			"name" : "hadoop11:27017",
			"ip" : "192.168.17.11",
			"health" : 0,
			"state" : 8,
			"stateStr" : "(not reachable/healthy)",
			"uptime" : 0,
			"optime" : {
				"ts" : Timestamp(0, 0),
				"t" : NumberLong(-1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(0, 0),
				"t" : NumberLong(-1)
			},
			"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
			"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
			"lastHeartbeat" : ISODate("2019-08-26T06:04:18.050Z"),
			"lastHeartbeatRecv" : ISODate("2019-08-26T06:03:43.542Z"),
			"pingMs" : NumberLong(1),
			"lastHeartbeatMessage" : "Error connecting to hadoop11:27017 (192.168.17.11:27017) :: caused by :: Connection refused",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : -1
		},
		{
			"_id" : 1,
			"name" : "hadoop12:27017",
			"ip" : "192.168.17.12",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 908,
			"optime" : {
				"ts" : Timestamp(1566799457, 1),
				"t" : NumberLong(4)
			},
			"optimeDate" : ISODate("2019-08-26T06:04:17Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1566799456, 1),
			"electionDate" : ISODate("2019-08-26T06:04:16Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 2,
			"name" : "hadoop13:27017",
			"ip" : "192.168.17.13",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 576,
			"optime" : {
				"ts" : Timestamp(1566799452, 1),
				"t" : NumberLong(3)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1566799452, 1),
				"t" : NumberLong(3)
			},
			"optimeDate" : ISODate("2019-08-26T06:04:12Z"),
			"optimeDurableDate" : ISODate("2019-08-26T06:04:12Z"),
			"lastHeartbeat" : ISODate("2019-08-26T06:04:18.044Z"),
			"lastHeartbeatRecv" : ISODate("2019-08-26T06:04:18.764Z"),
			"pingMs" : NumberLong(2),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 1
		},
		{
			"_id" : 3,
			"name" : "hadoop14:27017",
			"ip" : "192.168.17.14",
			"health" : 1,
			"state" : 7,
			"stateStr" : "ARBITER",
			"uptime" : 576,
			"lastHeartbeat" : ISODate("2019-08-26T06:04:18.044Z"),
			"lastHeartbeatRecv" : ISODate("2019-08-26T06:04:17.137Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1566799457, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1566799457, 1)
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章