Deploy A MongoDB 4.0 sharded Cluster

Source: Internet
Author: User
Tags mkdir uuid

This article describes how to build a shard Cluster service based on MongoDB 4.0, as shown in the following table:

1. Create related catalogue

Create the following directories in three separate points:

[[email protected] ~]# mkdir -p /data/mongo/{config,router,shard}[[email protected] ~]# mkdir -p /data/mongo/config/{data,logs}[[email protected] ~]# mkdir -p /data/mongo/router/logs[[email protected] ~]# mkdir -p /data/mongo/shard/{data,logs}[[email protected] ~]# mkdir -p /data/mongo/shard/data/{shard1,shard2,shard3}[[email protected] ~]# chown -R mongod:mongod /data
2. Configuring the CONFIGSVR service 2.1 creating a configuration file
[[email protected] ~]# vi /data/mongo/config/mongodb.config net:  bindIp: 0.0.0.0  port: 27017processManagement:  fork: "true"replication:  replSetName: configRSsharding:  clusterRole: configsvrstorage:  dbPath: /data/mongo/config/datasystemLog:  destination: file  path: /data/mongo/config/logs/mongodb.log
2.2 Synchronizing the configuration file
[[email protected] ~]# scp /data/mongo/config/mongodb.config hdp07:/data/mongo/config[[email protected] ~]# scp /data/mongo/config/mongodb.config hdp08:/data/mongo/config
2.3 Starting the CONFIGSVR service
2.4 Initializing a cluster

Perform cluster initialization on a single node:

[[email protected] ~]# 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.thi  nkjoy.tt:27017 "}]})--the output is as follows {" OK ": 1," Operationtime ": Timestamp (1534816254, 1)," $gleStats " : {"Lastoptime": Timestamp (1534816254, 1), "Electionid": ObjectId ("000000000000000000000 ")}," Lastcommittedoptime ": Timestamp (0, 0)," $clusterTime ": {" Clustertime ": T Imestamp (1534816254, 1), "signature": {"hash": Bindata (0, "aaaaaaaaaaaaaaaaaaaaaaa Aaaa= ")," KeyId ": Numberlong (0)}}}--other node validation [[email protected] ~]# Mong         O......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": N Umberlong (1)}, "Laststablecheckpointtimestamp": Timestamp (1534816327, 1), "members  " : [                {                      "_id": 0, "name": "hdp06.thinkjoy.tt:27017", "health ": 1," state ": 1," Statestr ":" PRIMARY "," uptime "                                : "Optime": {"ts": Timestamp (1534816364, 1),                                "T": Numberlong (1)}, "Optimedurable": { "TS": Timestamp (1534816364, 1), "T": Numberlong (1)} , "Optimedate": Isodate ("2018-08-21t01:52:44z"), "optimedurabledate": ISOD                        Ate ("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": Timestam P (1534816265, 1), "Electiondate": Isodate ("2018-08-21t01:51:05z"), "CONFIGV Ersion ": 1}, {" _id ": 1," name ":" Hdp07.th inkjoy.tt:27017 "," Health ": 1," state ": 2," statest                                R ":" Secondary "," uptime ":" The "," 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, "infomes Sage ":", "ConfigVersion": 1, "Self": true, "last Heartbeatmessage ":"}, {"_id": 2, "name"                        : "hdp08.thinkjoy.tt:27017", "Health": 1, "state": 2,                                "Statestr": "Secondary", "uptime": +, "Optime": {                         "TS": Timestamp (1534816364, 1), "T": Numberlong (1)},                                "Optimedurable": {"ts": Timestamp (1534816364, 1), "T": Numberlong (1)}, "Optimedate": Isodate ("2018-08-21t0             1:52:44z "),           "Optimedurabledate": Isodate ("2018-08-21t01:52:44z"), "Lastheartbeat": Isodate ("2018-0                        8-21t01:52:57.945z ")," Lastheartbeatrecv ": Isodate (" 2018-08-21t01:52:57.944z "), "Pingms": Numberlong (0), "lastheartbeatmessage": "", "syncingto": "H dp06.thinkjoy.tt:27017 "," Syncsourcehost ":" hdp06.thinkjoy.tt:27017 "," Sync        SourceID ": 0," InfoMessage ":" "," ConfigVersion ": 1} ], "OK": 1, "Operationtime": Timestamp (1534816364, 1), "$gleStats": {"Lastoptim E ": Timestamp (0, 0)," Electionid ": ObjectId (" 000000000000000000000000 ")}," Lastcommittedop                Time ": Timestamp (1534816364, 1)," $clusterTime ": {" Clustertime ": Timestamp (1534816364, 1), "Signature":{"Hash": Bindata (0, "aaaaaaaaaaaaaaaaaaaaaaaaaaa="), "KeyId": Numberlong (0 )                }        }}
3. Configuring the Sharded service 3.1 creating a configuration file
[email protected] ~]# vi/data/mongo/shard/data/shard1/mongod.shard1net:bindip:0.0.0.0 Port: 27019processmanagement:fork: "True" Replication:replSetName:myShard_1sharding:clusterRole:shardsvrstorage:dbPath :/data/mongo/shard/data/shard1systemlog:destination:file Path:/data/mongo/shard/logs/shard1/mongodbs1.log[[ Email protected] ~]# vi/data/mongo/shard/data/shard2/mongod.shard2net:bindip:0.0.0.0 Port: 27020processmanagement:fork: "True" Replication:replSetName:myShard_2sharding:clusterRole:shardsvrstorage:dbPath :/data/mongo/shard/data/shard2systemlog:destination:file Path:/data/mongo/shard/logs/shard2/mongodbs2.log[[ Email protected] ~]# vi/data/mongo/shard/data/shard3/mongod.shard3net:bindip:0.0.0.0 Port: 27021processmanagement:fork: "True" Replication:replSetName:myShard_3sharding:clusterRole:shardsvrstorage:dbPath :/data/mongo/shard/data/shard3systemlog:destination:file Path:/DATA/MONGO/SHARD/LOGS/SHARD3/MONGODBS3. log 
3.2 Synchronizing the configuration file
[[email protected] ~]# scp -r /data/mongo/shard/data/* hdp07:/data/mongo/shard/data/[[email protected] ~]# scp -r /data/mongo/shard/data/* hdp08:/data/mongo/shard/data/
3.3 Starting the sharded service
[[email protected] ~]# mongod -f /data/mongo/shard/data/shard1/mongod.shard1[[email protected] ~]# mongod -f /data/mongo/shard/data/shard2/mongod.shard2[[email protected] ~]# mongod -f /data/mongo/shard/data/shard3/mongod.shard3[[email protected] ~]# mongod -f /data/mongo/shard/data/shard1/mongod.shard1[[email protected] ~]# mongod -f /data/mongo/shard/data/shard2/mongod.shard2[[email protected] ~]# mongod -f /data/mongo/shard/data/shard3/mongod.shard3[[email protected] ~]# mongod -f /data/mongo/shard/data/shard1/mongod.shard1[[email protected] ~]# mongod -f /data/mongo/shard/data/shard2/mongod.shard2[[email protected] ~]# mongod -f /data/mongo/shard/data/shard3/mongod.shard3
3.4 Initializing the Sharded service

The initialization service is performed on any of the nodes.

[[email protected] ~]# mongod--port 27019> rs.initiate ({_id: "Myshard_1", Members:[{_id:1,host: " hdp06.thinkjoy.tt:27019 ", Priority:2},{_id:2,host:" hdp07.thinkjoy.tt:27019 "},{_id:3,host:" Hdp08.thinkjoy.tt                : 27019 "}]}) {" OK ": 1," Operationtime ": Timestamp (1534818896, 1)," $clusterTime ": { "Clustertime": Timestamp (1534818896, 1), "signature": {"hash": Bindata (0, "aaaaaa  Aaaaaaaaaaaaaaaaaaaaa= ")," KeyId ": Numberlong (0)}}}[[email protected] ~]# mongod--port 27020> rs.initiate ({_id: "Myshard_2", Members:[{_id:1,host: "hdp06.thinkjoy.tt:27020", Priority:2 },{_id:2,host: "hdp07.thinkjoy.tt:27020"},{_id:3,host: "hdp08.thinkjoy.tt:27020"}]}) {"Operationtime": Timestamp ( 1534818908, 2), "OK": 0, "errmsg": "Already Initialized", "code":, "codename": "Already Initialized "," $clusterTime ": {" Clustertime ": Timestamp (1534818908, 2), "signature": {"hash": Bindata (0, "aaaaaaaaaaaaaaaaaaaa Aaaaaaa= ")," KeyId ": Numberlong (0)}}}[[email protected] ~]# Mongod-- Port 27021> rs.initiate ({_id: "Myshard_3", Members:[{_id:1,host: "hdp06.thinkjoy.tt:27021", Priority:2},{_id:2, Host: "hdp07.thinkjoy.tt:27021"},{_id:3,host: "hdp08.thinkjoy.tt:27021"}]}) {"Operationtime": Timestamp (1534818928 , 1), "OK": 0, "errmsg": "Already Initialized", "code": 23°c, "codename": "Alreadyinitialize                        D "," $clusterTime ": {" Clustertime ": Timestamp (1534818928, 1)," signature ": {                "Hash": Bindata (0, "aaaaaaaaaaaaaaaaaaaaaaaaaaa="), "KeyId": Numberlong (0) }        }}
4. Configure MONGOs Service
[[email protected] ~]# vi /data/mongo/router/mongod.routernet:  bindIp: 0.0.0.0  port: 27018processManagement:  fork: "true"sharding:  configDB: configRS/hdp06.thinkjoy.tt:27017,hdp07.thinkjoy.tt:27017,hdp08.thinkjoy.tt:27017systemLog:  destination: file  path: /data/mongo/router/logs/mongo_router.log[[email protected] ~]# scp /data/mongo/router/mongod.router hdp07:/data/mongo/router[[email protected] ~]# scp /data/mongo/router/mongod.router hdp08:/data/mongo/router[[email protected] ~]# mongos -f /data/mongo/router/mongod.router[[email protected] ~]# mongos -f /data/mongo/router/mongod.router[[email protected] ~]# mongos -f /data/mongo/router/mongod.router

To configure the Shard, add the main slice to the cluster as follows:

[[email protected] ~]# MONGO--port 27018mongos> show dbsadmin 0.000GBconfig 0.000gbmongos> use Adminswitche D to DB adminmongos> Db.runcommand ({"Addshard": "myshard_1/hdp06.thinkjoy.tt:27019", "MaxSize": 1024x768}) {"Shardadde                D ":" Myshard_1 "," OK ": 1," Operationtime ": Timestamp (1534819816, 6)," $clusterTime ": { "Clustertime": Timestamp (1534819816, 6), "signature": {"hash": Bindata (0, "A Aaaaaaaaaaaaaaaaaaaaaaaaaa= ")," KeyId ": Numberlong (0)}}}mongos> db.runc  Ommand ({"Addshard": "myshard_2/hdp06.thinkjoy.tt:27020", "MaxSize": 1024x768}) {"shardadded": "Myshard_2", "OK": 1, "Operationtime": Timestamp (1534819823, 5), "$clusterTime": {"Clustertime": Timestamp (                        1534819823, 5), "signature": {"hash": Bindata (0, "aaaaaaaaaaaaaaaaaaaaaaaaaaa="), "KeyId ": Numberlong (0)}}}mongos> db.runcommand ({" Addshard ":" myshard_3/hdp06.thinkjoy.tt:27021 "        , "MaxSize": 1024x768}) {"shardadded": "Myshard_3", "OK": 1, "Operationtime": Timestamp (1534819830, 5),                        "$clusterTime": {"Clustertime": Timestamp (1534819830, 5), "signature": {                "Hash": Bindata (0, "aaaaaaaaaaaaaaaaaaaaaaaaaaa="), "KeyId": Numberlong (0) }        }

Shard Cluster status query:

mongos> sh.status ()---sharding status---sharding version: {"_id": 1, "mincompatibleversion": 5, "CurrentVersion": 6, "Clusterid": ObjectId ("5b7b700b1cfaf6f26d2d0284")} shards: {"_id": "Mysha        Rd_1 "," host ":" myshard_1/hdp06.thinkjoy.tt:27019,hdp07.thinkjoy.tt:27019,hdp08.thinkjoy.tt:27019 "," state ": 1}  {"_id": "Myshard_2", "host": "myshard_2/hdp06.thinkjoy.tt:27020,hdp07.thinkjoy.tt:27020,hdp08.thinkjoy.tt:27020", "State": 1} {"_id": "Myshard_3", "host": "myshard_3/hdp06.thinkjoy.tt:27021,hdp07.thinkjoy.tt:27021,hdp08.t hinkjoy.tt:27021 "," state ": 1} Active Mongoses:" 4.0.1 ": 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 Hours:no recent migrations databases: {"_id": "MyDB", "pri Mary ":" Myshard_2 "," PARtitioned ": True," version ": {" UUID ": UUID (" 202a1d72-aa92-403d-a2a6-c7c3aa273323 ")," Lastmod ": 1}}                        Mydb.chapter Shard key: {"id": 1, "Subjectid": 1} unique:false                        Balancing:true Chunks:myshard_2 1 {"id": {"$minKey": 1}, "Subjectid": {"$minKey": 1}}-->> {"id": {"$maxKey": 1}, "Subjecti                        D ": {" $maxKey ": 1}} on:myshard_2 Timestamp (1, 0) mydb.question_knowledge_basic_id                        Shard key: {"knowledge_basic_id": 1, "question_id": 1} unique:false                                Balancing:true chunks:myshard_2 1 { "knowledge_basic_id": {"$minKey": 1}, "question_id": {         "$minKey": 1}               }-->> {"knowledge_basic_id": {"$maxKey": 1}, "question_id": {"$maxKey": 1}} on:myshard_2 Timestamp (1, 0) {"_id": "  Config "," primary ":" config "," partitioned ": true} config.system.sessions Shard Key: {"_id": 1} unique:false balancing:true Chu Nks:myshard_1 1 {"_id": {"$minKey": 1}}-->> {"_id  ": {" $maxKey ": 1}} on:myshard_1 Timestamp (1, 0)
5. Enable sharding on the library

This creates a mydb library, which is enabled with the Shard feature using the following command.

mongos>sh.enableSharding("MyDB"){        "ok" : 1,        "operationTime" : Timestamp(1534837173, 2214),        "$clusterTime" : {                "clusterTime" : Timestamp(1534837173, 2408),                "signature" : {                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),                        "keyId" : NumberLong(0)                }        }}

The following chapter enable Sharding technology for MyDB:

--创建一个索引mongos> use MyDBswitched to db MyDBmongos> db.chapter.createIndex({"id" : 1,"subjectId" : 1},{"name" : "new_idx"})--对表进行分片mongos> sh.shardCollection(‘MyDB.chapter‘,{id:1,subjectId:1}){        "collectionsharded" : "MyDB.chapter",        "collectionUUID" : UUID("a5f3b95c-ced6-4ae5-82d8-06d36176cbef"),        "ok" : 1,        "operationTime" : Timestamp(1535077873, 13),        "$clusterTime" : {                "clusterTime" : Timestamp(1535077873, 13),                "signature" : {                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),                        "keyId" : NumberLong(0)                }        }}

Deploy A MongoDB 4.0 sharded Cluster

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.