三个角色
mongos 单节点或双节点 configserver 至少三节点副本集 shard 至少三节点副本集
分片集群生产搭建参考文档 https://www.mongodb.com/docs/v4.2/core/sharded-cluster-components/ https://www.mongodb.com/docs/v4.2/tutorial/deploy-shard-cluster/
注意一定要关闭SELINUX 查看sestatus
安装mongodb
vim /etc/yum.repos.d/mongodb-org-4.2.repo
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
yum install -y mongodb-org mongodb-org-tools mongodb-org-shell
创建pid目录
mkdir /var/run/mongodb/
config搭建
config集群启动配置文件为3个组成replicaset
config1.conf
config2.conf
config3.conf
创建目录
mkdir -p /dbstorage/mongoconfig1
mkdir -p /dbstorage/mongoconfig2
mkdir -p /dbstorage/mongoconfig3
启动命令:
mongod --config config1.conf
mongod --config config2.conf
mongod --config config3.conf
配置文件
[root@localhost mongocluster]
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongodconfig1.log
storage:
dbPath: /dbstorage/mongoconfig1
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongodconfig1.pid
timeZoneInfo: /usr/share/zoneinfo
sharding:
clusterRole: configsvr
replication:
replSetName: mc0
net:
port: 27019
bindIp: localhost,10.1.1.10
[root@localhost mongocluster]
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongodconfig2.log
storage:
dbPath: /dbstorage/mongoconfig2
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongodconfig2.pid
timeZoneInfo: /usr/share/zoneinfo
sharding:
clusterRole: configsvr
replication:
replSetName: mc0
net:
port: 27020
bindIp: localhost,10.1.1.10
[root@localhost mongocluster]
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongodconfig3.log
storage:
dbPath: /dbstorage/mongoconfig3
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongodconfig3.pid
timeZoneInfo: /usr/share/zoneinfo
sharding:
clusterRole: configsvr
replication:
replSetName: mc0
net:
port: 27021
bindIp: localhost,10.1.1.10
初始化replicaset
rs.initiate(
{
_id: "mc0",
configsvr: true,
members: [
{ _id : 0, host : "10.1.1.10:27019" },
{ _id : 1, host : "10.1.1.10:27020" },
{ _id : 2, host : "10.1.1.10:27021" }
]
}
)
shard搭建
shard集群启动配置文件为3个组成replicaset
shard1.conf
shard2.conf
shard3.conf
创建目录
mkdir /dbstorage/mongoshard3
mkdir /dbstorage/mongoshard2
mkdir /dbstorage/mongoshard1
启动命令:
mongod --config shard1.conf
mongod --config shard2.conf
mongod --config shard3.conf
配置文件内容
[root@localhost mongocluster]
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongodshard1.log
storage:
dbPath: /dbstorage/mongoshard1
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongodshard1.pid
timeZoneInfo: /usr/share/zoneinfo
sharding:
clusterRole: shardsvr
replication:
replSetName: s0
net:
port: 27016
bindIp: localhost,10.1.1.10
[root@localhost mongocluster]
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongodshard2.log
storage:
dbPath: /dbstorage/mongoshard2
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongodshard2.pid
timeZoneInfo: /usr/share/zoneinfo
sharding:
clusterRole: shardsvr
replication:
replSetName: s0
net:
port: 27017
bindIp: localhost,10.1.1.10
[root@localhost mongocluster]
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongodshard3.log
storage:
dbPath: /dbstorage/mongoshard3
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongodshard3.pid
timeZoneInfo: /usr/share/zoneinfo
sharding:
clusterRole: shardsvr
replication:
replSetName: s0
net:
port: 27018
bindIp: localhost,10.1.1.10
设置replicaset
rs.initiate(
{
_id : "s0",
members: [
{ _id : 0, host : "10.1.1.10:27016" },
{ _id : 1, host : "10.1.1.10:27017" },
{ _id : 2, host : "10.1.1.10:27018" }
]
}
)
mongos节点
mongos集群启动配置文件为1个节点
mongos1.conf
创建目录
mkdir /dbstorage/mongos1
启动命令:
mongos --config mongos1.conf
配置文件内容
[root@localhost mongocluster]
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongodmongos1.log
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongodmongos1.pid
timeZoneInfo: /usr/share/zoneinfo
sharding:
configDB: mc0/10.1.1.10:27019,10.1.1.10:27020,10.1.1.10:27021
net:
port: 3717
bindIp: localhost,10.1.1.10
使用mongos连接mongo shard
mongo --host localhost --port 3717
将之前创建好的shard加入cluster,或者可以创建新的shard,加入集群
sh.addShard( "s0/10.1.1.10:27016,10.1.1.10:27017,10.1.1.10:27018")
查看状态
sh.status()
将一个数据库做sharding
sh.enableSharding("<database>")
对collection做sharding
sh.shardCollection("<database>.<collection>", { <shard key field> : "hashed" } )
sh.shardCollection("<database>.<collection>", { <shard key field> : 1, ... } )
附启动脚本,酌情修改
#!/bin/bash
/usr/bin/mongod --config /root/mongocluster/config1.conf
/usr/bin/mongod --config /root/mongocluster/config2.conf
/usr/bin/mongod --config /root/mongocluster/config3.conf
/usr/bin/mongod --config /root/mongocluster/shard1.conf
/usr/bin/mongod --config /root/mongocluster/shard2.conf
/usr/bin/mongod --config /root/mongocluster/shard3.conf
/usr/bin/mongos --config /root/mongocluster/mongos1.conf
|