- 官方
MongoDB 下载:https://www.mongodb.com/download-center/community/releases/archive
MongoDB 词汇表:https://www.mongodb.com/docs/manual/reference/glossary
MongoDB 副本集:https://www.mongodb.com/docs/manual/replication/
MongoDB 分片:https://www.mongodb.com/docs/manual/sharding/
MongoDB 存储 - 常见问题:https://www.mongodb.com/docs/manual/faq/storage/
MongoDB Database Tools:https://www.mongodb.com/docs/database-tools/
mongostat:https://www.mongodb.com/docs/database-tools/mongostat/
性能最佳实践:https://www.mongodb.com/blog/post/performance-best-practices-hardware-and-os-configuration
- 技术分享
千亿级数据迁移mongodb成本节省及性能优化实践:https://zhuanlan.zhihu.com/p/382398295
MongoDB 备份与恢复:https://www.runoob.com/mongodb/mongodb-mongodump-mongorestore.html
CentOS 基础操作命令:https://blog.csdn.net/u011424614/article/details/94555916
对换home分区与root分区的存储空间:https://blog.csdn.net/u011424614/article/details/125853913
CentOS 分区扩容:https://blog.csdn.net/u011424614/article/details/113275862
Dell R740安装CentOS:https://blog.csdn.net/u011424614/article/details/113306808
oplog
同步数据(1)一主两从(PSS)
三个数据存储节点
如果主节点不可用,则副本集选择某个从节点作为主节点,并继续正常操作。旧的主节点在可用时,可重新加入到副本集
(2)一主一从一仲裁(PSA)
config server 副本集(3台机器) 、2个 shard(每个分片3台机器=6台机器)、2 个 mongos (2台机器)
资源占用情况
角色 | 机器名 | IP | 端口 | 内存 (GB) | CPU (核) | 硬盘 |
---|---|---|---|---|---|---|
config server | mgd-config-01 | 192.168.2.11 | 27018 | 8 | 8 | 50 G |
config server | mgd-config-02 | 192.168.2.12 | 27018 | 8 | 8 | 50 G |
config server | mgd-config-03 | 192.168.2.13 | 27018 | 8 | 8 | 50 G |
shard【shard1】 | mgd-shard-01 | 192.168.2.14 | 27019 | 64 | 16 | 4 T |
shard【shard1】 | mgd-shard-02 | 192.168.2.15 | 27019 | 64 | 16 | 4 T |
shard【shard1】 | mgd-shard-03 | 192.168.2.16 | 27019 | 64 | 16 | 4 T |
shard【shard2】 | mgd-shard-04 | 192.168.2.17 | 27020 | 64 | 16 | 4 T |
shard【shard2】 | mgd-shard-05 | 192.168.2.18 | 27020 | 64 | 16 | 4 T |
shard【shard2】 | mgd-shard-06 | 192.168.2.19 | 27020 | 64 | 16 | 4 T |
mongos | mgd-mongos-01 | 192.168.2.20 | 27017 | 8 | 8 | 20 G |
mongos | mgd-mongos-02 | 192.168.2.21 | 27017 | 8 | 8 | 20 G |
为了获取最佳性能,每个机器应该只运行一个 MongoDB 进程
通过使用虚拟化或容器技术进行适当的大小调整和资源分配,多个 MongoDB 进程可以安全地在单个物理服务器上运行,而无需争抢资源
用户在同一主机上部署多个 MongoDB 进程。在这种情况下,您必须进行多项配置更改以确保每个进程都有足够的资源
grep avx /proc/cpuinfo
如果机器 IP 变化后,更新 hosts 映射文件即可;MongoDB 配置文件使用机器名进行绑定
#-- mgd-config-01
hostnamectl set-hostname mgd-config-01 --static
hostnamectl set-hostname mgd-config-01 --transient
#-- mgd-config-02
hostnamectl set-hostname mgd-config-02 --static
hostnamectl set-hostname mgd-config-02 --transient
#-- mgd-config-03
hostnamectl set-hostname mgd-config-03 --static
hostnamectl set-hostname mgd-config-03 --transient
#-- mgd-shard-01
hostnamectl set-hostname mgd-shard-01 --static
hostnamectl set-hostname mgd-shard-01 --transient
#-- mgd-shard-02
hostnamectl set-hostname mgd-shard-02 --static
hostnamectl set-hostname mgd-shard-02 --transient
#-- mgd-shard-03
hostnamectl set-hostname mgd-shard-03 --static
hostnamectl set-hostname mgd-shard-03 --transient
#-- mgd-shard-04
hostnamectl set-hostname mgd-shard-04 --static
hostnamectl set-hostname mgd-shard-04 --transient
#-- mgd-shard-05
hostnamectl set-hostname mgd-shard-05 --static
hostnamectl set-hostname mgd-shard-05 --transient
#-- mgd-shard-06
hostnamectl set-hostname mgd-shard-06 --static
hostnamectl set-hostname mgd-shard-06 --transient
#-- mgd-mongos-01
hostnamectl set-hostname mgd-mongos-01 --static
hostnamectl set-hostname mgd-mongos-01 --transient
#-- mgd-mongos-02
hostnamectl set-hostname mgd-mongos-02 --static
hostnamectl set-hostname mgd-mongos-02 --transient
hostnamectl status
cat > /etc/hosts <<EOF
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.11 mgd-config-01
192.168.2.12 mgd-config-02
192.168.2.13 mgd-config-03
192.168.2.14 mgd-shard-01
192.168.2.15 mgd-shard-02
192.168.2.16 mgd-shard-03
192.168.2.17 mgd-shard-04
192.168.2.18 mgd-shard-05
192.168.2.19 mgd-shard-06
192.168.2.20 mgd-mongos-01
192.168.2.21 mgd-mongos-02
EOF
#-- 禁用防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
#-- 防火墙状态
systemctl status firewalld.service
#-- (备用-可忽略)启用防火墙
systemctl enable firewalld.service
systemctl start firewalld.service
mkdir -p /data/mongodb/cluster/config/{data,logs}
mkdir -p /data/mongodb/cluster/shard1/{data,logs}
mkdir -p /data/mongodb/cluster/shard2/{data,logs}
mkdir -p /data/mongodb/cluster/mongos/{data,logs}
以下指令,在全部机器上执行
请先在官网 下载 MongoDB ,然后,上传到服务器;或者使用 wget 进行下载
#-- 安装依赖包
yum install -y libcurl openssl xz-libs
#-- 解压
tar -zxvf mongodb-linux-x86_64-rhel70-5.0.9.tgz -C /data/mongodb --strip=1
echo "PATH=$PATH:/data/mongodb/bin" > /etc/profile.d/mongodb.sh
source /etc/profile.d/mongodb.sh
mongo --version
groupadd mongod
useradd -g mongod mongod
chown -R mongod:mongod /data/mongodb
cat > /data/mongodb/cluster/config/mongod.conf <
mongod -f /data/mongodb/cluster/config/mongod.conf
cat > /data/mongodb/cluster/config/mongod.conf <
mongod -f /data/mongodb/cluster/config/mongod.conf
cat > /data/mongodb/cluster/config/mongod.conf <
mongod -f /data/mongodb/cluster/config/mongod.conf
mongo 192.168.2.11 --port 27018
rs.initiate(
{
_id: "config",
configsvr: true,
members: [
{ _id : 0, host : "mgd-config-01:27018" },
{ _id : 1, host : "mgd-config-02:27018" },
{ _id : 2, host : "mgd-config-03:27018" }
]
}
)
rs.status()
cat > /data/mongodb/cluster/shard1/mongod.conf <
mongod -f /data/mongodb/cluster/shard1/mongod.conf
cat > /data/mongodb/cluster/shard1/mongod.conf <
mongod -f /data/mongodb/cluster/shard1/mongod.conf
cat > /data/mongodb/cluster/shard1/mongod.conf <
mongod -f /data/mongodb/cluster/shard1/mongod.conf
mongo 192.168.2.14 --port 27019
rs.initiate(
{
_id: "shard1",
members: [
{ _id : 0, host : "mgd-shard-01:27019" },
{ _id : 1, host : "mgd-shard-02:27019" },
{ _id : 2, host : "mgd-shard-03:27019" }
]
}
)
rs.initiate(
{
_id: "shard1",
members: [
{ _id : 0, host : "mgd-shard-01:27019" },
{ _id : 1, host : "mgd-shard-02:27019" },
{ _id : 2, host : "mgd-shard-03:27019","arbiterOnly":true }
]
}
)
rs.status()
cat > /data/mongodb/cluster/shard2/mongod.conf <
mongod -f /data/mongodb/cluster/shard2/mongod.conf
cat > /data/mongodb/cluster/shard2/mongod.conf <
mongod -f /data/mongodb/cluster/shard2/mongod.conf
cat > /data/mongodb/cluster/shard2/mongod.conf <
mongod -f /data/mongodb/cluster/shard2/mongod.conf
mongo 192.168.2.17 --port 27020
rs.initiate(
{
_id: "shard2",
members: [
{ _id : 0, host : "mgd-shard-04:27020" },
{ _id : 1, host : "mgd-shard-05:27020" },
{ _id : 2, host : "mgd-shard-06:27020" }
]
}
)
rs.initiate(
{
_id: "shard2",
members: [
{ _id : 0, host : "mgd-shard-04:27020" },
{ _id : 1, host : "mgd-shard-05:27020" },
{ _id : 2, host : "mgd-shard-06:27020","arbiterOnly":true }
]
}
)
rs.status()
cat > /data/mongodb/cluster/mongos/mongod.conf <
mongos -f /data/mongodb/cluster/mongos/mongod.conf
cat > /data/mongodb/cluster/mongos/mongod.conf <
mongos -f /data/mongodb/cluster/mongos/mongod.conf
mongo 192.168.2.20 --port 27017
sh.addShard( "shard1/mgd-shard-01:27019,mgd-shard-02:27019,mgd-shard-03:27019")
sh.addShard( "shard2/mgd-shard-04:27020,mgd-shard-05:27020,mgd-shard-06:27020")
sh.status()
mongo 192.168.2.20 --port 27017
use admin
sh.enableSharding("testdb")
sh.shardCollection("testdb.order", {"_id": "hashed" })
use testdb
for (i = 1; i <= 10000; i=i+1){
db.order.insert({'price': 1})
}
use testdb
db.order.count()
db.order.stats()
软件:MongoDB Database Tools
部署环境:Windows 10
192.168.2.11 mgd-config-01
192.168.2.12 mgd-config-02
192.168.2.13 mgd-config-03
192.168.2.14 mgd-shard-01
192.168.2.15 mgd-shard-02
192.168.2.16 mgd-shard-03
192.168.2.17 mgd-shard-04
192.168.2.18 mgd-shard-05
192.168.2.19 mgd-shard-06
192.168.2.20 mgd-mongos-01
192.168.2.21 mgd-mongos-02
mongostat:https://www.mongodb.com/docs/database-tools/mongostat/
mongostat -h 192.168.2.21:27017 --discover
参数说明:https://www.mongodb.com/docs/database-tools/mongostat/#fields
参数 | 说明 |
---|---|
insert | 每秒插入数据库的对象数 |
query | 每秒查询操作数 |
update | 每秒更新操作的数量 |
delete | 每秒删除操作的次数 |
getmore | 每秒游标批处理操作的数量 |
command | 每秒的命令数 |
flushes | 每个轮询间隔之间触发的 WiredTiger 检查点的数量 |
dirty | WiredTiger 缓存中包含脏字节的百分比 |
used | 正在使用的 WiredTiger 缓存的百分比 |
vsize | 进程在最后一次使用时使用的虚拟内存量(以 MB 为单位) |
res | 进程在最后一次使用时使用的常驻内存量(以 MB 为单位) |
locked | 全局写锁的时间百分比 |
conn | 打开的连接总数 |
软件:YCSB
部署环境:mgd-mongos-02 机器
使用:https://github.com/brianfrankcooper/YCSB/wiki/Running-a-Workload
参数说明:https://github.com/brianfrankcooper/YCSB/wiki/Core-Properties
curl -O --location https://github.com/brianfrankcooper/YCSB/releases/download/0.17.0/ycsb-0.17.0.tar.gz
mkdir /opt/ycsb
tar -zxvf ycsb-0.17.0.tar.gz -C /opt/ycsb --strip=1
use admin
sh.enableSharding("ycsb")
sh.shardCollection("ycsb.usertable", {"_id": "hashed" })
use ycsb
db.usertable.stats()
recordcount:插入文档数量;threadcount:运行线程数;
fieldcount:字段数量;outputLoad.txt:输出结果(可选)
cd /opt/ycsb
./bin/ycsb load mongodb -s -P workloads/workloada -p mongodb.url=mongodb://192.168.2.21:27017/ycsb?w=0 -p threadcount=1 -p fieldcount=20 -p recordcount=100000 > outputLoad.txt
workloads/workloada:加载场景配置文件
例如:workloada 是 50% 读操作和 50% 更新操作的场景
其它场景文件,可以结合参数说明进行解读
cd /opt/ycsb
./bin/ycsb run mongodb -s -P workloads/workloada -p mongodb.url=mongodb://192.168.2.21:27017/ycsb?w=0 -p threadcount=1 -p operationcount=1000 > outputRun.txt
[OVERALL],RunTime(ms), 10110
[OVERALL],Throughput(ops/sec), 98.91196834817013
[UPDATE], Operations, 491
[UPDATE], AverageLatency(ms), 0.054989816700611
[UPDATE], MinLatency(ms), 0
[UPDATE], MaxLatency(ms), 1
[UPDATE], 95thPercentileLatency(ms), 1
[UPDATE], 99thPercentileLatency(ms), 1
[UPDATE], Return=0, 491
[UPDATE], 0, 464
[UPDATE], 1, 27
[UPDATE], 2, 0
[UPDATE], 3, 0
[UPDATE], 4, 0
...
分片键:Shard Keys — MongoDB Manual
分片键可实现在集群中的各个分片之间平均分配数据块
# 建库时,指定分片键
use admin
sh.enableSharding("testdb")
sh.shardCollection("testdb.user", {"_id": "hashed" })
# 建库时,指定分片键
use admin
sh.enableSharding("testdb")
sh.shardCollection("testdb.user", {"userid":1, "createTime":1})
索引:Indexes — MongoDB Manual
切记提前创建索引,因为当单个集合数据量变大时,创建索引会非常耗时
# userid 升序
db.user.createIndex({"userid":1})
# name 升序,age 降序
db.user.createIndex({ "name": 1, "age": -1 })
# 注意:
# 过滤条件为 name,或包含 name 的查询会使用索引(索引的第一个字段),以下都会用到索引
db.user.find({ name: '张三' }).explain()
db.user.find({ name: "张三", level: 10 }).explain()
db.user.find({ name: "张三", age: 23 }).explain()
# 查询条件为 age 时,不会使用上边创建的索引,而是使用的全表扫描
db.user.find({ age: 23 }).explain()
db.user.createIndex({"location":"2dsphere"})
# 切换到 admin 库
use admin
sh.status()
# 建库
sh.enableSharding("testdb")
# 创建集合和分片键
sh.shardCollection("testdb.user", {"userid":1, "createTime":1})
# 切换到 testdb 库
use testdb
# 创建集合索引
db.user.createIndex({"userid":1, "createTime":1})
db.user.createIndex({"userid":1})
db.user.createIndex({"name":1})
db.user.createIndex({"age":1})
db.user.createIndex({"location":"2dsphere"})
分片集群平衡器:Sharded Cluster Balancer — MongoDB Manual
MongoDB 平衡器是一个后台进程,用于监控 每个分片集合的数据。
指令 | 作用 |
---|---|
sh.getBalancerState() | 检查平衡器是否启用 |
sh.isBalancerRunning() | 检查平衡器是否在运行 |
sh.stopBalancer() | 禁用平衡器 |
sh.startBalancer() 、sh.setBalancerState(true) | 启用平衡器 |
sh.disableBalancing(“databaseName.collectionName”) | 指定集合禁用平衡器 |
sh.enableBalancing(“databaseName.collectionName”) | 指定集合启用平衡器 |
# 连接其中一台 mongos
mongo 192.168.2.20 --port 27017
# 切换到 config 库
use config
# 启用平衡器
sh.setBalancerState(true)
# 设置启动时间段
db.settings.update(
{ _id: "balancer" },
{ $set: { activeWindow : { start : "00:00", stop : "06:00" } } },
{ upsert: true }
)
# 连接其中一台 mongos
mongo 192.168.2.20 --port 27017
# 切换到 config 库
use config
# 禁用平衡器
sh.stopBalancer()
报错:Aborted (core dumped)
# 先删除 datapath 下的全部 .lock 文件,在执行修复指令
mongod --repair --dbpath /data/mongodb/cluster/shard1/data
scp -r /data/mongodb/cluster/shard1/data root@mgd-shard-03:/data/mongodb/cluster/shard1
参数 | 默认值 | 含义 |
---|---|---|
eviction_target | 80 | 当 cache used 超过 eviction_target ,后台evict线程开始淘汰 CLEAN PAGE |
eviction_trigger | 95 | 当 cache used 超过 eviction_trigger ,用户线程也开始淘汰 CLEAN PAGE |
eviction_dirty_target | 5 | 当 cache dirty 超过 eviction_dirty_target ,后台evict线程开始淘汰 DIRTY PAGE |
eviction_dirty_trigger | 20 | 当 cache dirty 超过 eviction_dirty_trigger , 用户线程也开始淘汰 DIRTY PAGE |
evict.threads_min | 4 | |
evict.threads_max | 4 |
参数 | 默认值 | 含义 |
---|---|---|
eviction_target | 75 | 当 cache used 超过 eviction_target ,后台evict线程开始淘汰 CLEAN PAGE |
eviction_trigger | 90 | 当 cache used 超过 eviction_trigger ,用户线程也开始淘汰 CLEAN PAGE |
eviction_dirty_target | 3 | 当 cache dirty 超过 eviction_dirty_target ,后台evict线程开始淘汰 DIRTY PAGE |
eviction_dirty_trigger | 15 | 当 cache dirty 超过 eviction_dirty_trigger , 用户线程也开始淘汰 DIRTY PAGE |
evict.threads_min | 4 | |
evict.threads_max | 16 |
# 如果机器名无法连接,需要把机器名换成 IP
mongo 192.168.2.14 --port 27019
mongo 192.168.2.15 --port 27019
mongo 192.168.2.16 --port 27019
mongo 192.168.2.17 --port 27020
mongo 192.168.2.18 --port 27020
mongo 192.168.2.19 --port 27020
db.adminCommand({setParameter: 1, wiredTigerEngineRuntimeConfig: "eviction_target=75,eviction_trigger=90,eviction_dirty_target=3,eviction_dirty_trigger=15,eviction=(threads_min=4,threads_max=16),checkpoint=(wait=30)"})
db.adminCommand({"setParameter": 1, "wiredTigerEngineRuntimeConfig": "cache_size=8G"})
cd /var/log
grep "mongod" messages
Jul 28 01:40:20 mgd-shard-03 abrt-hook-ccpp: Process 3368 (mongod) of user 0 killed by SIGABRT - ignoring (abrtd is not running)
Aug 30 17:10:48 mgd-rc-04 abrt-hook-ccpp: Process 23380 (mongod) of user 0 killed by SIGABRT - dumping core
Aug 30 17:11:56 mgd-rc-04 abrt-server: Executable '/data/mongodb/bin/mongod' doesn't belong to any package and ProcessUnpackaged is set to 'no'
sync && sync && sync
echo 3 > /proc/sys/vm/drop_caches
swapoff -a
swapon -a
角色启动顺序:config --> shard --> mongos
查看进程信息
ps -ef | grep mongod | grep -v grep
top