2018年09月30日 10:03:08 TOTORO_zip 閱讀數 916更多linux
分類專欄: MangoDBnginx
https://www.jianshu.com/p/e1855a1d38b7mongodb
根據對象存儲平臺Django+MongoDB+Ceph的需求,現搭建部署一個十節點的MongoDB集羣,主要如下關鍵點:數據庫
系統:CentOS7.0 64bitvim
十臺服務器:mongo00-mongo09服務器
服務器規劃:app
mongo00 | mongo01 | mongo02 | mongo03 | mongo04 | mongo05 | mongo06 | mongo07 | mongo08 | mongo09 |
---|---|---|---|---|---|---|---|---|---|
mongos | mongos | mongos | mongos | mongos | mongos | mongos | config | config | config |
shard0主 | shard1主 | shard2主 | shard3主 | shard4主 | shard5主 | shard6主 | shard7主 | shard8主 | shard9主 |
shard9副 | shard0副 | shard1副 | shard2副 | shard3副 | shard4副 | shard5副 | shard6副 | shard7副 | shard8副 |
shard8仲 | shard9仲 | shard0仲 | shard1仲 | shard2仲 | shard3仲 | shard4仲 | shard5仲 | shard6仲 | shard7仲 |
端口分配:運維
mongos:20000
linux運維
config:21000
spa
shard0:27000
shard1:27001
shard2:27002
shard3:27003
shard4:27004
shard5:27005
shard6:27006
shard7:27007
shard8:27008
shard9:27009
#官網下載
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.2.tgz
#解壓
tar -xzvf mongodb-linux-x86_64-rhel70-4.0.2.tgz -C /usr/local/
cd /use/local
mv mongodb-linux-x86_64-rhel70-4.0.2 mongodb
#mongo服務經過配置文件啓動,存放配置文件目錄/usr/local/mongodb/conf
#存放日誌、進程管理信息的目錄/var/log/nginx/mongodb
#根據服務器規劃,在每臺服務器建立該節點所含shard的數據存放目錄/mnt/mongodata/shard0-9
#同時在mongo07-mongo09三臺服務器建立存放config server數據的數據目錄/mnt/mongodata/config
#mongo00:
mkdir -p /usr/local/mongodb/conf
mkdir -p /var/log/nginx/mongodb
mkdir -p /mnt/mongodata/shard0
mkdir -p /mnt/mongodata/shard9
mkdir -p /mnt/mongodata/shard8
#mongo01:
mkdir -p /usr/local/mongodb/conf
mkdir -p /var/log/nginx/mongodb
mkdir -p /mnt/mongodata/shard0
mkdir -p /mnt/mongodata/shard1
mkdir -p /mnt/mongodata/shard9
#mongo02:
mkdir -p /usr/local/mongodb/conf
mkdir -p /var/log/nginx/mongodb
mkdir -p /mnt/mongodata/shard0
mkdir -p /mnt/mongodata/shard1
mkdir -p /mnt/mongodata/shard2
#mongo03:
mkdir -p /usr/local/mongodb/conf
mkdir -p /var/log/nginx/mongodb
mkdir -p /mnt/mongodata/shard1
mkdir -p /mnt/mongodata/shard2
mkdir -p /mnt/mongodata/shard3
#mongo04:
mkdir -p /usr/local/mongodb/conf
mkdir -p /var/log/nginx/mongodb
mkdir -p /mnt/mongodata/shard2
mkdir -p /mnt/mongodata/shard3
mkdir -p /mnt/mongodata/shard4
#mongo05:
mkdir -p /usr/local/mongodb/conf
mkdir -p /var/log/nginx/mongodb
mkdir -p /mnt/mongodata/shard3
mkdir -p /mnt/mongodata/shard4
mkdir -p /mnt/mongodata/shard5
#mongo06:
mkdir -p /usr/local/mongodb/conf
mkdir -p /var/log/nginx/mongodb
mkdir -p /mnt/mongodata/shard4
mkdir -p /mnt/mongodata/shard5
mkdir -p /mnt/mongodata/shard6
#mongo07:
mkdir -p /usr/local/mongodb/conf
mkdir -p /var/log/nginx/mongodb
mkdir -p /mnt/mongodata/shard5
mkdir -p /mnt/mongodata/shard6
mkdir -p /mnt/mongodata/shard7
mkdir -p /mnt/mongodata/config
#mongo08:
mkdir -p /usr/local/mongodb/conf
mkdir -p /var/log/nginx/mongodb
mkdir -p /mnt/mongodata/shard6
mkdir -p /mnt/mongodata/shard7
mkdir -p /mnt/mongodata/shard8
mkdir -p /mnt/mongodata/config
#mongo09:
mkdir -p /usr/local/mongodb/conf
mkdir -p /var/log/nginx/mongodb
mkdir -p /mnt/mongodata/shard7
mkdir -p /mnt/mongodata/shard8
mkdir -p /mnt/mongodata/shard9
mkdir -p /mnt/mongodata/config
vim /etc/profile
#加入如下內容
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
#當即生效
source /etc/profile
使用命令mongod -v
輸出信息版本信息驗證環境變量是否配置成功
一、config server配置服務器(副本集)
根據服務器規劃,在mongo07-mongo09上部署三臺config server副本集,在該三臺服務器上分別添加如下配置文件:
vim /usr/local/mongodb/conf/config.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/nginx/mongodb/config.log
# Where and how to store data.
storage:
dbPath: /mnt/mongodata/config
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /var/log/nginx/mongodb/configsrv.pid
# network interfaces
net:
port: 21000
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: configs
sharding:
clusterRole: configsvr
啓動這三臺服務器的config server
mongod -f /usr/local/mongodb/conf/config.conf
登錄任意一臺服務器,初始化副本集
mongo --port 21000
#定義副本集配置(鍵「_id」對應的值必須與配置文件中的replicaction.replSetName一致)
config = {
... _id : "configs",
... members : [
... {_id : 0, host : "mongo07:21000" },
... {_id : 1, host : "mongo08:21000" },
... {_id : 2, host : "mongo09:21000" }
... ]
... }
#初始化副本集
rs.initiate(config)
#查看分區狀態
rs.status();
二、shard server分片服務器(副本集)
配置shard0副本集
在mongo00、mongo0一、mongo02服務器上作如下配置:
vim /usr/local/mongodb/conf/shard0.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/nginx/mongodb/shard0.log
# Where and how to store data.
storage:
dbPath: /mnt/mongodata/shard0
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /var/log/nginx/mongodb/shard0.pid
# network interfaces
net:
port: 27000
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard0
sharding:
clusterRole: shardsvr
啓動這三臺服務器的shard0 server
mongod -f /usr/local/mongodb/conf/shard0.conf
登錄任意一臺服務器,初始化副本集
mongo --port 27000
#定義副本集配置(鍵「_id」對應的值必須與配置文件中的replicaction.replSetName一致,priority表明權重[1,100],大的被分配爲主服務器,0永久不會變爲主服務器)
config = {
... _id : "shard0",
... members : [
... {_id : 0, host : "mongo00:27000", priority : 2 },
... {_id : 1, host : "mongo01:27000", priority : 1 },
... {_id : 2, host : "mongo02:27000", arbiterOnly :true }
... ]
... }
#初始化副本集
rs.initiate(config)
#查看分區狀態
rs.status();
配置shard1副本集
在mongo0一、mongo0二、mongo03服務器上作如下配置:
vim /usr/local/mongodb/conf/shard1.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/nginx/mongodb/shard1.log
# Where and how to store data.
storage:
dbPath: /mnt/mongodata/shard1
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /var/log/nginx/mongodb/shard1.pid
# network interfaces
net:
port: 27001
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
啓動這三臺服務器的shard1 server
mongod -f /usr/local/mongodb/conf/shard1.conf
登錄任意一臺服務器,初始化副本集
mongo --port 27001
#定義副本集配置(鍵「_id」對應的值必須與配置文件中的replicaction.replSetName一致,priority表明權重[1,100],大的被分配爲主服務器,0永久不會變爲主服務器)
config = {
... _id : "shard1",
... members : [
... {_id : 0, host : "mongo01:27001", priority : 2 },
... {_id : 1, host : "mongo02:27001", priority : 1 },
... {_id : 2, host : "mongo03:27001", arbiterOnly :true }
... ]
... }
#初始化副本集
rs.initiate(config)
#查看分區狀態
rs.status();
配置shard2副本集
在mongo0二、mongo0三、mongo04服務器上作如下配置:
vim /usr/local/mongodb/conf/shard2.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/nginx/mongodb/shard2.log
# Where and how to store data.
storage:
dbPath: /mnt/mongodata/shard2
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /var/log/nginx/mongodb/shard2.pid
# network interfaces
net:
port: 27002
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr
啓動這三臺服務器的shard2 server
mongod -f /usr/local/mongodb/conf/shard2.conf
登錄任意一臺服務器,初始化副本集
mongo --port 27002
#定義副本集配置(鍵「_id」對應的值必須與配置文件中的replicaction.replSetName一致,priority表明權重[1,100],大的被分配爲主服務器,0永久不會變爲主服務器)
config = {
... _id : "shard2",
... members : [
... {_id : 0, host : "mongo02:27002", priority : 2 },
... {_id : 1, host : "mongo03:27002", priority : 1 },
... {_id : 2, host : "mongo04:27002", arbiterOnly :true }
... ]
... }
#初始化副本集
rs.initiate(config)
#查看分區狀態
rs.status();
配置shard3副本集
在mongo0三、mongo0四、mongo05服務器上作如下配置:
vim /usr/local/mongodb/conf/shard3.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/nginx/mongodb/shard3.log
# Where and how to store data.
storage:
dbPath: /mnt/mongodata/shard3
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /var/log/nginx/mongodb/shard3.pid
# network interfaces
net:
port: 27003
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard3
sharding:
clusterRole: shardsvr
啓動這三臺服務器的shard3 server
mongod -f /usr/local/mongodb/conf/shard3.conf
登錄任意一臺服務器,初始化副本集
mongo --port 27003
#定義副本集配置(鍵「_id」對應的值必須與配置文件中的replicaction.replSetName一致,priority表明權重[1,100],大的被分配爲主服務器,0永久不會變爲主服務器)
config = {
... _id : "shard3",
... members : [
... {_id : 0, host : "mongo03:27003", priority : 2 },
... {_id : 1, host : "mongo04:27003", priority : 1 },
... {_id : 2, host : "mongo05:27003", arbiterOnly :true }
... ]
... }
#初始化副本集
rs.initiate(config)
#查看分區狀態
rs.status();
配置shard4副本集
在mongo0四、mongo0五、mongo06服務器上作如下配置:
vim /usr/local/mongodb/conf/shard4.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/nginx/mongodb/shard4.log
# Where and how to store data.
storage:
dbPath: /mnt/mongodata/shard4
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /var/log/nginx/mongodb/shard4.pid
# network interfaces
net:
port: 27004
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard4
sharding:
clusterRole: shardsvr
啓動這三臺服務器的shard4 server
mongod -f /usr/local/mongodb/conf/shard4.conf
登錄任意一臺服務器,初始化副本集
mongo --port 27004
#定義副本集配置(鍵「_id」對應的值必須與配置文件中的replicaction.replSetName一致,priority表明權重[1,100],大的被分配爲主服務器,0永久不會變爲主服務器)
config = {
... _id : "shard4",
... members : [
... {_id : 0, host : "mongo04:27004", priority : 2 },
... {_id : 1, host : "mongo05:27004", priority : 1 },
... {_id : 2, host : "mongo06:27004", arbiterOnly :true }
... ]
... }
#初始化副本集
rs.initiate(config)
#查看分區狀態
rs.status();
配置shard5副本集
在mongo0五、mongo0六、mongo07服務器上作如下配置:
vim /usr/local/mongodb/conf/shard5.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/nginx/mongodb/shard5.log
# Where and how to store data.
storage:
dbPath: /mnt/mongodata/shard5
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /var/log/nginx/mongodb/shard5.pid
# network interfaces
net:
port: 27005
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard5
sharding:
clusterRole: shardsvr
啓動這三臺服務器的shard5 server
mongod -f /usr/local/mongodb/conf/shard5.conf
登錄任意一臺服務器,初始化副本集
mongo --port 27005
#定義副本集配置(鍵「_id」對應的值必須與配置文件中的replicaction.replSetName一致,priority表明權重[1,100],大的被分配爲主服務器,0永久不會變爲主服務器)
config = {
... _id : "shard5",
... members : [
... {_id : 0, host : "mongo05:27005", priority : 2 },
... {_id : 1, host : "mongo06:27005", priority : 1 },
... {_id : 2, host : "mongo07:27005", arbiterOnly :true }
... ]
... }
#初始化副本集
rs.initiate(config)
#查看分區狀態
rs.status();
配置shard6副本集
在mongo0六、mongo0七、mongo08服務器上作如下配置:
vim /usr/local/mongodb/conf/shard6.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/nginx/mongodb/shard6.log
# Where and how to store data.
storage:
dbPath: /mnt/mongodata/shard6
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /var/log/nginx/mongodb/shard6.pid
# network interfaces
net:
port: 27006
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard6
sharding:
clusterRole: shardsvr
啓動這三臺服務器的shard6 server
mongod -f /usr/local/mongodb/conf/shard6.conf
登錄任意一臺服務器,初始化副本集
mongo --port 27006
#定義副本集配置(鍵「_id」對應的值必須與配置文件中的replicaction.replSetName一致,priority表明權重[1,100],大的被分配爲主服務器,0永久不會變爲主服務器)
config = {
... _id : "shard6",
... members : [
... {_id : 0, host : "mongo06:27006", priority : 2 },
... {_id : 1, host : "mongo07:27006", priority : 1 },
... {_id : 2, host : "mongo08:27006", arbiterOnly :true }
... ]
... }
#初始化副本集
rs.initiate(config)
#查看分區狀態
rs.status();
配置shard7副本集
在mongo0七、mongo0八、mongo09服務器上作如下配置:
vim /usr/local/mongodb/conf/shard7.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/nginx/mongodb/shard7.log
# Where and how to store data.
storage:
dbPath: /mnt/mongodata/shard7
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /var/log/nginx/mongodb/shard7.pid
# network interfaces
net:
port: 27007
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard7
sharding:
clusterRole: shardsvr
啓動這三臺服務器的shard7 server
mongod -f /usr/local/mongodb/conf/shard7.conf
登錄任意一臺服務器,初始化副本集
mongo --port 27007
#定義副本集配置(鍵「_id」對應的值必須與配置文件中的replicaction.replSetName一致,priority表明權重[1,100],大的被分配爲主服務器,0永久不會變爲主服務器)
config = {
... _id : "shard7",
... members : [
... {_id : 0, host : "mongo07:27007", priority : 2 },
... {_id : 1, host : "mongo08:27007", priority : 1 },
... {_id : 2, host : "mongo09:27007", arbiterOnly :true }
... ]
... }
#初始化副本集
rs.initiate(config)
#查看分區狀態
rs.status();
配置shard8副本集
在mongo0八、mongo0九、mongo00服務器上作如下配置:
vim /usr/local/mongodb/conf/shard8.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/nginx/mongodb/shard8.log
# Where and how to store data.
storage:
dbPath: /mnt/mongodata/shard8
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /var/log/nginx/mongodb/shard8.pid
# network interfaces
net:
port: 27008
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard8
sharding:
clusterRole: shardsvr
啓動這三臺服務器的shard8 server
mongod -f /usr/local/mongodb/conf/shard8.conf
登錄任意一臺服務器,初始化副本集
mongo --port 27008
#定義副本集配置(鍵「_id」對應的值必須與配置文件中的replicaction.replSetName一致,priority表明權重[1,100],大的被分配爲主服務器,0永久不會變爲主服務器)
config = {
... _id : "shard8",
... members : [
... {_id : 0, host : "mongo08:27008", priority : 2 },
... {_id : 1, host : "mongo09:27008", priority : 1 },
... {_id : 2, host : "mongo00:27008", arbiterOnly :true }
... ]
... }
#初始化副本集
rs.initiate(config)
#查看分區狀態
rs.status();
配置shard9副本集
在mongo0九、mongo00、mongo01服務器上作如下配置:
vim /usr/local/mongodb/conf/shard9.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/nginx/mongodb/shard9.log
# Where and how to store data.
storage:
dbPath: /mnt/mongodata/shard9
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /var/log/nginx/mongodb/shard9.pid
# network interfaces
net:
port: 27009
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard9
sharding:
clusterRole: shardsvr
啓動這三臺服務器的shard9 server
mongod -f /usr/local/mongodb/conf/shard9.conf
登錄任意一臺服務器,初始化副本集
mongo --port 27009
#定義副本集配置(鍵「_id」對應的值必須與配置文件中的replicaction.replSetName一致,priority表明權重[1,100],大的被分配爲主服務器,0永久不會變爲主服務器)
config = {
... _id : "shard9",
... members : [
... {_id : 0, host : "mongo09:27009", priority : 2 },
... {_id : 1, host : "mongo00:27009", priority : 1 },
... {_id : 2, host : "mongo01:27009", arbiterOnly :true }
... ]
... }
#初始化副本集
rs.initiate(config)
#查看分區狀態
rs.status();
三、mongos server路由服務器(在mongo00-mongo06上配置)
注意:啓動mongodb時,先啓動配置服務器和分片服務器,最後啓動路由服務器
vim /usr/local/mongodb/conf/mongos.conf
systemLog:
destination: file
logAppend: true
path: /var/log/nginx/mongodbmongos.log
processManagement:
fork: true
# pidFilePath: /var/log/nginx/mongodbmongos.pid
# network interfaces
net:
port: 20000
bindIp: 0.0.0.0
#監聽的配置服務器,只能有1個或者3個 configs爲配置服務器的副本集名字
sharding:
configDB: configs/mongo07:21000,mongo08:21000,mongo09:21000
啓動這七臺服務器的mongos server
mongod -f /usr/local/mongodb/conf/mongos.conf
四、啓用分片
目前已經搭建好配置服務器、數據分片服務器、路由服務器,下面進行分片啓用,使得app鏈接到路由服務器時可使用分片機制
登陸任意一臺mongos
mongo --port 20000
#使用admin數據庫
use admin
#串聯路由服務器與分片副本集
sh.addShard("shard0/mongo00:27000,mongo01:27000,mongo02:27000")
sh.addShard("shard1/mongo01:27001,mongo02:27001,mongo03:27001")
sh.addShard("shard2/mongo02:27002,mongo03:27002,mongo04:27002")
sh.addShard("shard3/mongo03:27003,mongo04:27003,mongo05:27003")
sh.addShard("shard4/mongo04:27004,mongo05:27004,mongo06:27004")
sh.addShard("shard5/mongo05:27005,mongo06:27005,mongo07:27005")
sh.addShard("shard6/mongo06:27006,mongo07:27006,mongo08:27006")
sh.addShard("shard7/mongo07:27007,mongo08:27007,mongo09:27007")
sh.addShard("shard8/mongo08:27008,mongo09:27008,mongo00:27008")
sh.addShard("shard9/mongo09:27009,mongo00:27009,mongo01:27009")
#查看集羣狀態
sh.status()
培訓四個月的linux運維,能達到什麼樣的水平?