monodb
副本集集群
实现复制和自动故障转移的 MongoDB 服务器集群。
安装
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-5.0.9.tgz
tar zxf mongodb-linux-x86_64-rhel80-5.0.9.tgz
mv mongodb-linux-x86_64-rhel80-5.0.9 /usr/local/mongodb
useradd -M -s /sbin/nologin mongod
mkdir -p /data/mongodb /var/log/mongodb
chown mongod. /data/mongodb /var/log/mongodb
cat <<EOF > /etc/profile.d/mongodb.sh
PATH=\$PATH:/usr/local/mongodb/bin
EOF
source /etc/profile.d/mongodb.sh
#创建配置文件
cat <<EOF > /etc/mongod.conf
replSet=mongodb-cluster-test
port=27017
bind_ip_all=true
dbpath=/data/mongodb
logpath=/var/log/mongodb/mongod.log
logappend=true
fork=true
pidfilepath=/var/run/mongodb/mongod.pid
directoryperdb=true
wiredTigerCollectionBlockCompressor=zstd
EOF
#创建systemd配置文件
cat <<EOF > /usr/lib/systemd/system/mongod.service
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target
[Service]
User=mongod
Group=mongod
RuntimeDirectory=mongodb
Environment="OPTIONS=-f /etc/mongod.conf"
ExecStart=/usr/local/mongodb/bin/mongod \$OPTIONS
PIDFile=/var/run/mongodb/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu timev
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start mongod
配置集群
#mongo cli
#初始化
rs.initiate()
#添加节点(执行的当前节点将为主节点,添加的为副本节点)
rs.add("192.168.32.15:27017")
#添加仲裁节点(不存数据只用来选举用,当资源不够时可以添加一个仲裁节点)
rs.addArb("192.168.32.16:27017")
分片集群
分片集群是分布在许多分片(服务器)上的数据集的集合,以实现水平可扩展性和更好的读写操作性能。
生产配置:
安装
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-5.0.9.tgz
tar zxf mongodb-linux-x86_64-rhel80-5.0.9.tgz
mv mongodb-linux-x86_64-rhel80-5.0.9 /usr/local/mongodb
useradd -M -s /sbin/nologin mongod
mkdir -p /data/mongodb /var/log/mongodb
chown mongod. /data/mongodb /var/log/mongodb
cat <<EOF > /etc/profile.d/mongodb.sh
PATH=\$PATH:/usr/local/mongodb/bin
EOF
source /etc/profile.d/mongodb.sh
#创建systemd配置文件
cat <<EOF > /usr/lib/systemd/system/mongod.service
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target
[Service]
User=mongod
Group=mongod
RuntimeDirectory=mongodb
Environment="OPTIONS=-f /etc/mongod.conf"
ExecStart=/usr/local/mongodb/bin/mongod \$OPTIONS
PIDFile=/var/run/mongodb/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu timev
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start mongod
1.创建配置服务器副本集
安装及生成配置文件需要在每个配置服务器节点执行
生成配置文件:
cat <<EOF > /etc/mongod.conf
replSet=rs-config-test
configsvr=true
port=27019
bind_ip_all=true
dbpath=/data/mongodb
logpath=/var/log/mongod.log
logappend=true
fork=true
pidfilepath=/var/run/mongodb/mongod.pid
directoryperdb=true
wiredTigerCollectionBlockCompressor=zstd
EOF
初始化副本集:
# 连接其中一个节点,_id与配置文件replSet对应,节点使用主机名加端口(不要使用IP)
rs.initiate(
{
_id : "rs-config-test",
configsvr: true,
members: [
{ _id : 0, host : "m1:27019" },
{ _id : 1, host : "m2:27019" },
{ _id : 2, host : "m3:27019" }
]
}
)
2.创建分片服务器副本集
安装及生成配置文件需要在每个分片服务器节点执行
生成配置文件:
cat <<EOF > /etc/mongod.conf
replSet=rs-shard-test-1
shardsvr=true
port=27018
bind_ip_all=true
dbpath=/data/mongodb
logpath=/var/log/mongod.log
logappend=true
fork=true
pidfilepath=/var/run/mongodb/mongod.pid
directoryperdb=true
wiredTigerCollectionBlockCompressor=zstd
EOF
初始化副本集:
# 连接其中一个节点,_id与配置文件replSet对应,节点使用主机名加端口(不要使用IP)
rs.initiate(
{
_id : "rs-shard-test-1",
members: [
{ _id : 0, host : "m1:27018" },
{ _id : 1, host : "m2:27018" },
{ _id : 2, host : "m3:27018" }
]
}
)
3.部署查询路由器mongos
客户端连接查询路由器,为高可用负载均衡应部署多个
生成配置文件:
cat <<EOF > /etc/mongod.conf
port=27017
bind_ip_all=true
logpath=/var/log/mongod.log
logappend=true
fork=true
pidfilepath=/var/run/mongodb/mongod.pid
EOF
添加分片:
sh.addShard("rs-shard-test-1/m1:27018,m2:27018,m3:27018")
sh.addShard("rs-shard-test-2/m4:27018,m5:27018,m6:27018")
#为数据库启用分片
sh.enableSharding("<database>")
#集合启用哈希分片
sh.shardCollection("<database>.<collection>", {_id: "hashed"})
#集合启用默认范围分片(会连续写满一个chunk(64MB),才会自动在其他分片上创建新chunk)
sh.shardCollection("<database>.<collection>", {_id: 1})
#查看指定集合分片的分布情况
db.<collection>.getShardDistribution()
#查看所有分片信息
db.printShardingStatus()
##手动分片
#设置分片标签名称
sh.addShardToZone("<shards._id>", "DC1")
sh.addShardToZone("<shards._id>", "DC2")
#指定片键sid值范围1-100存放在标签为DC1的分片中
sh.updateZoneKeyRange(
"tt.c1",
{sid: 1},
{sid: 100},
"DC1"
)
##手动拆分chunk
#对指定位置进行split,新的chunk的边界从指定的片键值开始
#后续的写入会根据片键所在的范围写入相近的chunk
#注意:已有数据情况下会拆分<指定片键值的数据到新的chunk里面,原chunk保留原本所有数据
sh.splitAt("tt.c1", {sid: 70})
#在有指定片键值的chunk创建两个大致相等的chunk
sh.splitFind("tt.c1", {sid: 70})
测试数据
use testdb
sh.enableSharding("testdb")
sh.shardCollection("testdb.book", {id: "hashed"})
var cnt = 0;
for(var i=0; i<1000; i++){
var dl = [];
for(var j=0; j<100; j++){
dl.push({
"id" : "BBK-" + i + "-" + j,
"type" : "Revision",
"version" : "IricSoneVB0001",
"title" : "Jackson's Life",
"subCount" : 10,
"location" : "China CN Shenzhen Futian District",
"author" : {
"name" : 50,
"email" : "RichardFoo@yahoo.com",
"gender" : "female"
},
"createTime" : new Date()
});
}
cnt += dl.length;
db.book.insertMany(dl);
print("insert ", cnt);
}
均衡器 blancer
均衡器主要根据集合在各个shard上chunk的数量来决定的,相差超过一定阈值(和chunk总数量相关)就会触发chunk迁移。
#自定义均衡器工作时间
use config
sh.setBalancerState( true )
db.settings.update(
{ _id : "balancer" },
{ $set : { activeWindow : { start : "3:00", stop : "5:00" } } },
{ upsert: true }
)
#查看是否开启
sh.getBalancerState()
#查看当前配置
sh.getBalancerWindow()
sh.status()
#开启关 闭
sh.stopBalancer()
sh.startBalancer()
#关闭开启指定集合上的
sh.disableBalancing("tt.c1");
sh.enableBalancing("tt.c1");
常用操作
mongo --shell --eval "db.serverStatus()"
#mongo cli下操作
#查看引擎配置
db.serverStatus().wiredTiger
#查看副本集集群状态
rs.status()
#查看分片集群状态
sh.status()
#查看所有分片信息
use admin
db.runCommand({ listshards : 1})
#查看开启分片的数据库
use config
db.databases.find()
#查看集群配置
rs.conf()
#当前op
db.currentOp()
#删除节点
rs.remove("172.20.6.12:27017")
#关闭节点
use admin
db.shutdownServer()