MongoDB分片集群搭建
2021-06-25 本文已影响0人
小屁孩cmq
1,服务器环境准备
进程 | IP地址 | 端口 |
---|---|---|
config server 配置服务器1(primary) | 139.196.240.226 | 28001 |
config server 配置服务器2(secondary) | 8.133.184.34 | 28001 |
config server 配置服务器3(secondary) | 139.224.244.2 | 28001 |
shard server1 主节点 | 139.196.240.226 | 28011 |
shard server1 仲裁 | 8.133.184.34 | 28011 |
shard server1 副节点 | 139.224.244.2 | 28011 |
shard server2 主节点 | 139.196.240.226 | 28012 |
shard server2 仲裁 | 8.133.184.34 | 28012 |
shard server2 副节点 | 139.224.244.2 | 28012 |
shard server3 主节点 | 139.196.240.226 | 28013 |
shard server3 仲裁 | 8.133.184.34 | 28013 |
shard server3 副节点 | 139.224.244.2 | 28013 |
路由服务器 | 139.196.240.226 | 28000 |
2,配置config server服务器
- 集群之间通信的安全认证证书生成
- 在每台机器的 /usr/local/mongodb 目录下创建证书存放目录:cert,如下:
cd /var/lib/mongo
mkdir cert
- 生成集群通信证书key文件mongokey,并且将mongokey文件复制到每台机器的 /usr/local/mongodb/cert 目录下
随便进入一台机器,通过openssl命令生成通信key,key文件名字为:mongokey,如下:
cd /
openssl rand -base64 745 > /var/lib/mongo/cert/mongokey
cd /var/lib/mongo/cert/
ls
# 必须将mongokey文件赋权限
chmod -R 600 mongokey
- 维护好配置文件config.conf,如下:
systemLog:
destination: file
path: /usr/local/monogodb/config/log/logs
logAppend: true
storage:
dbPath: /usr/local/mongodb/config/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /usr/local/mongodb/config/log/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
bindIp: 0.0.0.0
port: 28001
security:
#链接认证,也就是账号密码
authorization: enabled
#集群内部通信安全证书文件,每台的证书必须一致
keyFile: /usr/local/mongodb/cert/mongokey
sharding:
clusterRole: configsvr
replication:
replSetName: configs
- 启动mongo服务
cd /usr/local/mongodb/config/mongdb-4.4.2/bin
./mongod -f /usr/local/mongodb/config/conf/config.conf --fork
- 连接登录其中一个节点的mongo
cd /usr/local/mongodb/config/mongdb-4.4.2/bin
./mongo --port 28001
- 初始化副本集
> use admin
> config={_id:'configs',
configsvr:true,
members : [
{_id : 0, host : '139.224.244.2:28001' },
{_id : 1, host : '8.133.184.34:28001'},
{_id : 2, host : '139.196.240.226:28001' }
]}
> rs.initiate(config)
image.png
3,配置分片副本集
- 集群之间通信的安全认证证书生成
操作如上。
- 维护好配置文件config.conf,如下:
systemLog:
destination: file
path: /usr/local/monogodb/shard1/log/logs
logAppend: true
storage:
dbPath: /usr/local/mongodb/shard1/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /usr/local/mongodb/shard1/log/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
bindIp: 0.0.0.0
port: 28011
security:
#链接认证,也就是账号密码
authorization: enabled
#集群内部通信安全证书文件,每台的证书必须一致
keyFile: /usr/local/mongodb/cert/mongokey
sharding:
clusterRole: shardsvr
replication:
replSetName: shard1
- 启动mongo服务
cd /usr/local/mongodb/shard1/mongdb-4.4.2/bin
./mongod -f /usr/local/mongodb/shard1/conf/config.conf --fork
- 连接登录其中一个节点的mongo
cd /usr/local/mongodb/shard1/mongdb-4.4.2/bin
./mongo --port 28011
- 初始化副本集
> use admin
> config={_id:'shard1',
members : [
{_id : 0, host : '139.224.244.2:28011' },
{_id : 1, host : '8.133.184.34:28011'},
{_id : 2, host : '139.196.240.226:28011' }
]}
> rs.initiate(config)
- 同样的方法构建副本集shard2和shard3
4,配置路由服务器mongos
- 在三台服务器上安装mongoDB
cd /usr/local
mkdir mongodb
cd mongodb
mkdir mongos
安装mongoDB到config目录下,安装步骤参考MongoDB安装手册
- 集群之间通信的安全认证证书生成
操作如上。
- 维护好配置文件config.conf,如下:
systemLog:
destination: file
path: /usr/local/mongodb/mongos/log/logs
logAppend: true
processManagement:
fork: true
pidFilePath: /usr/local/mongodb/mongos/log/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
bindIp: 0.0.0.0
port: 28000
security:
#集群内部通信安全证书文件,每台的证书必须一致
keyFile: /usr/local/mongodb/cert/mongokey
sharding:
configDB: configs/139.224.244.2:28001,8.133.184.34:28001,139.19s6.240.22:28001
- 在其他节点创建账户认证
> use admin
> db.createUser(
{
user:"username",
pwd:"password",
roles:[{role:"root",db:"admin"}]
}
)
- 启动mongo服务
cd /usr/local/mongodb/mongos/mongdb-4.4.2/bin
./mongos -f /usr/local/mongodb/mongos/conf/config.conf --fork
- 启用分片
登录mongos路由服务器
mongo --port 28000
#使用admin数据库
use admin
#串联路由服务器与分配副本集
sh.addShard("shard1/139.224.244.2:28011,8.133.184.34:28011,139.196.240.226:28011")
sh.addShard("shard2/139.224.244.2:28012,8.133.184.34:28012,139.196.240.226:28012")
sh.addShard("shard3/139.224.244.2:28013,8.133.184.34:28013,139.196.240.226:28013")
image.png
5,测试分片
- 激活数据库分片功能
语法:( { enablesharding : "数据库名称" } )
mongos> db.runCommand( { enablesharding : "test" } )
- 指定分片建对集合分片,范围片键--创建索引
mongos> use admin
# 根据指定索引分片
mongos> db.runCommand( { shardcollection : "test.vast",key : {id: 1} } )
or
# 根据hash分片
mongos> db.runCommand( { shardcollection : "test.vast",key : {id: "hashed“} } )
- 集合分片验证
mongos> use test
mongos> for(i=0;i<20000;i++){ db.vast.insert({"id":i,"name":"clsn","age":70,"date":new Date()}); }
mongos> db.vast.stats()
排除掉不重要的信息的测试结果
image.png