mongo版本:7.0.5
操作系统:centos8 stream
集群环境
3个分片副本集
- shard1(dev1.mongodb.com:27018、dev2.mongodb.com:27018、dev3.mongodb.com:27018)
- shard2(dev1.mongodb.com:27019、dev2.mongodb.com:27019、dev3.mongodb.com:27019)
- shard3(dev1.mongodb.com:27020、dev2.mongodb.com:27020、dev3.mongodb.com:27020)
1个config副本集
- dev1.mongodb.com:28018
- dev2.mongodb.com:28018
- dev3.mongodb.com:28018
3个mongos(route)
- dev1.mongodb…com:29018
- dev2.mongodb.com:29018
- dev3.mongodb.com:29018
搭建分片副本集
mongodb下载:https://www.mongodb.com/try/download/community
解压安装
在dev1上安装第一个分片的副本集
tar xvf mongodb-linux-x86_64-rhel80-7.0.5.tgz
mv mongodb-linux-x86_64-rhel80-7.0.5 mongodb
添加副本集配置文件:mongo.conf
sharding:
clusterRole: shardsvr
processManagement:
fork: true
pidFilePath: /opt/mongodb/mongodb.pid
net:
bindIp: 0.0.0.0
port: 27018
storage:
# 这个路径要是不存在,就要手动创建,不然会报错
dbPath: /opt/mongodb/data/db
systemLog:
destination: file
path: /opt/mongodb/mongodb.log
logAppend: true
operationProfiling:
mode: slowOp
slowOpThresholdMs: 200
replication:
#副本集名字,3个分片分别为dev1 dev2 dev3
replSetName: dev1
oplogSizeMB: 10000
setParameter:
#事务锁超时最长时间(默认5毫秒)
maxTransactionLockRequestTimeoutMillis: 3000
security:
keyFile: /opt/mongodb/keyFile.key
authorization: enabled
创建mongo.cnf上面的数据目录
mkdir -p /opt/mongodb/data/db
创建mongo.cnf上面的密钥文件
yum install openssl
openssl rand -base64 756> /opt/mongodb/keyFile.key
# 最后修改其权限,权限不能太开放。
chmod 600 /opt/mongodb2/keyFile.key
启动dev1副本集
/opt/mongodb2/bin/mongod -f /opt/mongodb2/mongo.conf
启动成功。
新增配置文件:mongo2.conf
sharding:
clusterRole: shardsvr
processManagement:
fork: true
pidFilePath: /opt/mongodb/mongodb2.pid
net:
bindIp: 0.0.0.0
port: 27019
storage:
# 这个路径要是不存在,就要手动创建,不然会报错
dbPath: /opt/mongodb/data/db2
systemLog:
destination: file
path: /opt/mongodb/mongodb2.log
logAppend: true
operationProfiling:
mode: slowOp
slowOpThresholdMs: 200
replication:
#副本集名字
replSetName: dev2
oplogSizeMB: 10000
setParameter:
#事务锁超时最长时间(默认5毫秒)
maxTransactionLockRequestTimeoutMillis: 3000
security:
keyFile: /opt/mongodb/keyFile.key
authorization: enabled
主要是修改了db文件名、日志文件、端口号、副本集名称。然后去创建db2文件夹,文件不存在会报错
启动dev2副本集
/opt/mongodb2/bin/mongod -f /opt/mongodb2/mongo2.conf
新增配置文件:mongo3.conf
sharding:
clusterRole: shardsvr
processManagement:
fork: true
pidFilePath: /opt/mongodb/mongodb3.pid
net:
bindIp: 0.0.0.0
port: 27020
storage:
# 这个路径要是不存在,就要手动创建,不然会报错
dbPath: /opt/mongodb/data/db3
systemLog:
destination: file
# 这个路径要是不存在,就要手动创建,不然会报错
path: /opt/mongodb/mongodb3.log
logAppend: true
operationProfiling:
mode: slowOp
slowOpThresholdMs: 200
replication:
#副本集名字
replSetName: dev3
oplogSizeMB: 10000
setParameter:
#事务锁超时最长时间(默认5毫秒)
maxTransactionLockRequestTimeoutMillis: 3000
security:
keyFile: /opt/mongodb/keyFile.key
authorization: enabled
主要是修改了db文件名、端口号、副本集名称。然后去创建db3文件夹,文件不存在会报错
启动dev3副本集
/opt/mongodb2/bin/mongod -f /opt/mongodb/mongo3.conf
安装mongosh
下载:https://downloads.mongodb.com/compass/mongosh-2.1.3-linux-x64.tgz
cd /opt
tar -zxvf mongosh-2.1.3-linux-x64.tgz
chmod +x mongosh-2.1.3-linux-x64/bin/mongosh
cd mongosh-2.1.3-linux-x64/bin
sudo cp mongosh /usr/local/bin/
sudo cp mongosh_crypt_v1.so /usr/local/lib/
sudo ln -s $(pwd)/bin/* /usr/local/bin/
此时启动了三个副本集
现在在dev2.mongodb和dev3.mongodb重复上面的操作,确保服务都已经起来了。
登录副本集,添加初始化配置
配置副本集dev1
mongosh "mongodb://localhost:27018";
var rsconf = {
_id:'dev1',
members:
[
{
_id:1,
host:'dev1.mongodb.leve.com:27018'
},
{
_id:2,
host:'dev2.mongodb.leve.com:27018'
},
{
_id:3,
host:'dev3.mongodb.leve.com:27018'
}
]
}
rs.initiate(rsconf);
#上面这个命令只需要在dev1上允许一次就可以了,dev2和dev3就可以用rs.status();
配置副本集dev2
mongosh "mongodb://localhost:27019";
var rsconf = {
_id:'dev2',
members:
[
{
_id:1,
host:'dev171.mongodb.leve.com:27019'
},
{
_id:2,
host:'dev2.mongodb.leve.com:27019'
},
{
_id:3,
host:'dev3.mongodb.leve.com:27019'
}
]
}
rs.initiate(rsconf);
配置副本集dev3
mongosh "mongodb://localhost:27020";
var rsconf = {
_id:'dev3',
members:
[
{
_id:1,
host:'dev171.mongodb.leve.com:27020'
},
{
_id:2,
host:'dev2.mongodb.leve.com:27020'
},
{
_id:3,
host:'dev3.mongodb.leve.com:27020'
}
]
}
rs.initiate(rsconf);
最后用 rs.status(); 可以查看结果
搭建config复制集
在机器dev1上配置配置mongo-cfg.conf文件
systemLog:
destination: file
path: /opt/mongodb/mongo-cfg/logs/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /opt/mongo/mongo-cfg/data
directoryPerDB: true
wiredTiger:
engineConfig:
# 最大使用内存
cacheSizeGB: 1
directoryForIndexs: true
collectionConfig:
# 表压缩配置
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
bindIp: 0.0.0.0
port: 28018
replication:
oplogSizeMB: 2048
replSetName: dev-mongodb-config
sharding:
# 声明为配置节点
clusterRole: configsvr
processManagement:
fork: true
security:
keyFile: /opt/mongodb/keyFile.key
authorization: enabled
上面这个配置文件,三台机器都要有
创建上面配置文件描述的文件夹
mkdir -p /opt/mongodb/mongo-cfg/data
mkdir -p /opt/mongodb/mongo-cfg/logs
启动配置服务
/opt/mongodb/bin/mongod -f /opt/mongodb/mongo-cfg.conf
在另外两台机器上执行上面的步骤
初始化配置节点
mongosh "mongodb://localhost:28018"
# 初始化命令
rs.initiate(
{
_id : "dev-mongodb-config",
configsvr: true,
members: [
{ _id : 0, host : "dev1.mongodb.leve.com:28018" },
{ _id : 1, host : "dev2.mongodb.leve.com:28018" },
{ _id : 2, host : "dev3.mongodb.leve.com:28018" }
]
}
);
最后用 rs.status(); 可以查看结果
搭建mongos
配置mongos.conf文件
systemLog:
destination: file
path: /opt/mongodb2/mongos/logs/mongos.log
logAppend: true
net:
bindIp: 0.0.0.0
port: 29018
sharding:
# 指定配置节点
configDB: dev-mongodb-config/dev1.mongodb.leve.com:28018,dev2.mongodb.leve.com:28018,dev3.mongodb.leve.com:28018
processManagement:
fork: true
security:
keyFile: /opt/mongodb2/keyFile.key
创建上面配置文件描述的文件夹
mkdir -p /opt/mongodb/mongos/logs
启动route服务
/opt/mongodb/bin/mongos -config /opt/mongodb/mongos.conf
在另外两台机器上执行上面的步骤
配置数据库连接密码
创建管理员用户
# 登录节点
mongosh dev171.mongodb.leve.com:29018
# 登录之后先创建一个管理员账号
use admin;
db.createUser(
{
user: "leve",
pwd: "password",
roles: [ "root" ]
}
)
# 如果要修改用户,或者有新建的数据库
db.updateUser(
"leve",
{
roles: [
{ role: "root", db: "admin" }
]
}
)
配置分片
# 用管理员账户登录
mongosh -port 29018 -u root --authenticationDatabase admin
use admin;
db.runCommand(
{
addshard: "dev1/dev1.mongodb.leve.com:27018,dev2.mongodb.leve.com:27018,dev3.mongodb.leve.com:27018",
name: "dev1-shard"
}
);
db.runCommand(
{
addshard: "dev2/dev1.mongodb.leve.com:27019,dev2.mongodb.leve.com:27019,dev3.mongodb.leve.com:27019",
name: "dev2-shard"
}
);
db.runCommand(
{
addshard: "dev3/dev1.mongodb.leve.com:27020,dev2.mongodb.leve.com:27020,dev3.mongodb.leve.com:27020",
name: "dev3-shard"
}
);
查看分片状态
db.runCommand({listshards: 1})
完成!!