MongoDB Shard Build

Source: Internet
Author: User

First, the environment
$ cat/etc/redhat-release CentOS Linux release 7.0.1406 (Core) $ uname-alinux zhaopin-2-201 3.10.0-123.el7.x86_64 #1 SMP Mon June 12:09:22 UTC x86_64 x86_64 x86_64 gnu/linux$ MONGO--versionmongodb Shell version:3.0.6
Second, configure Shard Server1. Create a Directory
$ sudo mkdir-p/data/mongodb/{data/{sh0,sh1},backup/{sh0,sh1},log/{sh0,sh1},conf/{sh0,sh1}}
2. Preparing the configuration file

First Shard:

$ sudo vim/data/mongodb/conf/sh0/mongodb.conf# baseport = 27010maxConns = Filepermissions = 0700fork = Truenoauth = t Ruedirectoryperdb = Truedbpath =/data/mongodb/data/sh0pidfilepath =/data/mongodb/data/sh0/mongodb.pidoplogsize = 10journal = true# Securitynohttpinterface = truerest = false# Log LogPath =/data/mongodb/log/sh0/mongodb.loglogrotate = R Enamelogappend = trueslowms = 50replSet = Sh0 Shardsvr = True

A second shard:

$ sudo vim/data/mongodb/conf/sh1/mongodb.conf# baseport = 27011maxConns = Filepermissions = 0700fork = Truenoauth = t Ruedirectoryperdb = Truedbpath =/data/mongodb/data/sh1pidfilepath =/data/mongodb/data/sh1/mongodb.pidoplogsize = 10journal = true# Securitynohttpinterface = truerest = false# Loglogpath =/data/mongodb/log/sh1/mongodb.loglogrotate = re Namelogappend = trueslowms = 50replSet = Sh1shardsvr = True
3. Start Shard Server
$ sudo/opt/mongodb/bin/mongod--config/data/mongodb/conf/sh0/mongodb.confabout to fork child process, waiting until Ser Ver is ready for connections.forked Process:41492child process started successfully, parent exiting$ Sudo/opt/mongodb/bi N/mongod--config/data/mongodb/conf/sh1/mongodb.confabout to fork child process, waiting until server was ready for Connec Tions.forked Process:41509child Process started successfully, parent exiting$ PS aux | grep MONGO | Grep-v greproot     41492  0.5  0.0 518016 54604?        Sl   10:09   0:00/opt/mongodb/bin/mongod--config/data/mongodb/conf/sh0/mongodb.confroot     41509  0.5  0.0 516988 51824?        Sl   10:09   0:00/opt/mongodb/bin/mongod--config/data/mongodb/conf/sh1/mongodb.conf$ MONGO--port 27010MongoDB Shell version:3.0.6connecting to:127.0.0.1:27010/test>bye$ MONGO--port 27011MongoDB Shell version:3.0 .6connecting To:127.0.0.1:27011/test>bye
Third, configure Config Server1. Create a Directory
$ sudo mkdir-p/data/mongodb/{data/{cf0,cf1,cf2},backup/{cf0,cf1,cf2},log/{cf0,cf1,cf2},conf/{cf0,cf1,cf2}}
2. Preparing the configuration file

First Configuration server:

$ sudo vim/data/mongodb/conf/cf0/config.conf# baseport = 27000maxConns = 800filePermissions = 0700fork = Truenoauth = Tru Edirectoryperdb = Truedbpath =/data/mongodb/data/cf0pidfilepath =/data/mongodb/data/cf0/config.pidoplogsize = 10journal = true# Securitynohttpinterface = truerest = false# Loglogpath =/data/mongodb/log/cf0/config.loglogrotate = ren Amelogappend = Trueslowms = 50configsvr = True

A second configuration server:

$ sudo vim/data/mongodb/conf/cf1/config.conf# baseport = 27001maxConns = 800filePermissions = 0700fork = Truenoauth = Tru Edirectoryperdb = Truedbpath =/data/mongodb/data/cf1pidfilepath =/data/mongodb/data/cf1/config.pidoplogsize = 10journal = true# Securitynohttpinterface = truerest = false# Loglogpath =/data/mongodb/log/cf1/config.loglogrotate = ren Amelogappend = Trueslowms = 50configsvr = True

A third configuration server:

$ sudo vim/data/mongodb/conf/cf2/config.conf# baseport = 27002maxConns = 800filePermissions = 0700fork = Truenoauth = Tru Edirectoryperdb = Truedbpath =/data/mongodb/data/cf2pidfilepath =/data/mongodb/data/cf2/config.pidoplogsize = 10journal = true# Securitynohttpinterface = truerest = false# Loglogpath =/data/mongodb/log/cf2/config.loglogrotate = ren Amelogappend = Trueslowms = 50configsvr = True
3. Start
$ sudo/opt/mongodb/bin/mongod--config/data/mongodb/conf/cf0/config.confabout to fork child process, waiting until serv Er is ready for connections.forked Process:41759child process started successfully, parent exiting$ Sudo/opt/mongodb/bin /mongod--config/data/mongodb/conf/cf1/config.confabout to fork child process, waiting until server was ready for CONNECTI Ons.forked Process:41774child Process started successfully, parent exiting$ Sudo/opt/mongodb/bin/mongod--config/data/ Mongodb/conf/cf2/config.confabout to fork child process, waiting until server was ready for connections.forked process:421 70child process started successfully, parent exiting$ PS aux | grep MONGO |        GREP-V greproot 41492 0.3 0.0 518016 54728? Sl 10:09 0:06/opt/mongodb/bin/mongod--config/data/mongodb/conf/sh0/mongodb.confroot 41509 0.3 0.0 518016 5476        2} Sl 10:09 0:06/opt/mongodb/bin/mongod--config/data/mongodb/conf/sh1/mongodb.confroot 41855 0.4 0.0 467828 5168 4?  Sl 10:25 0:03/opt/mongodb/bin/mongod--config/data/mongodb/conf/cf0/config.confroot 41870 0.4 0.0 467828 52312        ?  Sl 10:25 0:03/opt/mongodb/bin/mongod--config/data/mongodb/conf/cf1/config.confroot 42170 0.9 0.0 467824 52392        ? Sl 10:37 0:00/opt/mongodb/bin/mongod--config/data/mongodb/conf/cf2/config.conf
Iv. Configuring query Routers1. Create a Directory
$ sudo mkdir-p/data/mongodb/{data/{ms0,ms1},backup/{ms0,ms1},log/{ms0,ms1},conf/{ms0,ms1}}
2. Preparing the configuration file

First Configuration server:

$ sudo vim/data/mongodb/conf/ms0/mongos.conf# baseport = 30000maxConns = 800filePermissions = 0700fork = Truepidfilepath =/data/mongodb/data/ms0/mongos.pid# Loglogpath =/data/mongodb/log/ms0/mongos.loglogrotate = Renamelogappend = Trueconfigdb = 172.30.2.201:27000,172.30.2.201:27001,172.30.2.201:27002

A second configuration server:

$ sudo vim/data/mongodb/conf/ms1/mongos.conf# baseport = 30000maxConns = Filepermissions = 0700fork = Truepidfilepath =/data/mongodb/data/ms1/mongos.pid# Log LogPath =/data/mongodb/log/ms1/mongos.loglogrotate = Renamelogappend = Trueconfigdb = 172.30.2.201:27000,172.30.2.201:27001,172.30.2.201:27002
3. Start
$ sudo/opt/mongodb/bin/mongos--config/data/mongodb/conf/ms0/mongos.confabout to fork child process, waiting until serv Er is ready for connections.forked Process:42233child process started successfully, parent exiting$ Sudo/opt/mongodb/bin /mongos--config/data/mongodb/conf/ms1/mongos.confabout to fork child process, waiting until server was ready for CONNECTI Ons.forked Process:42286child Process started successfully, parent exiting$ PS aux | grep MONGO |        GREP-V greproot 41492 0.3 0.0 518016 54728? Sl 10:09 0:06/opt/mongodb/bin/mongod--config/data/mongodb/conf/sh0/mongodb.confroot 41509 0.3 0.0 518016 5476        0? Sl 10:09 0:07/opt/mongodb/bin/mongod--config/data/mongodb/conf/sh1/mongodb.confroot 41855 0.4 0.0 546724 3781        2?  Sl 10:25 0:03/opt/mongodb/bin/mongod--config/data/mongodb/conf/cf0/config.confroot 41870 0.4 0.0 546724 38188        ? Sl 10:25 0:03/opt/mongodb/bin/mongod--config/data/mongodb/conf/cf1/confiG.confroot 42170 0.6 0.0 546720 41960?  Sl 10:37 0:00/opt/mongodb/bin/mongod--config/data/mongodb/conf/cf2/config.confroot 42233 0.5 0.0 233536 10188        ?  Sl 10:38 0:00/opt/mongodb/bin/mongos--config/data/mongodb/conf/ms0/mongos.confroot 42286 0.4 0.0 241728 9056        ? Sl 10:38 0:00/opt/mongodb/bin/mongos--config/data/mongodb/conf/ms1/mongos.conf
V. Initializing a single-node replica set

The benefit of configuring a replica set is for high availability, configuring a single node is my own. To save time, the configuration of the Shard does not need to be modified, as is the case with subsequent additions to the node and replica set

Shard One:

$ MONGO--port 27010MongoDB Shell version:3.0.6connecting to:127.0.0.1:27010/test> use adminswitched to DB admin>                cfg={_id: "Sh0", members:[{_id:0,host: "172.30.2.201:27010"}]} {"_id": "Sh0", "members": [ {"_id": 0, "host": "172.30.2.201:27010"}]} > rs.initiate (CFG); {"OK": 1}sh0:other> rs.status ();                {"Set": "Sh0", "date": Isodate ("2015-10-09t02:45:39.178z"), "MyState": 1, "members": [                        {"_id": 0, "name": "172.30.2.201:27010",                        "Health": 1, "state": 1, "Statestr": "PRIMARY", "Uptime": 2197, "Optime": Timestamp (1444358731, 1), "optimedate": Isoda Te ("2015-10-09t02:45:31z"), "Electiontime": Timestamp (1444358731, 2), "Electiondate": Isodate ("2015-10-09t02:45:31z"), "ConfigVersion": 1, "Self": true}], "OK": 1}sh0:primary>bye

Shard Two:

$ MONGO--port 27011MongoDB Shell version:3.0.6connecting to:127.0.0.1:27011/test> cfg={_id: "Sh1", members:[{_id:0, Host: "172.30.2.201:27011"}]} {"_id": "SH1", "members": [{"_id": 0, "host": "172.30.2.201:27011"}]}> rs.initiate (CFG); {"OK": 1}sh1:other> rs.status ();                {"Set": "Sh1", "date": Isodate ("2015-10-09t02:46:32.961z"), "MyState": 1, "members": [                        {"_id": 0, "name": "172.30.2.201:27011",                        "Health": 1, "state": 1, "Statestr": "PRIMARY", "Uptime": 2244, "Optime": Timestamp (1444358790, 1), "optimedate": Isoda Te ("2015-10-09t02:46:30z"), "Electiontime": Timestamp (1444358790, 2), "Elec TioNdate ": Isodate (" 2015-10-09t02:46:30z ")," ConfigVersion ": 1," Self ": true }], "OK": 1}sh1:primary>bye
Vi. Configuring Shards
$ MONGO--port 30000MongoDB Shell version:3.0.6connecting to:127.0.0.1:30000/testmongos> use admin;switched to DB ADM Inmongos> Sh.addshard ("sh0/172.30.2.201:27010"); {"shardadded": "Sh0", "OK": 1}mongos> sh.addshard ("sh1/172.30.2.201:27011"); {"shardadded": "Sh1", "OK": 1}mongos> use mydb;switched to DB mydbmongos> db.createcollection ("test"); {"OK": 1, "$gleStats": {"Lastoptime": Timestamp (1444358911, 1), "election Id ": ObjectId (" 56172a4bc03d9b1667f8e928 ")}}mongos> sh.enablesharding (" MyDB "); {"OK": 1}mongos> sh.shardcollection ("Mydb.test", {"_id": 1});        {"collectionsharded": "Mydb.test", "OK": 1}mongos> sh.status ();---sharding status---sharding version: { "_id": 1, "mincompatibleversion": 5, "CurrentVersion": 6, "Clusterid": ObjectId ("561728b4030ea038 Bcb57fa0 ")} shards: {" _id ":" Sh0 "," host ":" sh0/172.30.2.201:27010 "} {" _id ":" SH1 "," HOST ":" sh1/172.30.2.201:27011 "} balancer:currently enabled:yes currently running:no Failed BA  Lancer rounds in last 5 attempts:0 migration Results for the last Hours:no recent migrations Databases: {"_id": "admin", "partitioned": false, "PRIMARY": "config"} {"_id": "MyDB", "partitio                        Ned ": True," PRIMARY ":" Sh0 "} Mydb.test Shard key: {" _id ": 1}  Chunks:sh0 1 {"_id": {"$minKey": 1}}-->> { "_id": {"$maxKey": 1}} On:sh0 Timestamp (1, 0)

The visible shards have been configured to complete


MongoDB Shard Build

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.