1. 程式人生 > >MongoDB shard叢集部署

MongoDB shard叢集部署

# dpkg -l | grep mongo
ii  mongodb-org                              3.2.14                            amd64        MongoDB open source document-oriented database system (metapackage)
ii  mongodb-org-mongos                       3.2.14                            amd64        MongoDB sharded cluster query router
ii  mongodb-org-server                       3.2.14                            amd64        MongoDB database server
ii  mongodb-org-shell                        3.2.14                            amd64        MongoDB shell client
ii  mongodb-org-tools                        3.2.14                            amd64        MongoDB tools



172.16.1.62 mongos(27018) configsvr(27019)
172.16.1.63 shard1 (27020, 27021, 27022)
172.16.1.64 shard2 (27020, 27021, 27022)


在172.16.1.63和172.16.1.65上執行
mkdir -p /home/data/
mkdir -p /home/log
mkdir -p /home/data/data_27020
mkdir -p /home/data/data_27021
mkdir -p /home/data/data_27022


mkfs.xfs -f /dev/vdb 
mount -t xfs -o rw,noatime /dev/vdb /home/data


在172.16.1.62上執行
mkdir -p /home/data/
mkdir -p /home/log


在主機172.16.1.63上執行下面的操作
vim /home/data/mongod_27020.cfg
內容如下
systemLog:
  destination: file
  path: /home/mongo/log/shard1_27020.log
  logAppend: true
processManagement:
  fork: true
  pidFilePath: "/home/mongo/shard1_27020.pid"
net:
  port: 27020
storage:
  dbPath: "/home/mongo/data/data_27020"
  engine: wiredTiger
  journal:
    enabled: true
  directoryPerDB: true
operationProfiling:
  slowOpThresholdMs: 10
  mode: "slowOp"
#security:
#  keyFile: "/etc/mongodb/keyfile"
#  clusterAuthMode: "keyFile"
replication:
  oplogSizeMB: 50
  replSetName: "shard1"
  secondaryIndexPrefetch: "all"


vim /home/data/mongod_27021.cfg
內容如下
systemLog:
  destination: file
  path: /home/mongo/log/shard1_27021.log
  logAppend: true
processManagement:
  fork: true
  pidFilePath: "/home/mongo/shard1_27021.pid"
net:
  port: 27021
storage:
  dbPath: "/home/mongo/data/data_27021"
  engine: wiredTiger
  journal:
    enabled: true
  directoryPerDB: true
operationProfiling:
  slowOpThresholdMs: 10
  mode: "slowOp"
#security:
#  keyFile: "/etc/mongodb/keyfile"
#  clusterAuthMode: "keyFile"
replication:
  oplogSizeMB: 50
  replSetName: "shard1"
  secondaryIndexPrefetch: "all"
  
vim /home/data/mongod_27021.cfg
內容如下
systemLog:
  destination: file
  path: /home/mongo/log/shard1_27022.log
  logAppend: true
processManagement:
  fork: true
  pidFilePath: "/home/mongo/shard1_27022.pid"
net:
  port: 27022
storage:
  dbPath: "/home/mongo/data/data_27022"
  engine: wiredTiger
  journal:
    enabled: true
  directoryPerDB: true
operationProfiling:
  slowOpThresholdMs: 10
  mode: "slowOp"
#security:
#  keyFile: "/etc/mongodb/keyfile"
#  clusterAuthMode: "keyFile"
replication:
  oplogSizeMB: 50
  replSetName: "shard1"
  secondaryIndexPrefetch: "all"



在主機172.16.1.65上執行下面的操作
vim /home/data/mongod_27020.cfg
systemLog:
  destination: file
  path: /home/mongo/log/shard2_27020.log
  logAppend: true
processManagement:
  fork: true
  pidFilePath: "/home/mongo/shard2_27020.pid"
net:
  port: 27020
storage:
  dbPath: "/home/mongo/data/data_27020"
  engine: wiredTiger
  journal:
    enabled: true
  directoryPerDB: true
operationProfiling:
  slowOpThresholdMs: 10
  mode: "slowOp"
#security:
#  keyFile: "/etc/mongodb/keyfile"
#  clusterAuthMode: "keyFile"
replication:
  oplogSizeMB: 50
  replSetName: "shard2"
  secondaryIndexPrefetch: "all"


在主機172.16.1.65上執行下面的操作
vim /home/data/mongod_27021.cfg  
systemLog:
  destination: file
  path: /home/mongo/log/shard2_27021.log
  logAppend: true
processManagement:
  fork: true
  pidFilePath: "/home/mongo/shard2_27021.pid"
net:
  port: 27021
storage:
  dbPath: "/home/mongo/data/data_27021"
  engine: wiredTiger
  journal:
    enabled: true
  directoryPerDB: true
operationProfiling:
  slowOpThresholdMs: 10
  mode: "slowOp"
#security:
#  keyFile: "/etc/mongodb/keyfile"
#  clusterAuthMode: "keyFile"
replication:
  oplogSizeMB: 50
  replSetName: "shard2"
  secondaryIndexPrefetch: "all"
  
在主機172.16.1.65上執行下面的操作
vim /home/data/mongod_27022.cfg
systemLog:
  destination: file
  path: /home/mongo/log/shard2_27022.log
  logAppend: true
processManagement:
  fork: true
  pidFilePath: "/home/mongo/shard2_27022.pid"
net:
  port: 27022
storage:
  dbPath: "/home/mongo/data/data_27022"
  engine: wiredTiger
  journal:
    enabled: true
  directoryPerDB: true
operationProfiling:
  slowOpThresholdMs: 10
  mode: "slowOp"
#security:
#  keyFile: "/etc/mongodb/keyfile"
#  clusterAuthMode: "keyFile"
replication:
  oplogSizeMB: 50
  replSetName: "shard2"
  secondaryIndexPrefetch: "all"

在主機172.16.1.62上執行下面的操作
vim /home/data/mongos.cfg
systemLog:
  destination: file
  path: /home/mongo/log/mongos.log
  logAppend: true
processManagement:
  fork: true
  pidFilePath: /home/mongo/data/mongos.pid
net:
  port: 27018
sharding:
  configDB: 172.16.1.62:27019
#security:
#  keyFile: "/etc/mongodb/keyfile"
#  clusterAuthMode: "keyFile" 
 
在主機172.16.1.62上執行下面的操作
vim /home/data/configsvr.cfg
systemLog:
  destination: file
  path: /home/mongo/log/configsvr.log
  logAppend: true
processManagement:
  fork: true
  pidFilePath: "/home/mongo/data/configsvr.pid"
net:
  port: 27019
storage:
  dbPath: "/home/mongo/data/config"
  engine: wiredTiger
  journal:
    enabled: true
#security:
#  keyFile: "/etc/mongodb/keyfile"
#  clusterAuthMode: "keyFile"
sharding:
  clusterRole: configsvr

啟動服務

主機172.16.1.63
mongod --config /home/data/mongod_27020.cfg 
mongod --config /home/data/mongod_27021.cfg 
mongod --config /home/data/mongod_27022.cfg 


主機172.16.1.65
mongod --config /home/data/mongod_27020.cfg 
mongod --config /home/data/mongod_27021.cfg 
mongod --config /home/data/mongod_27022.cfg


主機172.16.1.62
mongod --config /home/data/configsvr.cfg 
mongos --config /home/data/mongos.cfg 

主機172.16.1.63
mongo --port 27020
use admin
config = { _id:"shard1", members:[{_id:0,host:"172.16.1.63:27020"},{_id:1,host:"172.16.1.63:27021"},{_id:2,host:"172.16.1.63:27022",arbiterOnly:true}]} 
rs.initiate(config)


主機172.16.1.65
mongo --port 27020
use admin
config = { _id:"shard2", members:[{_id:0,host:"172.16.1.65:27020"},{_id:1,host:"172.16.1.65:27021"},{_id:2,host:"172.16.1.65:27022",arbiterOnly:true}]} 
rs.initiate(config)

主機172.16.1.62
mongo --port 27018
use admin
db.runCommand({addshard :"shard1/172.16.1.63:27020,172.16.1.63:27021,172.16.1.63:27022"});
db.runCommand({addshard :"shard2/172.16.1.65:27020,172.16.1.65:27021,172.16.1.65:27022"});
sh.status()
db.runCommand({listshards : 1 } )


建立資料驗證寫入成功
sh.enableSharding("testdb")
sh.shardCollection("testdb.heheda",{_id : 1 })
use testdb
for (i=1;i<=100;i++) db.heheda.insert({name: "user"+i, age: (i%10)})