MongoDB分區叢集搭建

來源:互聯網
上載者:User

標籤:process   auth   settings   data   設定檔   chm   sla   prefix   base64   

環境:
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
一、準備工作
  • 在所有節點安裝mongodb-4 並建立相關檔案夾
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}
  • 產生key並複製至所有主機
#在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/
二、配置configsvr
  • 在configsvr主機(IP:192.168.1.4)操作
  • 產生三個configsvr的設定檔:
#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:      directoryForIndexes: trueprocessManagement:  fork: true  # fork and run in background  pidFilePath: /var/run/mongodb/mongod1.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.bindIpAll setting.  bindIpAll: true  maxIncomingConnections: 500  unixDomainSocket:    enabled: true    pathPrefix: /tmp/mongod1    filePermissions: 0700security:  keyFile: /etc/mongo/mongo.key  authorization: enabledreplication:  replSetName: BigBosssharding:  clusterRole: configsvrEOF#configsvr2的設定檔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, alternatively, use the net.bindIpAll setting.  bindIpAll: true  maxIncomingConnections: 500  unixDomainSocket:    enabled: true    pathPrefix: /tmp/mongod2    filePermissions: 0700security:  keyFile: /etc/mongo/mongo.key  authorization: enabledreplication:  replSetName: BigBosssharding:  clusterRole: configsvrEOF#configsvr3的設定檔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 setting.  bindIpAll: true  maxIncomingConnections: 500  unixDomainSocket:    enabled: true    pathPrefix: /tmp/mongod3    filePermissions: 0700security:  keyFile: /etc/mongo/mongo.key  authorization: enabledreplication:  replSetName: BigBosssharding:  clusterRole: configsvrEOF
  • 啟動mongod:
mongod -f /etc/mongo/configsvc1.confmongod -f /etc/mongo/configsvc2.confmongod -f /etc/mongo/configsvc3.conf

  • 初始化configsrv複本集群:
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()

三、配置shard1複本集:
  • 在shard1主機(IP:192.168.1.1)操作
  • 產生三個mongod的設定檔:
#mongod1.conf設定檔:cat << EOF > /etc/mongo/mongod1.confsystemLog:  destination: file  logAppend: true  path: /var/log/mongodb/mongod1.logstorage:  dbPath: /data/mongod1  journal:    enabled: true  wiredTiger:    engineConfig:      directoryForIndexes: true     processManagement:  fork: true  # fork and run in background  pidFilePath: /var/run/mongodb/mongod1.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    filePermissions: 0700security:  keyFile: /etc/mongo/mongo.key  authorization: enabledreplication:  replSetName: shard1sharding:  clusterRole: shardsvrEOF#mongod2.conf設定檔: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     processManagement:  fork: true  # fork and run in background  pidFilePath: /var/run/mongodb/mongod2.pid  # location of pidfile  timeZoneInfo: /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    filePermissions: 0700security:  keyFile: /etc/mongo/mongo.key  authorization: enabledreplication:  replSetName: shard1sharding:  clusterRole: shardsvrEOF#mongod3.conf設定檔: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.bindIpAll setting.  bindIpAll: true  maxIncomingConnections: 500  unixDomainSocket:    enabled: true    pathPrefix: /tmp/mongod3    filePermissions: 0700security:  keyFile: /etc/mongo/mongo.key  authorization: enabledreplication:  replSetName: shard1sharding:  clusterRole: shardsvrEOF
  • 啟動mongod:
mongod -f /etc/mongo/mongod1.confmongod -f /etc/mongo/mongod2.confmongod -f /etc/mongo/mongod3.conf
  • 初始化shard1複本集
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()

四、配置shard2複本集:
  • 在shard2主機(IP:192.168.1.2)操作
  • 產生三個mongod的設定檔:
#mongod1.conf設定檔:cat << EOF > /etc/mongo/mongod1.confsystemLog:  destination: file  logAppend: true  path: /var/log/mongodb/mongod1.logstorage:  dbPath: /data/mongod1  journal:    enabled: true  wiredTiger:    engineConfig:      directoryForIndexes: true     processManagement:  fork: true  # fork and run in background  pidFilePath: /var/run/mongodb/mongod1.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    filePermissions: 0700security:  keyFile: /etc/mongo/mongo.key  authorization: enabledreplication:  replSetName: shard2sharding:  clusterRole: shardsvrEOF#mongod2.conf設定檔: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     processManagement:  fork: true  # fork and run in background  pidFilePath: /var/run/mongodb/mongod2.pid  # location of pidfile  timeZoneInfo: /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    filePermissions: 0700security:  keyFile: /etc/mongo/mongo.key  authorization: enabledreplication:  replSetName: shard2sharding:  clusterRole: shardsvrEOF#mongod3.conf設定檔: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.bindIpAll setting.  bindIpAll: true  maxIncomingConnections: 500  unixDomainSocket:    enabled: true    pathPrefix: /tmp/mongod3    filePermissions: 0700security:  keyFile: /etc/mongo/mongo.key  authorization: enabledreplication:  replSetName: shard2sharding:  clusterRole: shardsvrEOF
  • 啟動mongod:
mongod -f /etc/mongo/mongod1.confmongod -f /etc/mongo/mongod2.confmongod -f /etc/mongo/mongod3.conf
  • 初始化shard2複本集
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()

五、配置shard1複本集
  • 在shard1主機(IP:192.168.1.1)操作
  • 產生三個mongod的設定檔:
#mongod1.conf設定檔:cat << EOF > /etc/mongo/mongod1.confsystemLog:  destination: file  logAppend: true  path: /var/log/mongodb/mongod1.logstorage:  dbPath: /data/mongod1  journal:    enabled: true  wiredTiger:    engineConfig:      directoryForIndexes: true     processManagement:  fork: true  # fork and run in background  pidFilePath: /var/run/mongodb/mongod1.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    filePermissions: 0700security:  keyFile: /etc/mongo/mongo.key  authorization: enabledreplication:  replSetName: shard3sharding:  clusterRole: shardsvrEOF#mongod2.conf設定檔: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     processManagement:  fork: true  # fork and run in background  pidFilePath: /var/run/mongodb/mongod2.pid  # location of pidfile  timeZoneInfo: /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    filePermissions: 0700security:  keyFile: /etc/mongo/mongo.key  authorization: enabledreplication:  replSetName: shard3sharding:  clusterRole: shardsvrEOF#mongod3.conf設定檔: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.bindIpAll setting.  bindIpAll: true  maxIncomingConnections: 500  unixDomainSocket:    enabled: true    pathPrefix: /tmp/mongod3    filePermissions: 0700security:  keyFile: /etc/mongo/mongo.key  authorization: enabledreplication:  replSetName: shard3sharding:  clusterRole: shardsvrEOF
  • 啟動mongod:
mongod -f /etc/mongo/mongod1.confmongod -f /etc/mongo/mongod2.confmongod -f /etc/mongo/mongod3.conf
  • 初始化shard3複本集
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()
六、配置Route
  • 建立mongos設定檔:
#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
  • 啟動mongos並設定一個串連的帳號密碼
#啟動mongos -f /etc/mongo/route.conf#串連mongo#設定管理員帳號密碼use admindb.createUser({    user: "root",    pwd: "123456",    roles: [ { role: "__system", db: "admin" } ]  })exit

  • 重連至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分區叢集搭建

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.