目录
一、mongodb分片集群三种角色
二、安装
1、准备工作
2、安装
configsvr配置
router配置
三、测试
四、整合Springboot
192.168.178.134 |
CentOS Linux release 7.7.1908 (Core) |
27017 | router |
28018 | configsvr |
||
29019 | shardsvr |
||
29029 | shardsvr |
||
192.168.178.136 |
CentOS Linux release 7.7.1908 (Core) |
27017 | router |
28018 | configsvr | ||
29019 | shardsvr | ||
29029 | shardsvr | ||
192.168.178.137 |
CentOS Linux release 7.7.1908 (Core) |
27017 | router |
28018 | configsvr | ||
29019 | shardsvr | ||
29029 | shardsvr |
1.1、关闭selinux,关闭防火墙(或者开放端口)及安装软件包,在3台服务器上都执行。
#关闭Selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
#开启端口号27017
firewall-cmd --permanent --add-port=27017/tcp
firewall-cmd --reload
#关闭防火墙
systemctl stop firewalld
#安装工具
dnf -y install net-tools bash-completion vim wget
#重启
reboot
注意:安装工具包报错执行以下命令
yum install epel-release
yum install dnf -y
dnf -y install net-tools bash-completion vim wget
1.2、解压缩文件
#解压缩
tar -zxvf mongodb-linux-x86_64-enterprise-rhel70-6.0.3.tgz
#移动文件
mv mongodb-linux-x86_64-enterprise-rhel70-6.0.3 /usr/local/mongodb
1.3、创建存储配置的目录
mkdir -p /data/mongodb/{router,config,shard,key}
#创建日志文件
touch /data/mongodb/config/configsvr.log
#创建存储数据目录
mkdir /data/mongodb/config/data -p
1、 在/data/mongodb/config 创建configsvr.conf配置文件
systemLog:
destination: file
logAppend: true
path: /data/mongodb/config/configsvr.log
storage:
dbPath: /data/mongodb/config/data
journal:
enabled: true
processManagement:
fork: true
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28018
bindIp: 0.0.0.0
replication:
replSetName: mycluster
sharding:
clusterRole: configsvr
security:
authorization: enabled
keyFile: /data/mongodb/key/mongo.keyfile
注:上面配置在没有设置mongodb密码的情况下,请注释掉security对应的配置。
2、 启动mongodb
/usr/local/mongodb/bin/mongod -f /data/mongodb/config/configsvr.conf
#检查是否启动成功
netstat -antup | grep mongod
3、 登录客户端,配置configsvr
mongosh 192.168.178.134:28018
use admin
config = { _id:"mycluster",
configsvr: true,
members:[
{_id:0,host:"192.168.178.134:28018"},
{_id:1,host:"192.168.178.136:28018"},
{_id:2,host:"192.168.178.137:28018"}
]
}
rs.initiate(config)
#查看状态
rs.status();
1、在/data/mongodb/router/目录下创建router.conf配置文件。
systemLog:
destination: file
logAppend: true
path: /data/mongodb/router/router.log
processManagement:
fork: true
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27017
bindIp: 0.0.0.0
sharding:
configDB: mycluster/192.168.178.134:28018,192.168.178.136:28018,192.168.178.137:28018
2、创建日志文件启动
touch /data/mongodb/router/router.log
#启动,注意是mongos,不是mongod了
/usr/local/mongodb/bin/mongos -f /data/mongodb/router/router.conf
1、由于每个数据有两个分片,所以需要创建两个文件
mkdir -p /data/mongodb/shard/shardsvr1
mkdir -p /data/mongodb/shard/shardsvr2
2、配置文件
在上面的目录下创建shardsvr.conf配置文件
shardsvr1文件下对应的shardsvr.conf文件
systemLog:
destination: file
logAppend: true
path: /data/mongodb/shard/shardsvr1/shardsvr.log
storage:
dbPath: /data/mongodb/shard/shardsvr1
journal:
enabled: true
processManagement:
fork: true
timeZoneInfo: /usr/share/zoneinfo
net:
port: 29019
bindIp: 0.0.0.0
replication:
replSetName: myclusterdata1
sharding:
clusterRole: shardsvr
shardsvr2文件下对应的shardsvr.conf文件
systemLog:
destination: file
logAppend: true
path: /data/mongodb/shard/shardsvr2/shardsvr.log
storage:
dbPath: /data/mongodb/shard/shardsvr2
journal:
enabled: true
processManagement:
fork: true
timeZoneInfo: /usr/share/zoneinfo
net:
port: 29029
bindIp: 0.0.0.0
replication:
replSetName: myclusterdata2
sharding:
clusterRole: shardsvr
3、创建日志文件启动
touch /data/mongodb/shard/shardsvr1/shardsvr.log
touch /data/mongodb/shard/shardsvr2/shardsvr.log
#启动
/usr/local/mongodb/bin/mongod -f /data/mongodb/shard/shardsvr1/shardsvr.conf
/usr/local/mongodb/bin/mongod -f /data/mongodb/shard/shardsvr2/shardsvr.conf
4、配置集群
配置集群1并查看状态
mongosh 192.168.178.134:29019
use admin
config = { _id:"myclusterdata1",
members:[
{_id:0,host:"192.168.178.134:29019"},
{_id:1,host:"192.168.178.136:29019"},
{_id:2,host:"192.168.178.137:29019"}
]
}
rs.initiate(config)
rs.status()
配置集群2并查看状态
mongosh 192.168.178.134:29029
use admin
config = { _id:"myclusterdata2",
members:[
{_id:0,host:"192.168.178.134:29029"},
{_id:1,host:"192.168.178.136:29029"},
{_id:2,host:"192.168.178.137:29029"}
]
}
rs.initiate(config)
rs.status()
5、router中填加shardsvr
mongosh 192.168.178.134:27017
use admin
sh.addShard("myclusterdata1/192.168.178.134:29019,192.168.178.136:29019,192.168.178.137:29019")
sh.addShard("myclusterdata2/192.168.178.134:29029,192.168.178.136:29029,192.168.178.137:29029")
sh.status()
插入数据
mongosh 192.168.3.81:27017
use mydb
for(i=1; i<=500;i++){
db.myuser.insert( {name:'mytest'+i, age:i} )
}
sh.status()
db.myuser.count();
db.myuser.countDocuments()
db.myuser.estimatedDocumentCount()
切换到myclusterdata1节点
./mongosh 192.168.178.134:29019
use mydb
db.myuser.estimatedDocumentCount()
db.myuser.find({"name":"mytest400"});
可见在从分片上并没有数据,注意:默认情况下所有数据未分片存储,只存在主集群中。
数据分片存储
mongosh 192.168.178.134:27017
use mydb
db.dropDatabase()
use admin
db.runCommand( { enablesharding :"mydb"});
db.runCommand( { shardcollection : "mydb.myuser",key : {_id: "hashed"} } )
use mydb
for(i=1; i<=500;i++){
db.myuser.insert( {name:'mytest'+i, age:i} )
}
可以看到在myclusterdata1从库上也有了数据库mydb。
查看分片状态
sh.status();
Linux安装mongodb集群&&整合SpringBoot_熟透的蜗牛的博客-CSDN博客_springboot整合mongodb集群
参考: https://blog.csdn.net/w975121565/article/details/126978303