文章目录
mongo集群搭建
1. 环境介绍
192.168.100.10 u10
192.168.100.11 u12
192.168.100.12 u11
2. 配置hosts文件
配置路径 /etc/hosts
3. 下载mongo
https://downloads.mongodb.com/linux/mongodb-linux-x86_64-enterprise-rhel70-4.2.8.tgz
4. 解压创建文件夹
执行以下命令
mkdir -p /momgodb
cd mongodb
tar -zxvf mongodb-linux-x86_64-enterprise-rhel70-4.2.8.tgz
mv mongodb-linux-x86_64-enterprise-rhel70-4.2.8 mongodb-4.2.8
mkdir -p logs
mkdir -p conf
mkdir -p data/{0,1,2,scvr}
5. 配置副本集
每台机器创建以下三个文件到conf下
systemLog:
destination: file
path: "/mongodb/logs/mongod0.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/mongodb/data/0"
processManagement:
fork: true
net:
bindIp: 0.0.0.0
port: 27016
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: "rs0"
sharding:
clusterRole: shardsvr
systemLog:
destination: file
path: "/mongodb/logs/mongod1.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/mongodb/data/1"
processManagement:
fork: true
net:
bindIp: 0.0.0.0
port: 27017
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: "rs1"
sharding:
clusterRole: shardsvr
systemLog:
destination: file
path: "/mongodb/logs/mongod2.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/mongodb/data/2"
processManagement:
fork: true
net:
bindIp: 0.0.0.0
port: 27018
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: "rs2"
sharding:
clusterRole: shardsvr
6. 生成auth_key.conf
openssl rand -base64 756 > /mongodb/conf/auth_key.conf
chmod 400 /mongodb/conf/auth_key.conf
将文件分别复制到三台机器上的conf目录下
例如:scp /mongodb/conf/auth_key.conf 192.168.100.11:/mongodb/conf/
7. 配置服务器
将下面文件分别复制到三台服务器conf下
systemLog:
destination: file
path: "/mongodb/logs/csvr.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/mongodb/data/csvr"
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
bindIp: 0.0.0.0
port: 27019
replication:
oplogSizeMB: 2048
replSetName: csvr
sharding:
clusterRole: configsvr
processManagement:
fork: true
8. 配置mongos
将下面文件分别复制到三台服务器conf下
systemLog:
destination: file
path: "/mongodb/logs/mongos.log"
logAppend: true
net:
bindIp: 0.0.0.0
port: 27020
sharding:
configDB: csvr/u10:27019,u11:27019,u12:27019
processManagement:
fork: true
9. 启动配置服务
三台机器分别启动
在mongodb目录下执行如下命令
mongodb-4.2.8/bin/mongod -f conf/csvr.yaml
启动完成后随便一台机器中执行
mongodb-4.2.8/bin/mongo -port 27019
rs.initiate( {
_id : "csvr",
configsvr: true,
members: [
{ _id: 0, host: "u10:27019" },
{ _id: 1, host: "u11:27019" },
{ _id: 2, host: "u12:27019" }
]
})
10. 启动配置副本集
三台机器分别启动
在mongodb目录下执行如下命令
mongodb-4.2.8/bin/mongod -f conf/mongod0.yaml
mongodb-4.2.8/bin/mongod -f conf/mongod1.yaml
mongodb-4.2.8/bin/mongod -f conf/mongod2.yaml
启动完成后随便一台机器中执行
mongodb-4.2.8/bin/mongo -port 27016
rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "u10:27016" },
{ _id: 1, host: "u11:27016" },
{ _id: 2, host: "u12:27016" }
]
})
mongodb-4.2.8/bin/mongo -port 27017
rs.initiate( {
_id : "rs1",
members: [
{ _id: 0, host: "u10:27017" },
{ _id: 1, host: "u11:27017" },
{ _id: 2, host: "u12:27017" }
]
})
mongodb-4.2.8/bin/mongo -port 27018
rs.initiate( {
_id : "rs2",
members: [
{ _id: 0, host: "u10:27018" },
{ _id: 1, host: "u11:27018" },
{ _id: 2, host: "u12:27018" }
]
})
11. 启动配置mongos
三台机器分别启动
在mongodb目录下执行如下命令
mongodb-4.2.8/bin/mongos -f conf/mongos.yaml
启动完成后随便一台机器中执行
mongodb-4.2.8/bin/mongo -port 27020
添加分片
use admin
db.runCommand( { addshard : "rs0/u10:27016,u11:27016,u12:27016",name:"shard1"} )
db.runCommand( { addshard : "rs1/u10:27017,u11:27017,u12:27017",name:"shard2"} )
db.runCommand( { addshard : "rs2/u10:27018,u11:27018,u12:27018",name:"shard3"} )
sh.status()
db.runCommand( { enablesharding :"dbName"});
12. 开启密码验证
首先创建root用户
随便一台机器中执行
mongodb-4.2.8/bin/mongo -port 27020
use admin
db.createUser(
{
user:"root",
pwd:"root",
roles:[{role:"root",db:"admin"}]
}
)
创建指定数据库用户
db.createUser({user:"normal", pwd:"isiteam", roles:[{role:"dbOwner", db:"product"}]})
取消conf下面yaml配置文件中注释的登录验证配置(一共四个配置)
重新启动服务
启动顺序只能为:
1.启动配置服务
2.启动副本集
3.启动mongos
然后可以用创建的账号进行登录
地址为192.168.100.10:27020;192.168.100.11:27020;192.168.100.12:27020中的任何一个
启动脚本
mongodb-4.2.8/bin/mongod -f conf/csvr.yaml
mongodb-4.2.8/bin/mongod -f conf/mongod0.yaml
mongodb-4.2.8/bin/mongod -f conf/mongod1.yaml
mongodb-4.2.8/bin/mongod -f conf/mongod2.yaml
mongodb-4.2.8/bin/mongos -f conf/mongos.yaml
13. springboot配置链接
spring:
data:
mongodb:
uri: mongodb://normal:isiteam@192.168.100.10:27020,192.168.100.11:27020,192.168.100.12:27020/product?authSource=admin&authMechanism=SCRAM-SHA-1&maxPoolSize=500&minPoolSize=10