MongoDB分片集群搭建
集群规划
node1实例 | node2实例 | node3实例 | node4实例 | node5实例 | 端口 | 副本集 |
---|---|---|---|---|---|---|
mongos | mongos | mongos | mongos | mongos | 27017 | mongos |
config | config | config | config | config | 27018 | config |
shard1主 | shard1副 | shard1仲裁 | shard1副 | shard1副 | 27019 | shard1 |
shard2仲裁 | shard2主 | shard2副 | shard2副 | shard2副 | 27020 | shard2 |
shard3副 | shard3仲裁 | shard3主 | shard3副 | shard3副 | 27021 | shard3 |
安装部署
生成认证文件
openssl rand -base64 741 > db.key
配置启动config server
编辑生成config server配置文件config.conf
systemLog:
destination: file
logAppend: true
path: /opt/data/mongodb/config/logs/config.log
storage:
dbPath: /opt/data/mongodb/config/data/
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: /opt/data/mongodb/config/data/config.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
replication:
replSetName: config
sharding:
clusterRole: configsvr
security:
authorization: enabled
keyFile: /opt/server/mongodb/bin/db.key
javascriptEnabled: true
net:
port: 27018
bindIp: 0.0.0.0
启动config实例
./mongod -f config.conf
配置启动shard1 server
编辑生成shard1 server配置文件shard1.conf
systemLog:
destination: file
logAppend: true
path: /opt/data/mongodb/shard1/logs/shard1.log
storage:
dbPath: /opt/data/mongodb/shard1/data/
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: /opt/data/mongodb/shard1/data/shard1.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
security:
authorization: enabled
keyFile: /opt/server/mongodb/bin/db.key
javascriptEnabled: true
net:
port: 27019
bindIp: 0.0.0.0
启动shard1实例
./mongod -f shard1.conf
配置启动shard2 server
编辑生成shard2 server配置文件shard2.conf
systemLog:
destination: file
logAppend: true
path: /opt/data/mongodb/shard2/logs/shard2.log
storage:
dbPath: /opt/data/mongodb/shard2/data/
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: /opt/data/mongodb/shard2/data/shard2.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr
security:
authorization: enabled
keyFile: /opt/server/mongodb/bin/db.key
javascriptEnabled: true
net:
port: 27020
bindIp: 0.0.0.0
启动shard1实例
./mongod -f shard2.conf
配置启动shard3 server
编辑生成shard3 server配置文件shard3.conf
systemLog:
destination: file
logAppend: true
path: /opt/data/mongodb/shard3/logs/shard3.log
storage:
dbPath: /opt/data/mongodb/shard3/data/
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: /opt/data/mongodb/shard3/data/shard3.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
replication:
replSetName: shard3
sharding:
clusterRole: shardsvr
security:
authorization: enabled
keyFile: /opt/server/mongodb/bin/db.key
javascriptEnabled: true
net:
port: 27021
bindIp: 0.0.0.0
启动shard1实例
./mongod -f shard3.conf
配置启动mongos server
编辑生成mongos server配置文件mongos.conf
systemLog: destination: file logAppend: true path: /opt/data/mongodb/mongos/logs/mongos.logprocessManagement: fork: true # fork and run in background pidFilePath: /opt/data/mongodb/mongos/data/mongos.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfosharding: configDB: config/node1:27018,node2:27018,node3:27018,node4:27018,node5:27018security: keyFile: /opt/server/mongodb/bin/db.key javascriptEnabled: truenet: port: 27017 bindIp: 0.0.0.0
启动mongos实例
./mongos -f mongos.conf
配置分片集
配置config分片
登录任一机器的config实例
./mongo --host node1 --port 27018
use adminconfig = {_id : "shard3",members : [ {_id : 0, host : "node1:27018"}, {_id : 1, host : "node2:27018"}, {_id : 2, host : "node3:27018"}, {_id : 3, host : "node4:27018"}, {_id : 4, host : "node5:27018"} ]}rs.initiate(config)rs.status()
配置shard1分片
登录任一机器的shard1实例
./mongo --host node1 --port 27019
use adminconfig = {_id : "shard3",members : [ {_id : 0, host : "node1:27019"}, {_id : 1, host : "node2:27019"}, {_id : 2, host : "node3:27019",arbiterOnly: true}, {_id : 3, host : "node4:27019"}, {_id : 4, host : "node5:27019"} ]}rs.initiate(config)rs.status()
配置shard2分片
登录任一机器的shard2实例
./mongo --host node1 --port 27020
use admin
config = {
_id : "shard3",
members : [
{_id : 0, host : "node1:27020",arbiterOnly: true},
{_id : 1, host : "node2:27020"},
{_id : 2, host : "node3:27020"},
{_id : 3, host : "node4:27020"},
{_id : 4, host : "node5:27020"}
]
}
rs.initiate(config)
rs.status()
配置shard3分片
登录任一机器的shard3实例
./mongo --host node1 --port 27021
use admin
config = {
_id : "shard3",
members : [
{_id : 0, host : "node1:27021"},
{_id : 1, host : "node2:27021",arbiterOnly: true},
{_id : 2, host : "node3:27021"},
{_id : 3, host : "node4:27021"},
{_id : 4, host : "node5:27021"}
]
}
rs.initiate(config)
rs.status()
在mongos添加分片信息
登录任一机器的mongos实例
./mongos --host node1 --port 27017
use admin
sh.addShard("shard3/node1:27021,node2:27021,node3:27021,node4:27021,node5:27021")
sh.addShard("shard2/node1:27020,node2:27020,node3:27020,node4:27020,node5:27020")
sh.addShard("shard1/node1:27019,node2:27019,node3:27019,node4:27019,node5:27019")
创建用户设置密码
创建管理员
use admin
db.createUser({
user: "root",
pwd: "passmongo",
roles: [{"role":"root","db":"admin"}],
})
db.createUser({
user: "dbadmin",
pwd: "passmongo",
roles: [{"role":"dbAdminAnyDatabase","db":"admin"}],
})
创建数据库用户
use db1
db.createUser({
user: 'db1owner',
pwd: 'db1owner',
roles: [ { role: "dbOwner", db: "db1" } ] });
db.createUser({
user: 'db1',
pwd: 'db1',
roles: [ { role: "readWrite", db: "db1" } ] });