mongo分片集群部署
測試環境192.168.56.101-213
前期準備:
openssl rand -base64 756 > /home/software/mongodb/mongodbkey
chmod 600 /home/software/mongodb/mongodbkey
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinx/config
setenforce 0
systemctl stop firewalld
systemctl disable firewalld
tar -zxvf mongodb-linux-x86_64-rhel70-4.0.7.tgz
mv mongodb-linux-x86_64-rhel70-4.0.7 mongodb
cd /home/software/mongodb
mkdir conf mongos config-server shard1 shard2 shard3
mkdir mongos/{log,data}
mkdir shard1/{log,data}
mkdir shard2/{log,data}
mkdir shard3/{log,data}
mkdir config-server/{log,data}
環境變量設置,如下
cat /etc/profile.d/mongodb.sh
# 內容
export MONGODB_HOME=/home/software/mongodb4
export PATH=$MONGODB_HOME/bin:$PATH
# 使立即生效
source /etc/profile.d/mongodb.sh
mongodb的啟動順序是,先啟動配置服務器,在啟動分片,最後啟動mongos
安全關閉mongodb方法一
kill -2 `ps -ef | grep mongod| awk ‘NR==1 {print $2}‘`
安全關閉mongodb方法二
/home/software/mongodb4/bin/mongo -host 127.0.0.1 -port 30000
> use admin; --使用管理員數據庫
> db.shutdownServer(); --安全關閉MongoDB
配置服務器是一個普通的mongod進程,所以只需要新開一個實例即可。配置服務器必須開啟1個或則3個,開啟2個則會報錯:
主機ip及服務
192.168.56.101:configsvr服務,shard1服務,shard2服務,shard3服務,mongos服務
192.168.56.102:configsvr服務,shard1服務,shard2服務,shard3服務,mongos服務
192.168.56.102:configsvr服務,shard1服務,shard2服務,shard3服務,mongos服務
分布:每個機子起3個shard實例,從shard1到shard3的端口號為27001 , 27002 , 27003
每個機子都有config實例,端口號21000
每個機子都有mongs實例,端口號27017
三臺服務器目錄機構一樣,mongo的配置文件在/home/software/mongodb/conf目錄下
配置文件基本三臺機子通用,但是初始化的時候要註意ip及端口是否正確
-------------config-server.conf的配置文件-----------
systemLog:
destination: file
logAppend: true
path: /home/software/mongodb/config-server/log/congigsrv.log
storage:
dbPath: /home/software/mongodb/config-server/data
journal:
enabled: true
wiredTiger:
engineConfig:
directoryForIndexes: true
processManagement:
fork: true # fork and run in background
pidFilePath: /home/software/mongodb/config-server/log/configsrv.pid # location of pidfile
net:
port: 21000
#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: 65535
unixDomainSocket:
enabled: true
##pathPrefix: /tmp/mongod1
filePermissions: 0700
security:
keyFile: /home/software/mongodb/mongodbkey
authorization: enabled
replication:
replSetName: configs
sharding:
clusterRole: configsvr
3臺都啟動:mongod -f /home/software/mongodb/conf/config-server.conf
--------------初始化configsrv副本集群 -------------
mongo --port 21000
rs.initiate(
... {
... _id: "configs",
... version: 1,
... protocolVersion: 1,
... writeConcernMajorityJournalDefault: true,
... configsvr: true,
... members: [
... {
... _id: 0,
... host: "192.168.56.101:21000",
... arbiterOnly: false,
... buildIndexes: true,
... hidden: false,
... priority: 66,
... tags: {
... BigBoss: "YES"
... },
... slaveDelay: 0,
... votes: 1
... },
... {
... _id: 1,
... host: "192.168.56.102:21000",
... arbiterOnly: false,
... buildIndexes: true,
... hidden: false,
... priority: 55,
... tags: {
... BigBoss: "NO"
... },
... slaveDelay: 0,
... votes: 1
... },
... {
... _id: 2,
... host: "192.168.56.103:21000",
... arbiterOnly: false,
... buildIndexes: true,
... hidden: false,
... priority: 33,
... tags: {
... BigBoss: "YES"
... },
... slaveDelay: 0,
... votes: 1
... }
... ],
... settings: {
... chainingAllowed : true,
... }
... }
... )
----------shard1的配置文件------
systemLog:
destination: file
logAppend: true
path: /home/software/mongodb/shard1/log/shard1.log
storage:
dbPath: /home/software/mongodb/shard1/data
journal:
enabled: true
wiredTiger:
engineConfig:
directoryForIndexes: true
processManagement:
fork: true # fork and run in background
pidFilePath: /home/software/mongodb/config-server/log/configsrv.pid # location of pidfile
##timeZoneInfo: /usr/share/zoneinfo
net:
port: 27001
#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: 65535
unixDomainSocket:
enabled: true
##pathPrefix: /tmp/mongod1
filePermissions: 0700
security:
keyFile: /home/software/mongodb/mongodbkey
authorization: enabled
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
3臺啟動:mongod -f /home/software/mongodb/conf/shard1.conf
mongo --port 27001
初始化shard1副本集
rs.initiate(
... {
... _id: "shard1",
... version: 1,
... protocolVersion: 1,
... writeConcernMajorityJournalDefault: true,
... members: [
... {
... _id: 0,
... host: "192.168.56.101:27001",
... arbiterOnly: false,
... buildIndexes: true,
... hidden: false,
... priority: 66,
... tags: {
... BigBoss: "YES"
... },
... slaveDelay: 0,
... votes: 1
... },
... {
... _id: 1,
... host: "192.168.56.102:27001",
... arbiterOnly: false,
... buildIndexes: true,
... hidden: false,
... priority: 55,
... tags: {
... BigBoss: "NO"
... },
... slaveDelay: 0,
... votes: 1
... },
... {
... _id: 2,
... host: "192.168.56.103:27001",
... arbiterOnly: false,
... buildIndexes: true,
... hidden: false,
... priority: 33,
... tags: {
... BigBoss: "NO"
... },
... slaveDelay: 0,
... votes: 1
... }
... ],
... settings: {
... chainingAllowed : true,
... }
... }
... )
------------------------shard2的配置文件---------------
systemLog:
destination: file
logAppend: true
path: /home/software/mongodb/shard2/log/shard2.log
storage:
dbPath: /home/software/mongodb/shard2/data
journal:
enabled: true
wiredTiger:
engineConfig:
directoryForIndexes: true
processManagement:
fork: true # fork and run in background
pidFilePath: /home/software/mongodb/config-server/log/configsrv.pid # location of pidfile
##timeZoneInfo: /usr/share/zoneinfo
net:
port: 27002
#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: 65535
unixDomainSocket:
enabled: true
##pathPrefix: /tmp/mongod1
filePermissions: 0700
security:
keyFile: /home/software/mongodb/mongodbkey
authorization: enabled
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr
3臺啟動:mongod -f /home/software/mongodb/conf/shard2.conf
mongo --port 27002
初始化shard2副本集
rs.initiate(
{
_id: "shard2",
version: 1,
protocolVersion: 1,
writeConcernMajorityJournalDefault: true,
members: [
{
_id: 0,
host: "192.168.56.101:27002",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 66,
tags: {
BigBoss: "YES"
},
slaveDelay: 0,
votes: 1
},
{
_id: 1,
host: "192.168.56.102:27002",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 55,
tags: {
BigBoss: "NO"
},
slaveDelay: 0,
votes: 1
},
{
_id: 2,
host: "192.168.56.103:27002",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 33,
tags: {
BigBoss: "NO"
},
slaveDelay: 0,
votes: 1
}
],
settings: {
chainingAllowed : true,
}
}
)
-------------------shard3的配置文件----------
systemLog:
destination: file
logAppend: true
path: /home/software/mongodb/shard3/log/shard3.log
storage:
dbPath: /home/software/mongodb/shard3/data
journal:
enabled: true
wiredTiger:
engineConfig:
directoryForIndexes: true
processManagement:
fork: true # fork and run in background
pidFilePath: /home/software/mongodb/config-server/log/configsrv.pid # location of pidfile
##timeZoneInfo: /usr/share/zoneinfo
net:
port: 27003
#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: 65535
unixDomainSocket:
enabled: true
##pathPrefix: /tmp/mongod1
filePermissions: 0700
security:
keyFile: /home/software/mongodb/mongodbkey
authorization: enabled
replication:
replSetName: shard3
sharding:
clusterRole: shardsvr
3臺啟動:mongod -f /home/software/mongodb/conf/shard3.conf
mongo --port 27003
初始化shard3副本集
rs.initiate(
{
_id: "shard3",
version: 1,
protocolVersion: 1,
writeConcernMajorityJournalDefault: true,
members: [
{
_id: 0,
host: "192.168.56.101:27003",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 66,
tags: {
BigBoss: "YES"
},
slaveDelay: 0,
votes: 1
},
{
_id: 1,
host: "192.168.56.102:27003",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 55,
tags: {
BigBoss: "NO"
},
slaveDelay: 0,
votes: 1
},
{
_id: 2,
host: "192.168.56.103:27003",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 33,
tags: {
BigBoss: "NO"
},
slaveDelay: 0,
votes: 1
}
],
settings: {
chainingAllowed : true,
}
}
)
-----------配置Route------
創建mongos配置文件如下:
systemLog:
destination: file
logAppend: true
path: /home/software/mongodb/mongos/log/mongos.log
processManagement:
fork: true # fork and run in background
pidFilePath: /home/software/mongodb/mongos/log/mongos.pid # location of pidfile
#timeZoneInfo: /usr/share/zoneinfo
net:
bindIpAll: true
maxIncomingConnections: 500
unixDomainSocket:
enabled: true
#pathPrefix: /tmp
filePermissions: 0700
security:
keyFile: /home/software/mongodb/mongodbkey
# authorization: enabled
#replication:
sharding:
configDB: configs/192.168.56.101:21000,192.168.56.102:21000,192.168.56.103:21000
啟動mongos並設置一個連接的賬號密碼
#啟動
mongos -f /home/software/mongodb/conf/mongos.conf
#連接
mongo
#設置管理員賬號密碼
use admin
db.createUser(
{
user: "root",
pwd: "123456",
roles: [ { role: "__system", db: "admin" } ]
}
)
重連至mongodb
mongo -uroot -p123456 --authenticationDatabase admin
#添加分片主機至集群中
sh.addShard("shard1/192.168.56.101:27001,192.168.56.102:27001,192.168.56.103:27001");
sh.addShard("shard2/192.168.56.101:27002,192.168.56.102:27002,192.168.56.103:27002");
sh.addShard("shard3/192.168.56.101:27003,192.168.56.102:27003,192.168.56.103:27003");
#查看狀態
sh.status()
####為了展示出效果,修改一下默認的chunksize大小,這裏修改為1M
#默認的chunksize大小為64M,示例修改命令如下:
#use config
#db.settings.save( { _id:"chunksize", value: <sizeInMB> } )
use config
db.settings.save( { _id:"chunksize", value: 1 } )
#為test數據庫開啟分片
#選擇一個片鍵age並指定一個集合mycoll對其進行分片
sh.enableSharding("test")
sh.shardCollection("test.mycoll", {"age": 1})
#測試分片,寫入數據到數據庫中
use test
for (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()
mongo分片集群部署