本文内容均来自个人笔记并重新梳理,如有错误欢迎指正!
如果对您有帮助,烦请点赞、关注、转发、订阅专栏!
专栏订阅入口
Linux 专栏 | Docker 专栏 | Kubernetes 专栏
往期精彩文章
【Docker】(全网首发)Kylin V10 下 MySQL 容器内存占用异常的解决方法
目录
一、背景介绍
本文基于 MongoDB 4.2 版本,在 Kylin V10 操作系统下完成了 MongoDB 副本集模式(1主2从)的非容器化部署验证工作,以下为完整过程。
免费提供离线安装包,安装更方便,欢迎下载使用:MongoDB 副本集模式离线安装包.tgz
二、过程回顾
1、创建目录
# 创建安装目录、运行目录、pid目录、数据目录、配置目录、日志目录
mkdir -p /data/mongodb4-install /data/mongodb4/bin /data/mongodb4/run /data/mongodb4/data /data/mongodb4/conf /data/mongodb4/log
2、下载安装包
cd /data/mongodb4-install
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.25.tgz
3、安装二进制文件
cd /data/mongodb4-install
# 解压文件
tar -xzf mongodb-linux-x86_64-rhel70-4.2.25.tgz
# 拷贝可执行文件
cp mongodb-linux-x86_64-rhel70-4.2.25/bin/* /data/mongodb4/bin/
# 拷贝配置文件(文件内容见附录)
cp /data/mongodb4-install/mongod.conf /data/mongodb4/conf/mongod.conf
# 拷贝启动文件(文件内容见附录)
cp /data/mongodb4-install/mongod.service /usr/lib/systemd/system/mongod.service
4、创建用户、用户主、修改属主
# 创建用户、用户主
groupadd mongod
useradd -g mongod mongod
# 修改目录属主
chown -R mongod:mongod /data/mongodb4
# 可以为 mongod 设置密码
# passwd mongod 或 echo <new_password> | passwd --stdin mongod
5、修改环境变量
# 分别在 root 用户和 mongod 用户下执行
cat >> ~/.bashrc <<EOF
export PATH=$PATH:/data/mongodb4/bin
EOF
############################################################################
tail -5 ~/.bashrc && source ~/.bashrc
6、检查动态库文件
ldd /data/mongodb4/bin/mongod | grep "not found"
# 如果返回的结果为空,则可以忽略后续步骤
# 如果返回的结果如下,则需要执行后续步骤
libcrypto.so.10 => not found
libssl.so.10 => not found
cd /data/mongodb4-install
# 下载 openssl 源码
wget -O OpenSSL_1_0_2n.tgz https://codeload.github.com/openssl/openssl/tar.gz/refs/tags/OpenSSL_1_0_2n
# 解压文件
tar -xzf OpenSSL_1_0_2n.tgz && cd openssl-OpenSSL_1_0_2n
# 源码编译
mkdir -p /usr/local/openssl_1.0.2
./config shared --prefix=/usr/local/openssl_1.0.2
make && make install
cp -p /usr/local/openssl_1.0.2/lib/libssl.so /lib64/libssl.so.10
cp -p /usr/local/openssl_1.0.2/lib/libcrypto.so /lib64/libcrypto.so.10
# 再次检查动态库文件
ldd /data/mongodb4/bin/mongod | grep "not found"
7、启动服务
#重新加载
systemctl daemon-reload
#启动服务
systemctl start mongod.service
#查看服务状态
systemctl status mongod.service
#设置服务自启
systemctl enable mongod.service
8、初始化副本集
# 执行 mongo 命令
mongo
# 连接到任何一个节点,执行初始化
rs.initiate( {
_id : "prod01",
members: [
{ _id: 0, host: "<节点一IP>:27017" },
{ _id: 1, host: "<节点二IP>:27017" },
{ _id: 2, host: "<节点三IP>:27017" }
]
})
# 注意:其他服务无法解析主机名,请使用节点IP进行初始化,否则业务服务会有如下报错:
panic: server selection error: server selection timeout, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: kylin-test-00003:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp: lookup kylin-test-00003 on 10.96.0.10:53: no such host }, { Addr: kylin-test-00004:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp: lookup kylin-test-00004 on 10.96.0.10:53: no such host }, { Addr: kylin-test-00005:27017, Type: Unknown, Last error: connection() error occurred during connection handshake: dial tcp: lookup kylin-test-00005 on 10.96.0.10:53: no such host }, ] }
# 查看副本集配置、状态
rs.conf()
rs.status()
# 其他必要的初始化处理
9、设置备份任务
# 主库服务器执行,切换至 root 用户
mkdir /data/backup && touch /data/backup/backup.sh
cat > /data/backup/backup.sh <<EOF
#!/bin/bash
data_time=\$(date +%F_%H)
save_time=7
/data/mongodb4/bin/mongodump -u<your_user> -p<your_passwd> --authenticationDatabase user_log --gzip -o /data/backup/\${data_time}mongo
find /data/backup -maxdepth 1 -type d -mtime +\${save_time} | xargs rm -rf
EOF
############################################################################
echo "00 */6 * * * bash /data/backup/backup.sh" > /var/spool/cron/root
crontab -l
附录
1、mongod.conf
# where to write logging data.
systemLog:
quiet: true
destination: file
logAppend: true
path: /data/mongodb4/log/mongod.log
# Where and how to store data.
storage:
dbPath: /data/mongodb4/data
journal:
enabled: true
commitIntervalMs: 500
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb4/run/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
# authorization: enabled
#replca set
replication:
replSetName: "prod01"
2、mongod.service
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /data/mongodb4/conf/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/data/mongodb4/bin/mongod $OPTIONS
PermissionsStartOnly=true
PIDFile=/data/mongodb4/run/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
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 for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
[Install]
WantedBy=multi-user.target