說明
只需要在一個節(jié)點執(zhí)行即可,下載安裝不做描述,主要是配置說明
節(jié)點信息
192.168.1.1 node0
192.168.1.2 node1
192.168.1.3 node2
192.168.1.4 node3
192.168.1.5 node4
分配
config節(jié)點:node2,node3,node4
router節(jié)點:node0,node1
shard節(jié)點:node0,node1,node2,node3,node4
清空數(shù)據(jù)
for ip in 1 2 3 4 5
do
for i in 1 2 3 4 5
do
ssh 192.168.1.$ip "rm -rf /home/mongodb/data/shard$i/*"
done
ssh 192.168.1.$ip "rm -rf /home/mongodb/data/{config,mongos}/*"
done
創(chuàng)建數(shù)據(jù)目錄
for ip in 1 2 3 4 5
do
for i in 1 2 3 4 5
do
ssh 192.168.1.$ip "mkdir -p /home/mongodb/data/shard$i/{data,log}"
done
ssh 192.168.1.$ip "mkdir -p /home/mongodb/data/{config,mongos}/{data,log}"
done
創(chuàng)建config配置
for ip in 3 4 5
do
ssh 192.168.1.$ip "echo '## content
systemLog:
destination: file
logAppend: true
path: /home/mongodb/data/config/log/config.log
# Where and how to store data.
storage:
dbPath: /home/mongodb/data/config/data
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /home/mongodb/data/config/log/configsrv.pid
# network interfaces
net:
port: 21000
bindIp: 192.168.1.$ip
#operationProfiling:
replication:
replSetName: config
sharding:
clusterRole: configsvr ' > /home/mongodb/config/config.conf "
done
啟動config
for ip in 3 4 5
do
ssh 192.168.1.$ip "/home/mongodb/bin/mongod --config /home/mongodb/config/config.conf"
done
配置config副本集
連接
mongo 192.168.1.3:21000
config變量
config = {
_id : "config",
members : [
{_id : 0, host : "192.168.1.3:21000" },
{_id : 1, host : "192.168.1.4:21000" },
{_id : 2, host : "192.168.1.5:21000" }
]
}
初始化副本集
rs.initiate(config)
查看分區(qū)狀態(tài)
rs.status();
創(chuàng)建shard1
for ip in 1 2 3
do
ssh 192.168.1.$ip "echo '#配置文件內(nèi)容
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /home/mongodb/data/shard1/log/shard1.log
# Where and how to store data.
storage:
dbPath: /home/mongodb/data/shard1/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 20
# how the process runs
processManagement:
fork: true
pidFilePath: /home/mongodb/data/shard1/log/shard1.pid
# network interfaces
net:
port: 27001
bindIp: 192.168.1.$ip
#operationProfiling:
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr ' > /home/mongodb/config/shard1.conf"
done
創(chuàng)建shard2
for ip in 2 3 4
do
ssh 192.168.1.$ip "echo '#配置文件內(nèi)容
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /home/mongodb/data/shard2/log/shard2.log
# Where and how to store data.
storage:
dbPath: /home/mongodb/data/shard2/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 20
# how the process runs
processManagement:
fork: true
pidFilePath: /home/mongodb/data/shard2/log/shard2.pid
# network interfaces
net:
port: 27002
bindIp: 192.168.1.$ip
#operationProfiling:
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr ' > /home/mongodb/config/shard2.conf"
done
創(chuàng)建shard3
for ip in 3 4 5
do
ssh 192.168.1.$ip "echo '#配置文件內(nèi)容
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /home/mongodb/data/shard3/log/shard3.log
# Where and how to store data.
storage:
dbPath: /home/mongodb/data/shard3/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 20
# how the process runs
processManagement:
fork: true
pidFilePath: /home/mongodb/data/shard3/log/shard3.pid
# network interfaces
net:
port: 27003
bindIp: 192.168.1.$ip
#operationProfiling:
replication:
replSetName: shard3
sharding:
clusterRole: shardsvr ' > /home/mongodb/config/shard3.conf"
done
創(chuàng)建shard4
for ip in 4 5 1
do
ssh 192.168.1.$ip "echo '#配置文件內(nèi)容
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /home/mongodb/data/shard4/log/shard4.log
# Where and how to store data.
storage:
dbPath: /home/mongodb/data/shard4/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 20
# how the process runs
processManagement:
fork: true
pidFilePath: /home/mongodb/data/shard4/log/shard4.pid
# network interfaces
net:
port: 27004
bindIp: 192.168.1.$ip
#operationProfiling:
replication:
replSetName: shard4
sharding:
clusterRole: shardsvr ' > /home/mongodb/config/shard4.conf"
done
創(chuàng)建shard5
for ip in 5 1 2
do
ssh 192.168.1.$ip "echo '#配置文件內(nèi)容
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /home/mongodb/data/shard5/log/shard5.log
# Where and how to store data.
storage:
dbPath: /home/mongodb/data/shard5/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 20
# how the process runs
processManagement:
fork: true
pidFilePath: /home/mongodb/data/shard5/log/shard5.pid
# network interfaces
net:
port: 27005
bindIp: 192.168.1.$ip
#operationProfiling:
replication:
replSetName: shard5
sharding:
clusterRole: shardsvr ' > /home/mongodb/config/shard5.conf"
done
啟動shard
for ip in 1 4 2 3 5
do
for i in 1 2 3 4 5
do
ssh 192.168.1.$ip "/home/mongodb/bin/mongod --config /home/mongodb/config/shard$i.conf"
done
done
配置shard1副本集
連接
mongo 192.168.1.1:27001
config變量
config = {
_id : "shard1",
members : [
{_id : 0, host : "192.168.1.1:27001" },
{_id : 1, host : "192.168.1.2:27001" },
{_id : 2, host : "192.168.1.3:27001" }
]
}
初始化副本集
rs.initiate(config)
查看分區(qū)狀態(tài)
rs.status();
配置shard2副本集
連接
mongo 192.168.1.2:27002
config變量
config = {
_id : "shard2",
members : [
{_id : 0, host : "192.168.1.2:27002" },
{_id : 1, host : "192.168.1.3:27002" },
{_id : 2, host : "192.168.1.4:27002" }
]
}
初始化副本集
rs.initiate(config)
查看分區(qū)狀態(tài)
rs.status();
配置shard3副本集
連接
mongo 192.168.1.3:27003
config變量
config = {
_id : "shard3",
members : [
{_id : 0, host : "192.168.1.3:27003" },
{_id : 1, host : "192.168.1.4:27003" },
{_id : 2, host : "192.168.1.5:27003" }
]
}
初始化副本集
rs.initiate(config)
查看分區(qū)狀態(tài)
rs.status();
配置shard4副本集
連接
mongo 192.168.1.4:27004
config變量
config = {
_id : "shard4",
members : [
{_id : 0, host : "192.168.1.4:27004" },
{_id : 1, host : "192.168.1.5:27004" },
{_id : 2, host : "192.168.1.1:27004" }
]
}
初始化副本集
rs.initiate(config)
查看分區(qū)狀態(tài)
rs.status();
配置shard5副本集
連接
mongo 192.168.1.5:27005
config變量
config = {
_id : "shard5",
members : [
{_id : 0, host : "192.168.1.5:27005" },
{_id : 1, host : "192.168.1.1:27005" },
{_id : 2, host : "192.168.1.2:27005" }
]
}
初始化副本集
rs.initiate(config)
查看分區(qū)狀態(tài)
rs.status();
創(chuàng)建mongos
for ip in 1 2
do
ssh 192.168.1.$ip "echo 'systemLog:
destination: file
logAppend: true
path: /home/mongodb/data/mongos/log/mongos.log
processManagement:
fork: true
# pidFilePath: /home/mongodb/data/mongos/log/mongos.pid
# network interfaces
net:
port: 20000
bindIp: 192.168.1.$ip
#監(jiān)聽的配置服務(wù)器,只能有1個或者3個 configs為配置服務(wù)器的副本集名字
sharding:
configDB: config/192.168.1.3:21000,192.168.1.4:21000,192.168.1.5:21000 ' > /home/mongodb/config/mongos.conf"
done
啟動mongos
for ip in 1 2
do
ssh 192.168.1.$ip "/home/mongodb/bin/mongos --config /home/mongodb/config/mongos.conf"
done
mongo 192.168.1.1:20000
使用admin數(shù)據(jù)庫
use admin
串聯(lián)路由服務(wù)器與分配副本集
sh.addShard("shard1/192.168.1.1:27001,192.168.1.2:27001,192.168.1.3:27001")
sh.addShard("shard2/192.168.1.2:27002,192.168.1.3:27002,192.168.1.4:27002")
sh.addShard("shard3/192.168.1.3:27003,192.168.1.4:27003,192.168.1.5:27003")
sh.addShard("shard4/192.168.1.4:27004,192.168.1.5:27004,192.168.1.1:27004")
sh.addShard("shard5/192.168.1.5:27005,192.168.1.1:27005,192.168.1.2:27005")
查看集群狀態(tài)
sh.status()