MongoDB Shard Cluster Construction

Source: Internet
Author: User
Tags install mongodb mongodb

Environment:
Centos 7.5 1804MongoDB 4.0.1shard分片主机:    shard1: IP:192.168.1.1    shard2: IP:192.168.1.2    shard2: IP:192.168.1.3    #三台主机分别启动三个mongod实例:        mongod1: 端口: 27017        mongod2: 端口: 27018        mongod2: 端口: 27019configsrv主机:    IP:192.168.1.4        mongod1: 端口: 27019        mongod2: 端口: 37018        mongod2: 端口: 47019Route主机:    192.168.1.5        mongods: 端口: 27017
First, the preparatory work
    • Install mongodb-4 on all nodes and create related folders
cat << EOF > /etc/yum.repos.d/mongodb.repo[mongodb-org-4.0]name=MongoDB 4.0 Repositorybaseurl=https://mirrors.aliyun.com/mongodb/yum/redhat/\$releasever/mongodb-org/4.0/\$basearch/gpgcheck=0enabled=1EOFyum install -y mongodb-orgmkdir -p /var/run/mongodbmkdir -p /data/mongod{1..3}mkdir -p /etc/mongomkdir -p /tmp/mongod{1..3}chown -R mongod.mongod /datachown -R mongod.mongod /var/run/mongodbchown -R mongod.mongod /tmp/mongod{1..3}
    • Generate key and copy to all hosts
#在192.168.1.1主机执行openssl rand -base64 756 > /etc/mongo/mongo.keychown -R mongod.mongod /etc/mongochmod -R 600 /etc/mongoscp -r /etc/mongo 192.168.1.2:/etc/scp -r /etc/mongo 192.168.1.3:/etc/scp -r /etc/mongo 192.168.1.4:/etc/scp -r /etc/mongo 192.168.1.5:/etc/
Second, the configuration Configsvr
    • Operation on CONFIGSVR host (ip:192.168.1.4)
    • To generate three configsvr configuration files:
#configsvr1的配置文件cat << EOF >/etc/mongo/configsvc1.confsystemlog:destination:file logappend:true path:/var /log/mongodb/mongod1.logstorage:dbpath:/data/mongod1 journal:enabled:true WiredTiger:engineConfig:dire CtoryForIndexes:trueprocessManagement:fork:true # Fork and run in background pidfilepath:/VAR/RUN/MONGODB/MONGOD1.P ID # Location of pidfile timeZoneInfo:/usr/share/zoneinfonet:port:27019 #bindIp: 0.0.0.0 # Enter 0.0.0.0,:: To Bin  D to all IPv4 and IPv6 addresses or, alternatively, use the Net.bindipall setting. Bindipall:true maxincomingconnections:500 unixDomainSocket:enabled:true pathprefix:/tmp/mongod1 filepermis Sions:0700security:keyfile:/etc/mongo/mongo.key AUTHORIZATION:ENABLEDREPLICATION:REPLSETNAME:BIGBOSSSHARDING:CL  USTERROLE:CONFIGSVREOF#CONFIGSVR2 configuration file Cat << EOF >/etc/mongo/configsvc2.confsystemlog:destination:file Logappend:true Path:/var/log/mongodb/mongod2.logstorage:dbpatH:/data/mongod2 journal:enabled:true wiredTiger:engineConfig:directoryForIndexes:trueprocessManagement: Fork:true # Fork and run in background pidfilepath:/var/run/mongodb/mongod2.pid # location of Pidfile TimeZoneInfo :/usr/share/zoneinfonet:port:37019 #bindIp: 0.0.0.0 # Enter 0.0.0.0,:: To bind to all IPv4 and IPv6 addresses or, AL  ternatively, use the Net.bindipall setting. Bindipall:true maxincomingconnections:500 unixDomainSocket:enabled:true pathprefix:/tmp/mongod2 filepermis Sions:0700security:keyfile:/etc/mongo/mongo.key AUTHORIZATION:ENABLEDREPLICATION:REPLSETNAME:BIGBOSSSHARDING:CL  USTERROLE:CONFIGSVREOF#CONFIGSVR3 configuration file Cat << EOF >/etc/mongo/configsvc3.confsystemlog:destination:file     Logappend:true path:/var/log/mongodb/mongod3.logstorage:dbpath:/data/mongod3 journal:enabled:true Wiredtiger: EngineConfig:directoryForIndexes:trueprocessManagement:fork:true # Fork and run in Background Pidfilepath:/var/run/mongodb/mongod3.pid # Location of Pidfile TimeZoneInfo:/usr/share/zoneinfonet:port:47019 #bindIp: 0.0.0.0 # Enter 0.0.0.0,:: To bind to all IPv4 and IPv6 addresses or, alternatively, use the Net.bindipall sett  Ing. Bindipall:true maxincomingconnections:500 unixDomainSocket:enabled:true pathprefix:/tmp/mongod3 filepermis Sions:0700security:keyfile:/etc/mongo/mongo.key AUTHORIZATION:ENABLEDREPLICATION:REPLSETNAME:BIGBOSSSHARDING:CL Usterrole:configsvreof
    • Start Mongod:
mongod -f /etc/mongo/configsvc1.confmongod -f /etc/mongo/configsvc2.confmongod -f /etc/mongo/configsvc3.conf

    • Initialize the configsrv replica cluster:
mongo --port 27019rs.initiate({  _id: "BigBoss",  version: 1,  protocolVersion: 1,  writeConcernMajorityJournalDefault: true,  configsvr: true,  members: [    {      _id: 0,      host: "192.168.1.4:27019",      arbiterOnly: false,      buildIndexes: true,      hidden: false,      priority: 66,      tags: {        BigBoss: "YES"      },      slaveDelay: 0,      votes: 1    },    {      _id: 1,      host: "192.168.1.4:37019",      arbiterOnly: false,      buildIndexes: true,      hidden: false,      priority: 55,      tags: {        BigBoss: "NO"      },      slaveDelay: 0,      votes: 1    },    {      _id: 2,      host: "192.168.1.4:47019",      arbiterOnly: false,      buildIndexes: true,      hidden: false,      priority: 33,      tags: {        BigBoss: "NO"      },      slaveDelay: 0,      votes: 1    }  ],  settings: {    chainingAllowed : true,  }})#查看副本集状态rs.status()

Third, configure Shard1 replica set:
    • in Shard1 host (ip:192.168.1.1) operation
    • generate three Mongod profiles:
#mongod1. conf configuration file: Cat << EOF >/etc/mongo/mongod1.confsystemlog:destination:file logappend:true path:/var/ Log/mongodb/mongod1.logstorage:dbpath:/data/mongod1 journal:enabled:true WiredTiger:engineConfig:direc Toryforindexes:true ProcessManagement:fork:true # Fork and run in background pidfilepath:/var/run/mongodb/mongo  D1.pid # Location of Pidfile timeZoneInfo:/usr/share/zoneinfonet:port:27017 #bindIp: 0.0.0.0 # Enter 0.0.0.0,:: To  Bind to all IPv4 and IPv6 addresses or, alternatively, use the Net.bindipall setting. Bindipall:true maxincomingconnections:500 unixDomainSocket:enabled:true pathprefix:/tmp/mongod1 filepermis Sions:0700security:keyfile:/etc/mongo/mongo.key authorization:enabledreplication:replSetName:shard1sharding:clu  sterrole:shardsvreof#mongod2.conf configuration file: Cat << EOF >/etc/mongo/mongod2.confsystemlog:destination:file Logappend:true Path:/VAR/LOG/MONGODB/MONGOD2.LOGSTORAGE:DBPATh:/data/mongod2 journal:enabled:true wiredTiger:engineConfig:directoryForIndexes:true Processmanag Ement:fork:true # Fork and run in background pidfilepath:/var/run/mongodb/mongod2.pid # location of Pidfile Timezo Neinfo:/usr/share/zoneinfonet:port:27018 #bindIp: 0.0.0.0 # Enter 0.0.0.0,:: To bind to all IPv4 and IPV6 addresses  Or, alternatively, use the Net.bindipall setting. Bindipall:true maxincomingconnections:500 unixDomainSocket:enabled:true pathprefix:/tmp/mongod2 filepermis Sions:0700security:keyfile:/etc/mongo/mongo.key authorization:enabledreplication:replSetName:shard1sharding:clu  sterrole:shardsvreof#mongod3.conf configuration file: Cat << EOF >/etc/mongo/mongod3.confsystemlog:destination:file     Logappend:true path:/var/log/mongodb/mongod3.logstorage:dbpath:/data/mongod3 journal:enabled:true Wiredtiger: EngineConfig:directoryForIndexes:true ProcessManagement:fork:true # Fork and run in Background pidfilepath:/var/run/mongodb/mongod3.pid # Location of Pidfile TimeZoneInfo:/usr/share/zoneinfonet:port : 27019 #bindIp: 0.0.0.0 # Enter 0.0.0.0,:: To bind to all IPv4 and IPv6 addresses or, alternatively, use the Net.bindip  All setting. Bindipall:true maxincomingconnections:500 unixDomainSocket:enabled:true pathprefix:/tmp/mongod3 filepermis Sions:0700security:keyfile:/etc/mongo/mongo.key authorization:enabledreplication:replSetName:shard1sharding:clu Sterrole:shardsvreof
    • Start Mongod:
mongod -f /etc/mongo/mongod1.confmongod -f /etc/mongo/mongod2.confmongod -f /etc/mongo/mongod3.conf
    • Initializing the Shard1 replica set
mongors.initiate({  _id: "shard1",  version: 1,  protocolVersion: 1,  writeConcernMajorityJournalDefault: true,  members: [    {      _id: 0,      host: "192.168.1.1:27017",      arbiterOnly: false,      buildIndexes: true,      hidden: false,      priority: 66,      tags: {        BigBoss: "YES"      },      slaveDelay: 0,      votes: 1    },    {      _id: 1,      host: "192.168.1.1:27018",      arbiterOnly: false,      buildIndexes: true,      hidden: false,      priority: 55,      tags: {        BigBoss: "NO"      },      slaveDelay: 0,      votes: 1    },    {      _id: 2,      host: "192.168.1.1:27019",      arbiterOnly: false,      buildIndexes: true,      hidden: false,      priority: 33,      tags: {        BigBoss: "NO"      },      slaveDelay: 0,      votes: 1    }  ],  settings: {    chainingAllowed : true,  }})#查看副本集状态rs.status()

Iv. Configuring the Shard2 replica set:
    • in Shard2 host (ip:192.168.1.2) operation
    • generate three Mongod profiles:
#mongod1. conf configuration file: Cat << EOF >/etc/mongo/mongod1.confsystemlog:destination:file logappend:true path:/var/ Log/mongodb/mongod1.logstorage:dbpath:/data/mongod1 journal:enabled:true WiredTiger:engineConfig:direc Toryforindexes:true ProcessManagement:fork:true # Fork and run in background pidfilepath:/var/run/mongodb/mongo  D1.pid # Location of Pidfile timeZoneInfo:/usr/share/zoneinfonet:port:27017 #bindIp: 0.0.0.0 # Enter 0.0.0.0,:: To  Bind to all IPv4 and IPv6 addresses or, alternatively, use the Net.bindipall setting. Bindipall:true maxincomingconnections:500 unixDomainSocket:enabled:true pathprefix:/tmp/mongod1 filepermis Sions:0700security:keyfile:/etc/mongo/mongo.key authorization:enabledreplication:replSetName:shard2sharding:clu  sterrole:shardsvreof#mongod2.conf configuration file: Cat << EOF >/etc/mongo/mongod2.confsystemlog:destination:file Logappend:true Path:/VAR/LOG/MONGODB/MONGOD2.LOGSTORAGE:DBPATh:/data/mongod2 journal:enabled:true wiredTiger:engineConfig:directoryForIndexes:true Processmanag Ement:fork:true # Fork and run in background pidfilepath:/var/run/mongodb/mongod2.pid # location of Pidfile Timezo Neinfo:/usr/share/zoneinfonet:port:27018 #bindIp: 0.0.0.0 # Enter 0.0.0.0,:: To bind to all IPv4 and IPV6 addresses  Or, alternatively, use the Net.bindipall setting. Bindipall:true maxincomingconnections:500 unixDomainSocket:enabled:true pathprefix:/tmp/mongod2 filepermis Sions:0700security:keyfile:/etc/mongo/mongo.key authorization:enabledreplication:replSetName:shard2sharding:clu  sterrole:shardsvreof#mongod3.conf configuration file: Cat << EOF >/etc/mongo/mongod3.confsystemlog:destination:file     Logappend:true path:/var/log/mongodb/mongod3.logstorage:dbpath:/data/mongod3 journal:enabled:true Wiredtiger: EngineConfig:directoryForIndexes:true ProcessManagement:fork:true # Fork and run in Background pidfilepath:/var/run/mongodb/mongod3.pid # Location of Pidfile TimeZoneInfo:/usr/share/zoneinfonet:port : 27019 #bindIp: 0.0.0.0 # Enter 0.0.0.0,:: To bind to all IPv4 and IPv6 addresses or, alternatively, use the Net.bindip  All setting. Bindipall:true maxincomingconnections:500 unixDomainSocket:enabled:true pathprefix:/tmp/mongod3 filepermis Sions:0700security:keyfile:/etc/mongo/mongo.key authorization:enabledreplication:replSetName:shard2sharding:clu Sterrole:shardsvreof
    • Start Mongod:
mongod -f /etc/mongo/mongod1.confmongod -f /etc/mongo/mongod2.confmongod -f /etc/mongo/mongod3.conf
    • Initializing the Shard2 replica set
mongors.initiate({  _id: "shard2",  version: 1,  protocolVersion: 1,  writeConcernMajorityJournalDefault: true,  members: [    {      _id: 0,      host: "192.168.1.2:27017",      arbiterOnly: false,      buildIndexes: true,      hidden: false,      priority: 66,      tags: {        BigBoss: "YES"      },      slaveDelay: 0,      votes: 1    },    {      _id: 1,      host: "192.168.1.2:27018",      arbiterOnly: false,      buildIndexes: true,      hidden: false,      priority: 55,      tags: {        BigBoss: "NO"      },      slaveDelay: 0,      votes: 1    },    {      _id: 2,      host: "192.168.1.2:27019",      arbiterOnly: false,      buildIndexes: true,      hidden: false,      priority: 33,      tags: {        BigBoss: "NO"      },      slaveDelay: 0,      votes: 1    }  ],  settings: {    chainingAllowed : true,  }})#查看shard2副本集状态rs.status()

V. Configuring SHARD1 Replica Sets
    • in Shard1 host (ip:192.168.1.1) operation
    • generate three Mongod profiles:
#mongod1. conf configuration file: Cat << EOF >/etc/mongo/mongod1.confsystemlog:destination:file logappend:true path:/var/ Log/mongodb/mongod1.logstorage:dbpath:/data/mongod1 journal:enabled:true WiredTiger:engineConfig:direc Toryforindexes:true ProcessManagement:fork:true # Fork and run in background pidfilepath:/var/run/mongodb/mongo  D1.pid # Location of Pidfile timeZoneInfo:/usr/share/zoneinfonet:port:27017 #bindIp: 0.0.0.0 # Enter 0.0.0.0,:: To  Bind to all IPv4 and IPv6 addresses or, alternatively, use the Net.bindipall setting. Bindipall:true maxincomingconnections:500 unixDomainSocket:enabled:true pathprefix:/tmp/mongod1 filepermis Sions:0700security:keyfile:/etc/mongo/mongo.key authorization:enabledreplication:replSetName:shard3sharding:clu  sterrole:shardsvreof#mongod2.conf configuration file: Cat << EOF >/etc/mongo/mongod2.confsystemlog:destination:file Logappend:true Path:/VAR/LOG/MONGODB/MONGOD2.LOGSTORAGE:DBPATh:/data/mongod2 journal:enabled:true wiredTiger:engineConfig:directoryForIndexes:true Processmanag Ement:fork:true # Fork and run in background pidfilepath:/var/run/mongodb/mongod2.pid # location of Pidfile Timezo Neinfo:/usr/share/zoneinfonet:port:27018 #bindIp: 0.0.0.0 # Enter 0.0.0.0,:: To bind to all IPv4 and IPV6 addresses  Or, alternatively, use the Net.bindipall setting. Bindipall:true maxincomingconnections:500 unixDomainSocket:enabled:true pathprefix:/tmp/mongod2 filepermis Sions:0700security:keyfile:/etc/mongo/mongo.key authorization:enabledreplication:replSetName:shard3sharding:clu  sterrole:shardsvreof#mongod3.conf configuration file: Cat << EOF >/etc/mongo/mongod3.confsystemlog:destination:file     Logappend:true path:/var/log/mongodb/mongod3.logstorage:dbpath:/data/mongod3 journal:enabled:true Wiredtiger: EngineConfig:directoryForIndexes:true ProcessManagement:fork:true # Fork and run in Background pidfilepath:/var/run/mongodb/mongod3.pid # Location of Pidfile TimeZoneInfo:/usr/share/zoneinfonet:port : 27019 #bindIp: 0.0.0.0 # Enter 0.0.0.0,:: To bind to all IPv4 and IPv6 addresses or, alternatively, use the Net.bindip  All setting. Bindipall:true maxincomingconnections:500 unixDomainSocket:enabled:true pathprefix:/tmp/mongod3 filepermis Sions:0700security:keyfile:/etc/mongo/mongo.key authorization:enabledreplication:replSetName:shard3sharding:clu Sterrole:shardsvreof
    • Start Mongod:
mongod -f /etc/mongo/mongod1.confmongod -f /etc/mongo/mongod2.confmongod -f /etc/mongo/mongod3.conf
    • Initializing the Shard3 replica set
mongors.initiate({  _id: "shard3",  version: 1,  protocolVersion: 1,  writeConcernMajorityJournalDefault: true,  members: [    {      _id: 0,      host: "192.168.1.3:27017",      arbiterOnly: false,      buildIndexes: true,      hidden: false,      priority: 66,      tags: {        BigBoss: "YES"      },      slaveDelay: 0,      votes: 1    },    {      _id: 1,      host: "192.168.1.3:27018",      arbiterOnly: false,      buildIndexes: true,      hidden: false,      priority: 55,      tags: {        BigBoss: "NO"      },      slaveDelay: 0,      votes: 1    },    {      _id: 2,      host: "192.168.1.3:27019",      arbiterOnly: false,      buildIndexes: true,      hidden: false,      priority: 33,      tags: {        BigBoss: "NO"      },      slaveDelay: 0,      votes: 1    }  ],  settings: {    chainingAllowed : true,  }})#查看shard3副本集状态rs.status()
Six, configure the route
    • To create a MONGOs configuration file:
#route是无状态的,在任何一台主机启动都行,只要能够连接至configsrv即可cat << EOF > /etc/mongo/route.confsystemLog:  destination: file  logAppend: true  path: /var/log/mongodb/mongod.logprocessManagement:  fork: true  # fork and run in background  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile  timeZoneInfo: /usr/share/zoneinfonet:  bindIpAll: true  maxIncomingConnections: 500  unixDomainSocket:    enabled: true    pathPrefix: /tmp    filePermissions: 0700security:  keyFile: /etc/mongo/mongo.key#  authorization: enabled#replication:sharding:  configDB: BigBoss/192.168.1.4:27019,192.168.1.4:37019,192.168.1.4:47019EOF
    • Start MONGOs and set up a connected account password
#启动mongos -f /etc/mongo/route.conf#连接mongo#设置管理员账号密码use admindb.createUser({    user: "root",    pwd: "123456",    roles: [ { role: "__system", db: "admin" } ]  })exit

    • Re-connect to MongoDB
mongo -uroot -p123456  --authenticationDatabase admin#添加分片主机至集群中sh.addShard("shard1/192.168.1.1:27017,192.168.1.1:27018,192.168.1.1:27019")sh.addShard("shard2/192.168.1.2:27017,192.168.1.2:27018,192.168.1.2:27019")sh.addShard("shard3/192.168.1.3:27017,192.168.1.3:27018,192.168.1.3:27019")#查看状态sh.status()####为了展示出效果,修改一下默认的chunksize大小,这里修改为1M#默认的chunksize大小为64M,示例修改命令如下:#use config#db.settings.save( { _id:"chunksize", value: <sizeInMB> } )use configdb.settings.save( { _id:"chunksize", value: 1 } )#为test数据库开启分片#选择一个片键age并指定一个集合mycoll对其进行分片sh.enableSharding("test")sh.shardCollection("test.mycoll", {"age": 1})#测试分片,写入数据到数据库中use testfor (i = 1; i <= 10000; i++) db.mycoll.insert({age:(i%100), name:"bigboss_user"+i, address:i+", Some Road, Zhengzhou, Henan", country:"China", course:"cousre"+"(i%12)"})#写入完成之后就可以查看分片信息了sh.status()





MongoDB Shard Cluster Construction

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.