CentOS 7 下 MongoDB 分片副本集羣搭建

MongoDB 下載

在這裏插入圖片描述

服務器準備

  • 服務器資源
IP HostName CPU Mem Data Roles Port Desc
192.168.8.75 auto01 4核 16GB 200GB
/home
mongos
config
shard1
shard2
shard3
20000
21000
27001
27002
27003
shard1 master
192.168.8.76 auto02 4核 16GB 200GB
/home
mongos
config
shard1
shard2
shard3
20000
21000
27001
27002
27003
shard2 master
192.168.8.77 auto03 4核 16GB 200GB
/home
mongos
config
shard1
shard2
shard3
20000
21000
27001
27002
27003
shard3 master
  • 防火牆關閉 – 所有節點,auto01 爲例
[root@auto01 ~]# systemctl stop firewalld && systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.

在這裏插入圖片描述

上傳 MongoDB 安裝包

  • 安裝包目錄創建 – 所有節點,auto01 爲例
[root@auto01 ~]# mkdir -p /home/soft
  • 上傳安裝包到目錄 – 所有節點
[root@auto01 ~]# ll /home/soft/ 		# 節點1
total 129596
-rw-r--r--. 1 root root 132702741 Apr 15 18:05 mongodb-linux-x86_64-rhel70-4.2.5.tgz
[root@auto02 ~]# ll /home/soft/ 		# 節點2
total 129596
-rw-r--r--. 1 root root 132702741 Apr 15 18:06 mongodb-linux-x86_64-rhel70-4.2.5.tgz
[root@auto03 ~]# ll /home/soft/ 		# 節點3
total 129596
-rw-r--r--. 1 root root 132702741 Apr 15 18:07 mongodb-linux-x86_64-rhel70-4.2.5.tgz
  • 解壓安裝包 – 所有節點,auto01 爲例
[root@auto01 soft]# cd /home/soft 		# 進入目錄
[root@auto01 soft]# tar -zxf mongodb-linux-x86_64-rhel70-4.2.5.tgz 		# 解壓安裝包
  • 遷移 MongoDB 目錄 – 所有節點,auto01 爲例
[root@auto01 soft]# mv /home/soft/mongodb-linux-x86_64-rhel70-4.2.5 /home/mongodb
[root@auto01 soft]# ll /home/mongodb/
  • 創建數據目錄 – 所有節點,auto01 爲例
[root@auto01 ~]# mkdir -p /home/mongodb/conf
[root@auto01 ~]# mkdir -p /home/mongodb/data/mongos/log
[root@auto01 ~]# mkdir -p /home/mongodb/data/config/{data,log}
[root@auto01 ~]# mkdir -p /home/mongodb/data/shard1/{data,log}
[root@auto01 ~]# mkdir -p /home/mongodb/data/shard2/{data,log}
[root@auto01 ~]# mkdir -p /home/mongodb/data/shard3/{data,log}
  • MongoDB 環境變量配置 – 所有節點,auto01 爲例
[root@auto01 ~]# echo " " >> /etc/profile
[root@auto01 ~]# echo "# Set MongoDB" >> /etc/profile
[root@auto01 ~]# echo " " >> /etc/profile
[root@auto01 ~]# echo "export MONGODB_HOME=/home/mongodb" >> /etc/profile
[root@auto01 ~]# echo "export PATH=\$MONGODB_HOME/bin:$PATH" >> /etc/profile
[root@auto01 ~]# vi /etc/profile
  • 刷新環境變量 – 所有節點,auto01 爲例
[root@auto01 ~]# source /etc/profile
[root@auto01 ~]# echo $MONGODB_HOME
/home/mongodb

MongoDB 配置文件配置

Config Server
  • config server 配置 – 所有節點,auto01 爲例
[root@auto01 ~]# cat /home/mongodb/conf/config.conf 
## content
systemLog:
  destination: file
  logAppend: true
  path: /home/mongodb/data/config/log/config.log
# Where and how to store data.
storage:
  dbPath: /home/mongodb/data/config/data
  journal:
    enabled: true
# how the process runs
processManagement:
  fork: true
  pidFilePath: /home/mongodb/data/config/log/configsrv.pid
# network interfaces
net:
  port: 21000
  bindIp: 0.0.0.0
#operationProfiling:
replication:
  replSetName: config        

sharding:
  clusterRole: configsvr
  • config server 啓動 – 所有節點,auto01 爲例
[root@auto01 ~]# /home/mongodb/bin/mongod -f /home/mongodb/conf/config.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2903
child process started successfully, parent exiting

在這裏插入圖片描述

  • config server 副本集配置 – 192.168.8.75
[root@auto01 ~]# mongo --host 192.168.8.75 --port 21000
> config = {
	_id : "config",
	members : [
		{_id : 0, host : "192.168.8.75:21000" },
		{_id : 1, host : "192.168.8.76:21000" },
		{_id : 2, host : "192.168.8.77:21000" }
	]
}

在這裏插入圖片描述

  • config server 副本集初始化 – 192.168.8.75
> rs.initiate(config);

在這裏插入圖片描述

  • config server 狀態查看 – 192.168.8.75
config:SECONDARY> rs.status()
{
	"set" : "config",
	"date" : ISODate("2020-04-15T14:07:01.584Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"configsvr" : true,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1586959619, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2020-04-15T14:06:59.430Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1586959619, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2020-04-15T14:06:59.430Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1586959619, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1586959619, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2020-04-15T14:06:59.430Z"),
		"lastDurableWallTime" : ISODate("2020-04-15T14:06:59.430Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1586959589, 1),
	"lastStableCheckpointTimestamp" : Timestamp(1586959589, 1),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2020-04-15T14:05:25.443Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1586959515, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2020-04-15T14:05:25.685Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2020-04-15T14:05:29.768Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.8.75:21000",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 844,
			"optime" : {
				"ts" : Timestamp(1586959619, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-04-15T14:06:59Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1586959525, 1),
			"electionDate" : ISODate("2020-04-15T14:05:25Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.8.76:21000",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 106,
			"optime" : {
				"ts" : Timestamp(1586959619, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1586959619, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-04-15T14:06:59Z"),
			"optimeDurableDate" : ISODate("2020-04-15T14:06:59Z"),
			"lastHeartbeat" : ISODate("2020-04-15T14:07:01.567Z"),
			"lastHeartbeatRecv" : ISODate("2020-04-15T14:07:00.513Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.8.75:21000",
			"syncSourceHost" : "192.168.8.75:21000",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.8.77:21000",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 106,
			"optime" : {
				"ts" : Timestamp(1586959619, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1586959619, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-04-15T14:06:59Z"),
			"optimeDurableDate" : ISODate("2020-04-15T14:06:59Z"),
			"lastHeartbeat" : ISODate("2020-04-15T14:07:01.565Z"),
			"lastHeartbeatRecv" : ISODate("2020-04-15T14:07:00.449Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.8.75:21000",
			"syncSourceHost" : "192.168.8.75:21000",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"$gleStats" : {
		"lastOpTime" : Timestamp(1586959515, 1),
		"electionId" : ObjectId("7fffffff0000000000000001")
	},
	"lastCommittedOpTime" : Timestamp(1586959619, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1586959619, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1586959619, 1)
}
Shard 1
  • shard 1 配置 – 所有節點,auto01 爲例
[root@auto01 ~]# cat /home/mongodb/conf/shard1.conf 
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/mongodb/data/shard1/log/shard1.log
# Where and how to store data.
storage:
  dbPath: /home/mongodb/data/shard1/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
       cacheSizeGB: 20

# how the process runs
processManagement:
  fork: true
  pidFilePath: /home/mongodb/data/shard1/log/shard1.pid
# network interfaces
net:
  port: 27001
  bindIp: 0.0.0.0

#operationProfiling:
replication:
  replSetName: shard1
sharding:
  clusterRole: shardsvr
  • shard 1 啓動 – 所有節點,auto01 爲例
[root@auto01 ~]# /home/mongodb/bin/mongod -f /home/mongodb/conf/shard1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3162
child process started successfully, parent exiting

在這裏插入圖片描述

  • shard 1 副本集配置 – 192.168.8.75
[root@auto01 ~]# mongo --host 192.168.8.75 --port 27001
> use admin
> config = {
	_id : "shard1",
	members : [
		{_id : 0, host : "192.168.8.75:27001" },
		{_id : 1, host : "192.168.8.76:27001" },
		{_id : 2, host : "192.168.8.77:27001" }
	]
}

在這裏插入圖片描述

  • shard 1 副本集初始化 – 192.168.8.75
> rs.initiate(config);

在這裏插入圖片描述

  • shard 1 狀態查看 – 192.168.8.75
shard1:SECONDARY> rs.status()
{
	"set" : "shard1",
	"date" : ISODate("2020-04-15T14:34:23.655Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1586961257, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2020-04-15T14:34:17.410Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1586961257, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2020-04-15T14:34:17.410Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1586961257, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1586961257, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2020-04-15T14:34:17.410Z"),
		"lastDurableWallTime" : ISODate("2020-04-15T14:34:17.410Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1586961247, 3),
	"lastStableCheckpointTimestamp" : Timestamp(1586961247, 3),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2020-04-15T14:34:07.197Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1586961236, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2020-04-15T14:34:07.409Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2020-04-15T14:34:08.671Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.8.75:27001",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 1277,
			"optime" : {
				"ts" : Timestamp(1586961257, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-04-15T14:34:17Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1586961247, 1),
			"electionDate" : ISODate("2020-04-15T14:34:07Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.8.76:27001",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 26,
			"optime" : {
				"ts" : Timestamp(1586961257, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1586961257, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-04-15T14:34:17Z"),
			"optimeDurableDate" : ISODate("2020-04-15T14:34:17Z"),
			"lastHeartbeat" : ISODate("2020-04-15T14:34:23.231Z"),
			"lastHeartbeatRecv" : ISODate("2020-04-15T14:34:22.399Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.8.75:27001",
			"syncSourceHost" : "192.168.8.75:27001",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.8.77:27001",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 26,
			"optime" : {
				"ts" : Timestamp(1586961257, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1586961257, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-04-15T14:34:17Z"),
			"optimeDurableDate" : ISODate("2020-04-15T14:34:17Z"),
			"lastHeartbeat" : ISODate("2020-04-15T14:34:23.227Z"),
			"lastHeartbeatRecv" : ISODate("2020-04-15T14:34:22.399Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.8.75:27001",
			"syncSourceHost" : "192.168.8.75:27001",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1586961257, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1586961257, 1)
}
Shard 2
  • shard 2 配置 – 所有節點,auto01 爲例
[root@auto01 ~]# cat /home/mongodb/conf/shard2.conf 
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/mongodb/data/shard2/log/shard2.log
# Where and how to store data.
storage:
  dbPath: /home/mongodb/data/shard2/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
       cacheSizeGB: 20

# how the process runs
processManagement:
  fork: true
  pidFilePath: /home/mongodb/data/shard2/log/shard2.pid
# network interfaces
net:
  port: 27002
  bindIp: 0.0.0.0

#operationProfiling:
replication:
  replSetName: shard2
sharding:
  clusterRole: shardsvr
  • shard 2 啓動 – 所有節點,auto01 爲例
[root@auto01 ~]# /home/mongodb/bin/mongod -f /home/mongodb/conf/shard2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3271
child process started successfully, parent exiting

在這裏插入圖片描述

  • shard 2 副本集配置 – 192.168.8.75
[root@auto01 ~]# mongo --host 192.168.8.75 --port 27002
> use admin
> config = {
	_id : "shard2",
	members : [
		{_id : 0, host : "192.168.8.75:27002" },
		{_id : 1, host : "192.168.8.76:27002" },
		{_id : 2, host : "192.168.8.77:27002" }
	]
}

在這裏插入圖片描述

  • shard 2 副本集初始化 – 192.168.8.75
> rs.initiate(config);

在這裏插入圖片描述

  • shard 2 狀態查看 – 192.168.8.75
shard2:SECONDARY> rs.status();
{
	"set" : "shard2",
	"date" : ISODate("2020-04-15T14:40:59.285Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1586961656, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2020-04-15T14:40:56.652Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1586961656, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2020-04-15T14:40:56.652Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1586961656, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1586961656, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2020-04-15T14:40:56.652Z"),
		"lastDurableWallTime" : ISODate("2020-04-15T14:40:56.652Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1586961646, 3),
	"lastStableCheckpointTimestamp" : Timestamp(1586961646, 3),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2020-04-15T14:40:46.417Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1586961634, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2020-04-15T14:40:46.651Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2020-04-15T14:40:47.729Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.8.75:27002",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 210,
			"optime" : {
				"ts" : Timestamp(1586961656, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-04-15T14:40:56Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1586961646, 1),
			"electionDate" : ISODate("2020-04-15T14:40:46Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.8.76:27002",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 24,
			"optime" : {
				"ts" : Timestamp(1586961656, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1586961656, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-04-15T14:40:56Z"),
			"optimeDurableDate" : ISODate("2020-04-15T14:40:56Z"),
			"lastHeartbeat" : ISODate("2020-04-15T14:40:58.488Z"),
			"lastHeartbeatRecv" : ISODate("2020-04-15T14:40:57.486Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.8.75:27002",
			"syncSourceHost" : "192.168.8.75:27002",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.8.77:27002",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 24,
			"optime" : {
				"ts" : Timestamp(1586961656, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1586961656, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-04-15T14:40:56Z"),
			"optimeDurableDate" : ISODate("2020-04-15T14:40:56Z"),
			"lastHeartbeat" : ISODate("2020-04-15T14:40:58.491Z"),
			"lastHeartbeatRecv" : ISODate("2020-04-15T14:40:57.451Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.8.75:27002",
			"syncSourceHost" : "192.168.8.75:27002",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1586961656, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1586961656, 1)
}
Shard3
  • shard3 配置 – 所有節點,auto01 爲例
[root@auto01 ~]# cat /home/mongodb/conf/shard3.conf 
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/mongodb/data/shard3/log/shard3.log
# Where and how to store data.
storage:
  dbPath: /home/mongodb/data/shard3/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
       cacheSizeGB: 20

# how the process runs
processManagement:
  fork: true
  pidFilePath: /home/mongodb/data/shard3/log/shard3.pid
# network interfaces
net:
  port: 27003
  bindIp: 0.0.0.0

#operationProfiling:
replication:
  replSetName: shard3
sharding:
  clusterRole: shardsvr
  • shard 3 啓動 – 所有節點,auto01 爲例
[root@auto01 ~]# /home/mongodb/bin/mongod -f /home/mongodb/conf/shard3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3364
child process started successfully, parent exiting

在這裏插入圖片描述

  • shard 3 副本集配置 – 192.168.8.75
[root@auto01 ~]# mongo --host 192.168.8.75 --port 27003
> use admin
> config = {
	_id : "shard3",
	members : [
		{_id : 0, host : "192.168.8.75:27003" },
		{_id : 1, host : "192.168.8.76:27003" },
		{_id : 2, host : "192.168.8.77:27003" }
	]
}

在這裏插入圖片描述

  • shard 3 副本集初始化 – 192.168.8.75
> rs.initiate(config);

在這裏插入圖片描述

  • shard 3 狀態查看 – 192.168.8.75
shard3:SECONDARY> rs.status();
{
	"set" : "shard3",
	"date" : ISODate("2020-04-15T14:45:56.071Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1586961949, 3),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2020-04-15T14:45:49.469Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1586961949, 3),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2020-04-15T14:45:49.469Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1586961949, 3),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1586961949, 3),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2020-04-15T14:45:49.469Z"),
		"lastDurableWallTime" : ISODate("2020-04-15T14:45:49.469Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1586961949, 3),
	"lastStableCheckpointTimestamp" : Timestamp(1586961949, 3),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2020-04-15T14:45:49.256Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1586961938, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2020-04-15T14:45:49.469Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2020-04-15T14:45:50.481Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.8.75:27003",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 204,
			"optime" : {
				"ts" : Timestamp(1586961949, 3),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-04-15T14:45:49Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1586961949, 1),
			"electionDate" : ISODate("2020-04-15T14:45:49Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.8.76:27003",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 17,
			"optime" : {
				"ts" : Timestamp(1586961949, 3),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1586961949, 3),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-04-15T14:45:49Z"),
			"optimeDurableDate" : ISODate("2020-04-15T14:45:49Z"),
			"lastHeartbeat" : ISODate("2020-04-15T14:45:55.294Z"),
			"lastHeartbeatRecv" : ISODate("2020-04-15T14:45:54.375Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.8.75:27003",
			"syncSourceHost" : "192.168.8.75:27003",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.8.77:27003",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 17,
			"optime" : {
				"ts" : Timestamp(1586961949, 3),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1586961949, 3),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-04-15T14:45:49Z"),
			"optimeDurableDate" : ISODate("2020-04-15T14:45:49Z"),
			"lastHeartbeat" : ISODate("2020-04-15T14:45:55.295Z"),
			"lastHeartbeatRecv" : ISODate("2020-04-15T14:45:54.269Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.8.75:27003",
			"syncSourceHost" : "192.168.8.75:27003",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1586961949, 3),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1586961949, 3)
}
Mongos Server
  • mongos server 配置 – 所有節點,auto01 爲例
[root@auto01 ~]# cat /home/mongodb/conf/mongos.conf 
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/mongodb/data/mongos/log/mongos.log

# how the process runs
processManagement:
  fork: true
  pidFilePath: /home/mongodb/data/mongos/log/mongos.pid

# network interfaces
net:
  port: 20000
  bindIp: 0.0.0.0

#replication:
sharding:
  configDB: config/192.168.8.75:21000,192.168.8.76:21000,192.168.8.77:21000
  • mongos 啓動 – 所有節點,auto01 爲例
[root@auto01 ~]# /home/mongodb/bin/mongos -f /home/mongodb/conf/mongos.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3468
child process started successfully, parent exiting

在這裏插入圖片描述

  • mongos 添加分片信息 – 192.168.8.75
[root@auto01 ~]# mongo --host 192.168.8.75 --port 20000
mongos> sh.addShard("shard1/192.168.8.75:27001,192.168.8.76:27001,192.168.8.77:27001")
mongos> sh.addShard("shard2/192.168.8.75:27002,192.168.8.76:27002,192.168.8.77:27002")
mongos> sh.addShard("shard3/192.168.8.75:27003,192.168.8.76:27003,192.168.8.77:27003")

在這裏插入圖片描述

  • mongos 狀態查看 – 192.168.8.75
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("5e9714a907e090051ac993df")
  }
  shards:
        {  "_id" : "shard1",  "host" : "shard1/192.168.8.75:27001,192.168.8.76:27001,192.168.8.77:27001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/192.168.8.75:27002,192.168.8.76:27002,192.168.8.77:27002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/192.168.8.75:27003,192.168.8.76:27003,192.168.8.77:27003",  "state" : 1 }
  active mongoses:
        "4.2.5" : 3
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard1	1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)

在這裏插入圖片描述

啓用訪問控制

線上環境集羣不可能使用免認證的方式,都要開啓安全認證。MongoDB在開啓了訪問控制後,只有一次添加用戶的機會,此後的操作都需要先認證通過。爲了方便,我們先添加用戶,然後再開啓訪問控制

添加用戶

連接上mongos添加的用戶會保存在config副本集中,但是不會保存到shard副本集,因此添加用戶的操作需要分別在config、shard01、shard02、shard03上執行。

  • config 副本集 – 連接
[root@auto01 ~]# mongo --host 192.168.8.75 --port 21000
config:PRIMARY> use admin
switched to db admin
  • config 副本集 – 添加管理員賬戶
config:PRIMARY> db.createUser(
	{
		user: "admin",
		pwd: "admin",
		roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"]
	}
)

在這裏插入圖片描述

  • shard 1 副本集 – 連接 && 添加管理員賬戶
[root@auto01 ~]# mongo --host 192.168.8.75 --port 27001
shard1:PRIMARY> use admin
switched to db admin
shard1:PRIMARY> db.createUser(
	{
		user: "admin",
		pwd: "admin",
		roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"]
	}
)

在這裏插入圖片描述

  • shard 2 副本集 – 連接 && 添加管理員賬戶
[root@auto01 ~]# mongo --host 192.168.8.75 --port 27002
shard2:PRIMARY> use admin
switched to db admin
shard2:PRIMARY> db.createUser(
	{
		user: "admin",
		pwd: "admin",
		roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"]
	}
)

在這裏插入圖片描述

  • shard 3 副本集 – 連接 && 添加管理員賬戶
[root@auto01 ~]# mongo --host 192.168.8.75 --port 27003
shard3:PRIMARY> use admin
switched to db admin
shard3:PRIMARY> db.createUser(
	{
		user: "admin",
		pwd: "admin",
		roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"]
	}
)

在這裏插入圖片描述

添加控制文件
  • 創建祕鑰文件目錄 – 所有節點,auto01 爲例
[root@auto01 ~]# mkdir /home/mongodb/key
  • keyfile 安全文件生成 – 192.168.8.75 生成,分發到其他節點
[root@auto01 ~]# openssl rand -base64 756 > /home/mongodb/key/mongo_auth.key
[root@auto01 ~]# scp /home/mongodb/key/mongo_auth.key [email protected]:/home/mongodb/key
[email protected]'s password: 		# 需要輸入密碼
  • keyfile 安全文件賦權 – 所有節點,auto01 爲例
[root@auto01 ~]# chmod 0600 /home/mongodb/key/mongo_auth.key
  • mongos.conf 添加 security 配置 – 所有節點,auto01 爲例
[root@auto01 ~]# echo " " >> /home/mongodb/conf/mongos.conf
[root@auto01 ~]# echo "# set security file" >> /home/mongodb/conf/mongos.conf
[root@auto01 ~]# echo "security:" >> /home/mongodb/conf/mongos.conf
[root@auto01 ~]# echo "  keyFile: /home/mongodb/key/mongo_auth.key" >> /home/mongodb/conf/mongos.conf

在這裏插入圖片描述

  • config.conf 添加 security 配置 – 所有節點,auto01 爲例
[root@auto01 ~]# echo " " >> /home/mongodb/conf/config.conf
[root@auto01 ~]# echo "# set security file" >> /home/mongodb/conf/config.conf
[root@auto01 ~]# echo "security:" >> /home/mongodb/conf/config.conf
[root@auto01 ~]# echo "  authorization: enabled" >> /home/mongodb/conf/config.conf
[root@auto01 ~]# echo "  keyFile: /home/mongodb/key/mongo_auth.key" >> /home/mongodb/conf/config.conf

在這裏插入圖片描述

  • shard1.conf 添加 security 配置 – 所有節點,auto01 爲例
[root@auto01 ~]# echo " " >> /home/mongodb/conf/shard1.conf
[root@auto01 ~]# echo "# set security file" >> /home/mongodb/conf/shard1.conf
[root@auto01 ~]# echo "security:" >> /home/mongodb/conf/shard1.conf
[root@auto01 ~]# echo "  authorization: enabled" >> /home/mongodb/conf/shard1.conf
[root@auto01 ~]# echo "  keyFile: /home/mongodb/key/mongo_auth.key" >> /home/mongodb/conf/shard1.conf

在這裏插入圖片描述

  • shard2.conf 添加 security 配置 – 所有節點,auto01 爲例
[root@auto01 ~]# echo " " >> /home/mongodb/conf/shard2.conf
[root@auto01 ~]# echo "# set security file" >> /home/mongodb/conf/shard2.conf
[root@auto01 ~]# echo "security:" >> /home/mongodb/conf/shard2.conf
[root@auto01 ~]# echo "  authorization: enabled" >> /home/mongodb/conf/shard2.conf
[root@auto01 ~]# echo "  keyFile: /home/mongodb/key/mongo_auth.key" >> /home/mongodb/conf/shard2.conf

在這裏插入圖片描述

  • shard3.conf 添加 security 配置 – 所有節點,auto01 爲例
[root@auto01 ~]# echo " " >> /home/mongodb/conf/shard3.conf
[root@auto01 ~]# echo "# set security file" >> /home/mongodb/conf/shard3.conf
[root@auto01 ~]# echo "security:" >> /home/mongodb/conf/shard3.conf
[root@auto01 ~]# echo "  authorization: enabled" >> /home/mongodb/conf/shard3.conf
[root@auto01 ~]# echo "  keyFile: /home/mongodb/key/mongo_auth.key" >> /home/mongodb/conf/shard3.conf

在這裏插入圖片描述

  • 重啓服務 – 所有節點,auto01 爲例
# 殺進程
[root@auto01 ~]# kill -9 `ps -ef | grep mongo | awk -F ' ' '{print $2}'`
# 啓動
[root@auto01 ~]# /home/mongodb/bin/mongod -f /home/mongodb/conf/config.conf && /home/mongodb/bin/mongod -f /home/mongodb/conf/shard1.conf && /home/mongodb/bin/mongod -f /home/mongodb/conf/shard2.conf && /home/mongodb/bin/mongod -f /home/mongodb/conf/shard3.conf && /home/mongodb/bin/mongos -f /home/mongodb/conf/mongos.conf

在這裏插入圖片描述

遇到問題

  • 集羣之間訪問連接異常 – 關閉防火牆解決
> rs.initiate(config);
{
	"operationTime" : Timestamp(0, 0),
	"ok" : 0,
	"errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: 192.168.8.76:21000 failed with Error connecting to 192.168.8.76:21000 :: caused by :: No route to host, 192.168.8.77:21000 failed with Error connecting to 192.168.8.77:21000 :: caused by :: No route to host",
	"code" : 74,
	"codeName" : "NodeNotFound",
	"$gleStats" : {
		"lastOpTime" : Timestamp(0, 0),
		"electionId" : ObjectId("000000000000000000000000")
	},
	"lastCommittedOpTime" : Timestamp(0, 0),
	"$clusterTime" : {
		"clusterTime" : Timestamp(0, 0),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章