Mongodb shard
在这之前,首先要做一些准备工作,例如:
-
mongod/s 的配置文件
-
mongod/s 的systemd文件
-
运行所需要的数据日志目录
Config.file ¶
先要生成cluster中的keyfile,使用
openssl rand -base64 756 > /path;
chmod 400 /path;
chown mongod:mongod /path
# CA私钥 && CA证书
openssl genrsa -out ca.key 4096
openssl req -new -sha256 -key ca.key -out ca.csr -subj "\
/C=CN\
/ST=JIANGSU\
/L=WUXI\
/O=BlackRabbit Inc.\
/OU=BRIO\
/CN=BRIO CA CERTIFICATE\
"
openssl x509 -extensions v3_ca -signkey ca.key -in ca.csr -req -days 365 -out ca.pem
# Server 证书
openssl genrsa -out server.key 4096
openssl req -new -sha256 -key server.key \
-subj "/C=CN/ST=JIANGSU/O=blackrabbit/CN=*.changsen.io" \
-reqexts SAN \
-config <(cat /etc/ssl/openssl.cnf \
<(printf "\n[SAN]\nsubjectAltName=DNS:*.dev.changsen.io,DNS:*.docs.changsen.io")) \
-out server.csr
openssl req -in server.csr -noout -text
openssl x509 -req \
-extfile <(printf "\n[SAN]\nsubjectAltName=DNS:*.dev.changsen.io,DNS:*.docs.changsen.io") \
-in server.csr \
-CA ca.pem \
-CAkey ca.key \
-CAcreateserial \
-out server.crt \
-days 365 -sha256
openssl x509 -text -noout -in server.crt
cat server.crt server.key > server.pem
mongod.xx.conf
systemLog:
quiet: true
destination: file
path: /data/mongodb/config(shardx)(mongos)/log/config(shardx)(mongos).log
logAppend: true
timeStampFormat: iso8601-local
# 存储
storage:
dbPath: /data/mongodb/config(shardx)(mongos)/data
journal:
enabled: true
directoryPerDB: true
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: snappy
indexConfig:
prefixCompression: true
# 进程管理
processManagement:
pidFilePath: /data/mongodb/config(shardx)(mongos)/run/config(shardx)(mongos).pid
fork: true
# 网络
net:
bindIpAll: true
port: 27019(270xx)
ssl:
mode: allowSSL
PEMKeyFile: /etc/mongod.d/server.pem
CAFile: /etc/mongod.d/ca.pem
allowInvalidHostnames: true
# 安全
security:
keyFile: /etc/mongod.d/mongodb.key
clusterAuthMode: sendKeyFile
# mongos 不需要下面的参数
authorization: enabled
# mongos 不需要下面的参数
sharding:
clusterRole: configsvr(shardsvr)
# mongos 需要下面的参数
# sharding:
# configDB: config/hostname:27019
# 复制
replication:
oplogSizeMB: 1024
replSetName: config(shardxxxx)
# only shardsvr
# operationProfiling:
# mode: slowOp
# slowOpThresholdMs: 500
systemd.service ¶
service文件名称随意指定都行,具体内容如下
[Unit]
Description=MongoDB Database Config Server
After=network.target
Documentation=https://docs.mongodb.org/manual
[Service]
User=mongod
Group=mongod
# config server
Environment="OPTIONS=--quiet -f /etc/mongod.d/mongod.config.conf"
ExecStart=/usr/bin/mongod $OPTIONS run
PIDFile=/data/mongodb/config/run/config.pid
# shardx server
# yum install -y numactl
Environment="OPTIONS=--quiet -f /etc/mongod.d/mongod.shardx.conf"
ExecStart=/usr/bin/numactl --interleave=all -- /usr/bin/mongod $OPTIONS run
PIDFile=/data/mongodb/shardx/run/shardx.pid
# mongos server
Environment="OPTIONS=-f /etc/mongod.d/mongos.conf"
ExecStart=/usr/bin/mongos $OPTIONS
PIDFile=/data/mongodb/mongos/run/mongos.pid
LimitFSIZE=infinity
LimitCPU=infinity
LimitAS=infinity
LimitNOFILE=64000
LimitNPROC=64000
TasksMax=infinity
TasksAccounting=false
[Install]
WantedBy=multi-user.target
# /etc/systemd/system/disable-thp.service
[Unit]
Description=Disable Transparent Huge Pages (THP)
[Service]
Type=simple
ExecStart=/bin/sh -c "echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled && echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag"
[Install]
WantedBy=multi-user.target
systemctl daemon-reload;
systemctl enable --now disable-thp.service
data,log,run ¶
添加镜像源
# /etc/yum.repos.d/mongodb-org-4.2.repo
[mongodb-enterprise-4.2]
name=MongoDB Enterprise Repository
baseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/4.2/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
mkdir -p /data/mongodb/{config,shardx,mongos}/{data,log,run}/
chown -R mongod:mongod /data/mongodb
接下来需要在mongo shell依次操作
use admin
rs.initiate({
_id : 'config',
members: [
{ _id : 0, host : "host:27019" }
]
});
rs.initiate({
_id : 'shard0000',
members: [
{ _id : 0, host : "host:27020" }
]
});
rs.initiate({
_id : 'shard0001',
members: [
{ _id : 0, host : "host:27021" }
]
});
# admin
db.createUser({
user: "admin",
pwd: "xxxxxxxx",
roles: ["root"]
})
rs.status()
# mongos
sh.addShard("shard0000/host:27020")
sh.addShard("shard0001/host:27021")
sh.status()
到这一步就OK了 😄