MongoDB Setup
验证:未记录
- 适用版本
- 未记录
- 验证范围
- 尚未登记验证记录。
本文缩写(英文全称与中文含义)
| 缩写 | 英文全称/名称 | 中文含义 |
|---|---|---|
| HA | High Availability | 高可用 |
| K8S | Kubernetes | 容器编排平台;8 表示 K 与 s 之间的八个字母 |
| OS | Operating System | 操作系统 |
| OSS | Open-Source Software | 开源软件 |
| RPO | Recovery Point Objective | 恢复点目标,可接受的数据丢失时间范围 |
| RTO | Recovery Time Objective | 恢复时间目标,可接受的恢复耗时 |
| TLS | Transport Layer Security | 传输层安全协议 |
| VM | Virtual Machine | 虚拟机 |
Links#
https://www.mongodb.com/docs/manual/installation/
https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/
https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-red-hat/
https://hub.docker.com/_/mongo
https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set/
https://www.mongodb.com/docs/manual/tutorial/enforce-keyfile-access-control-in-existing-replica-set/
https://github.com/mongodb/helm-charts
https://github.com/percona/percona-helm-charts
https://github.com/bitnami/charts/tree/main/bitnami/mongodb1. Linux VM With systemd#
install#
example:
Ubuntu 24.04
MongoDB Community 8.0
systemd service: mongod
note:
production should follow the exact official repository for your OS version
do not install random mongodb packages from old distro reposudo apt-get update
sudo apt-get install -y gnupg curl
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
sudo apt-get update
sudo apt-get install -y mongodb-orgdirectories#
sudo mkdir -p /var/lib/mongo /var/log/mongodb /etc/mongodb/tls
sudo chown -R mongodb:mongodb /var/lib/mongo /var/log/mongodb
sudo chmod 0755 /var/lib/mongo /var/log/mongodbconfig#
# /etc/mongod.conf
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
systemLog:
destination: file
path: /var/log/mongodb/mongod.log
logAppend: true
net:
port: 27017
bindIp: 127.0.0.1,10.0.1.10
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
authorization: enabled
keyFile: /etc/mongodb/keyfile
replication:
replSetName: rs0
operationProfiling:
slowOpThresholdMs: 100keyfile#
openssl rand -base64 756 | sudo tee /etc/mongodb/keyfile > /dev/null
sudo chown mongodb:mongodb /etc/mongodb/keyfile
sudo chmod 0400 /etc/mongodb/keyfilekeyfile notes:
same content on every replica set member
owner should be mongodb
permission should be strict
rotate with a planned maintenance proceduresystemd#
sudo systemctl daemon-reload
sudo systemctl enable mongod
sudo systemctl start mongod
sudo systemctl status mongodjournalctl -u mongod -f
tail -f /var/log/mongodb/mongod.loginitialize replica set#
mongosh
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "mongo-1.example.internal:27017" },
{ _id: 1, host: "mongo-2.example.internal:27017" },
{ _id: 2, host: "mongo-3.example.internal:27017" }
]
})create admin user#
use admin
db.createUser({
user: "root_admin",
pwd: passwordPrompt(),
roles: [
{ role: "root", db: "admin" }
]
})create application user#
use admin
db.createUser({
user: "order_app",
pwd: passwordPrompt(),
roles: [
{ role: "readWrite", db: "order" }
]
})firewall#
allow:
app subnet -> mongo nodes tcp/27017
mongo nodes -> mongo nodes tcp/27017
bastion/admin subnet -> mongo nodes tcp/27017, if required
deny:
public internet -> tcp/270172. Docker#
standalone for local dev#
docker network create mongo-net
docker run -d --name mongo --network mongo-net -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=change-me -v mongo-data:/data/db mongo:8mongosh "mongodb://root:change-me@localhost:27017/admin"docker compose replica set#
services:
mongo-1:
image: mongo:8
command: ["mongod", "--replSet", "rs0", "--bind_ip_all", "--keyFile", "/etc/mongo/keyfile"]
ports:
- "27017:27017"
volumes:
- mongo-1-data:/data/db
- ./keyfile:/etc/mongo/keyfile:ro
mongo-2:
image: mongo:8
command: ["mongod", "--replSet", "rs0", "--bind_ip_all", "--keyFile", "/etc/mongo/keyfile"]
ports:
- "27018:27017"
volumes:
- mongo-2-data:/data/db
- ./keyfile:/etc/mongo/keyfile:ro
mongo-3:
image: mongo:8
command: ["mongod", "--replSet", "rs0", "--bind_ip_all", "--keyFile", "/etc/mongo/keyfile"]
ports:
- "27019:27017"
volumes:
- mongo-3-data:/data/db
- ./keyfile:/etc/mongo/keyfile:ro
volumes:
mongo-1-data:
mongo-2-data:
mongo-3-data:openssl rand -base64 756 > keyfile
chmod 0400 keyfile
docker compose up -dmongosh "mongodb://localhost:27017"
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "mongo-1:27017" },
{ _id: 1, host: "mongo-2:27017" },
{ _id: 2, host: "mongo-3:27017" }
]
})docker notes:
good for local dev / lab
production needs persistent volume, backup, monitoring, TLS, resource limits
container memory limit should match WiredTiger cache configuration3. K8S With Helm#
option#
MongoDB official helm-charts repo:
includes community-operator chart
community charts are not officially supported by MongoDB
good for lab / learning
Percona Operator for MongoDB:
open-source operator
production-oriented HA / backup / TLS / users
recommended for self-managed OSS production if your team accepts Percona stack
Bitnami MongoDB chart:
simple Helm chart
supports standalone / replicaset
good for quick setup
production needs careful values reviewpercona operator#
helm repo add percona https://percona.github.io/percona-helm-charts/
helm repo update
kubectl create namespace mongodb
helm install psmdb-operator percona/psmdb-operator --namespace mongodbhelm install psmdb-db percona/psmdb-db --namespace mongodb --set finalizers.deletePvc=false --set replsets.rs0.size=3 --set replsets.rs0.volumeSpec.persistentVolumeClaim.resources.requests.storage=100Gicheck:
kubectl -n mongodb get pods
kubectl -n mongodb get psmdb
kubectl -n mongodb get pvcbitnami replicaset#
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
kubectl create namespace mongodb
helm install mongodb bitnami/mongodb --namespace mongodb --set architecture=replicaset --set replicaCount=3 --set auth.enabled=true --set auth.rootPassword='change-me' --set auth.replicaSetKey='change-me-replica-key' --set persistence.enabled=true --set persistence.size=100Giproduction values should include:
resource requests / limits
storageClass
pod anti-affinity
TLS
backup
monitoring exporter
network policy
secret managementnetwork policy#
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: mongodb-allow-app
namespace: mongodb
spec:
podSelector:
matchLabels:
app.kubernetes.io/name: mongodb
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
name: app
ports:
- protocol: TCP
port: 270174. High Availability#
replica set#
recommended:
3 data-bearing nodes across different AZs
odd number of voting members
majority writeConcern for critical write
app connection string includes all nodes and replicaSet name
example:
mongo-1: primary or secondary
mongo-2: primary or secondary
mongo-3: primary or secondary
connection string:
mongodb://order_app:<password>@mongo-1:27017,mongo-2:27017,mongo-3:27017/order?replicaSet=rs0&authSource=adminReplica set 部署不等于客户端自动正确故障切换;连接串、driver retry 设置和真实切换后的读写都要验证。
election:
primary unavailable -> secondaries elect new primary
writes fail during short election window
application driver must retry retryable writes
tune timeout and retry policyarbiter#
arbiter:
votes in election
does not store data
cannot become primary
use only when:
cost is constrained
two data-bearing nodes are unavoidable
avoid when:
you need strong durability across AZs
writeConcern majority should represent data-bearing majorityhidden / delayed secondary#
hidden secondary:
not visible to application read preference
useful for backup / analytics / reporting
delayed secondary:
intentionally lags
can help recover from accidental delete / bad write
not a replacement for real backupsharded cluster HA#
sharded cluster components:
shard replica sets
config server replica set
mongos routers
HA requirements:
each shard is a replica set
config servers run as replica set
multiple mongos behind app / service discovery
distribute pods / VMs across AZs
use sharding when:
one replica set cannot handle data size or throughput
shard key has been carefully testedbackup and restore#
HA is not backup:
replica set protects node failure
backup protects data corruption / accidental delete / ransomware / bad deploy
must have:
scheduled backup
tested restore
offsite copy
documented RPO / RTO5. Operations#
rolling restart#
order:
restart secondary first
wait until healthy and caught up
step down primary
restart old primary
verify new primary and replication lagrs.status()
rs.stepDown()upgrade#
upgrade checklist:
read official release notes
check driver compatibility
backup before upgrade
upgrade secondaries first
step down primary
upgrade old primary
only change featureCompatibilityVersion after rollback windowmaintenance commands#
db.adminCommand({ ping: 1 })
rs.status()
rs.printReplicationInfo()
rs.printSecondaryReplicationInfo()
db.serverStatus()
db.currentOp()完整生产实操#
部署、权限、故障与独立恢复提供本模块选定路径的分步实现和验收,补充本页安装参考。