MongoDB 4.0 Sharded Cluster部署

本文基于MongoDB 4.0介绍如何搭建shard集群服务,环境如下表所示:

1、创建相关目录

在三个节点分别创建以下目录:

[root@hdp06 ~]# mkdir -p /data/mongo/{config,router,shard} [root@hdp06 ~]# mkdir -p /data/mongo/config/{data,logs} [root@hdp06 ~]# mkdir -p /data/mongo/router/logs [root@hdp06 ~]# mkdir -p /data/mongo/shard/{data,logs} [root@hdp06 ~]# mkdir -p /data/mongo/shard/data/{shard1,shard2,shard3} [root@hdp06 ~]# chown -R mongod:mongod /data 2、配置Configsvr服务 2.1 创建配置文件 [root@hdp06 ~]# vi /data/mongo/config/mongodb.config net: bindIp: 0.0.0.0 port: 27017 processManagement: fork: "true" replication: replSetName: configRS sharding: clusterRole: configsvr storage: dbPath: /data/mongo/config/data systemLog: destination: file path: /data/mongo/config/logs/mongodb.log 2.2 同步配置文件 [root@hdp06 ~]# scp /data/mongo/config/mongodb.config hdp07:/data/mongo/config [root@hdp06 ~]# scp /data/mongo/config/mongodb.config hdp08:/data/mongo/config 2.3 启动configsvr服务 [root@hdp06 ~]# mongod -f /data/mongo/config/mongodb.config [root@hdp07 ~]# mongod -f /data/mongo/config/mongodb.config [root@hdp08 ~]# mongod -f /data/mongo/config/mongodb.config 2.4 初始化集群

在一个节点上执行集群初始化操作:

[root@hdp06 ~]# mongo >rs.initiate( { _id: "configRS", configsvr: true, members: [ { _id : 0, host : "hdp06.thinkjoy.tt:27017" }, { _id : 1, host : "hdp07.thinkjoy.tt:27017" }, { _id : 2, host : "hdp08.thinkjoy.tt:27017" } ] } ) --输出结果如下 { "ok" : 1, "operationTime" : Timestamp(1534816254, 1), "$gleStats" : { "lastOpTime" : Timestamp(1534816254, 1), "electionId" : ObjectId("000000000000000000000000") }, "lastCommittedOpTime" : Timestamp(0, 0), "$clusterTime" : { "clusterTime" : Timestamp(1534816254, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } --其他节点验证 [root@hdp07 ~]# mongo ...... configRS:SECONDARY> rs.status() { "set" : "configRS", "date" : ISODate("2018-08-21T01:52:59.734Z"), "myState" : 2, "term" : NumberLong(1), "syncingTo" : "hdp06.thinkjoy.tt:27017", "syncSourceHost" : "hdp06.thinkjoy.tt:27017", "syncSourceId" : 0, "configsvr" : true, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1534816364, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1534816364, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1534816364, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1534816364, 1), "t" : NumberLong(1) } }, "lastStableCheckpointTimestamp" : Timestamp(1534816327, 1), "members" : [ { "_id" : 0, "name" : "hdp06.thinkjoy.tt:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 115, "optime" : { "ts" : Timestamp(1534816364, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1534816364, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-08-21T01:52:44Z"), "optimeDurableDate" : ISODate("2018-08-21T01:52:44Z"), "lastHeartbeat" : ISODate("2018-08-21T01:52:57.945Z"), "lastHeartbeatRecv" : ISODate("2018-08-21T01:52:59.093Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1534816265, 1), "electionDate" : ISODate("2018-08-21T01:51:05Z"), "configVersion" : 1 }, { "_id" : 1, "name" : "hdp07.thinkjoy.tt:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 320, "optime" : { "ts" : Timestamp(1534816364, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-08-21T01:52:44Z"), "syncingTo" : "hdp06.thinkjoy.tt:27017", "syncSourceHost" : "hdp06.thinkjoy.tt:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 2, "name" : "hdp08.thinkjoy.tt:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 115, "optime" : { "ts" : Timestamp(1534816364, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1534816364, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-08-21T01:52:44Z"), "optimeDurableDate" : ISODate("2018-08-21T01:52:44Z"), "lastHeartbeat" : ISODate("2018-08-21T01:52:57.945Z"), "lastHeartbeatRecv" : ISODate("2018-08-21T01:52:57.944Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "hdp06.thinkjoy.tt:27017", "syncSourceHost" : "hdp06.thinkjoy.tt:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1 } ], "ok" : 1, "operationTime" : Timestamp(1534816364, 1), "$gleStats" : { "lastOpTime" : Timestamp(0, 0), "electionId" : ObjectId("000000000000000000000000") }, "lastCommittedOpTime" : Timestamp(1534816364, 1), "$clusterTime" : { "clusterTime" : Timestamp(1534816364, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } 3、配置Sharded服务 3.1 创建配置文件 root@hdp06 ~]# vi /data/mongo/shard/data/shard1/mongod.shard1 net: bindIp: 0.0.0.0 port: 27019 processManagement: fork: "true" replication: replSetName: myShard_1 sharding: clusterRole: shardsvr storage: dbPath: /data/mongo/shard/data/shard1 systemLog: destination: file path: /data/mongo/shard/logs/shard1/mongodbs1.log [root@hdp06 ~]# vi /data/mongo/shard/data/shard2/mongod.shard2 net: bindIp: 0.0.0.0 port: 27020 processManagement: fork: "true" replication: replSetName: myShard_2 sharding: clusterRole: shardsvr storage: dbPath: /data/mongo/shard/data/shard2 systemLog: destination: file path: /data/mongo/shard/logs/shard2/mongodbs2.log [root@hdp06 ~]# vi /data/mongo/shard/data/shard3/mongod.shard3 net: bindIp: 0.0.0.0 port: 27021 processManagement: fork: "true" replication: replSetName: myShard_3 sharding: clusterRole: shardsvr storage: dbPath: /data/mongo/shard/data/shard3 systemLog: destination: file path: /data/mongo/shard/logs/shard3/mongodbs3.log 3.2 同步配置文件 [root@hdp06 ~]# scp -r /data/mongo/shard/data/* hdp07:/data/mongo/shard/data/ [root@hdp06 ~]# scp -r /data/mongo/shard/data/* hdp08:/data/mongo/shard/data/ 3.3 启动sharded服务 [root@hdp06 ~]# mongod -f /data/mongo/shard/data/shard1/mongod.shard1 [root@hdp06 ~]# mongod -f /data/mongo/shard/data/shard2/mongod.shard2 [root@hdp06 ~]# mongod -f /data/mongo/shard/data/shard3/mongod.shard3 [root@hdp07 ~]# mongod -f /data/mongo/shard/data/shard1/mongod.shard1 [root@hdp07 ~]# mongod -f /data/mongo/shard/data/shard2/mongod.shard2 [root@hdp07 ~]# mongod -f /data/mongo/shard/data/shard3/mongod.shard3 [root@hdp08 ~]# mongod -f /data/mongo/shard/data/shard1/mongod.shard1 [root@hdp08 ~]# mongod -f /data/mongo/shard/data/shard2/mongod.shard2 [root@hdp08 ~]# mongod -f /data/mongo/shard/data/shard3/mongod.shard3 3.4 初始化sharded服务

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/18605470d21fc92d85b3f7ca86c1b7ca.html