MongoDB部署
1:环境
MongoDB 官网:https://www.mongodb.com/
各版本软件下载地址:https://www.mongodb.com/download-center/community?jmp=nav
CentOS 7.9
2:安装前准备
在安装之前首先确认该版本软件是否支持你的操作系统。
更多详情查看:https://docs.mongodb.com/manual/installation/
Platform 4.0 Community 3.6 Community 3.4 Community 3.2 Community 3.0 Community
RHEL/CentOS 6.2 and later ✓ ✓ ✓ ✓ ✓
RHEL/CentOS 7.0 and later ✓ ✓ ✓ ✓ ✓
版本说明:MongoDB:5.0.6
3:部署MongoDB
1:安装所需依赖
[root@mongodb ~]# yum -y install libcurl openssl
2:上传包
[root@mongodb ~]# ls
anaconda-ks.cfg mongodb-linux-x86_64-rhel70-5.0.6.tgz
3:解压包
[root@mongodb ~]# tar xf mongodb-linux-x86_64-rhel70-5.0.6.tgz
[root@mongodb ~]# ls
anaconda-ks.cfg mongodb-linux-x86_64-rhel70-5.0.6 mongodb-linux-x86_64-rhel70-5.0.6.tgz
[root@mongodb ~]# mv mongodb-linux-x86_64-rhel70-5.0.6 mongo
[root@mongodb ~]# mv mongo /usr/local/
4:创建数据存储目录和日志文件目录
[root@mongodb ~]# mkdir -p /usr/local/mongo/data/db
[root@mongodb ~]# mkdir -p /usr/local/mongo/logs
5:编辑 MongoDB 启动配置文件
[root@mongodb ~]# cat /usr/local/mongo/bin/mongo.conf
dbpath=/usr/local/mongo/data/db
logpath=/usr/local/mongo/logs/mongodb.log
fork=true
auth=true
bind_ip=0.0.0.0
6:参数说明
参数 参数说明
–dbpath 数据存放路径
–logpath 日志文件路径
–logappend 日志输出方式
–port 启用端口号
–fork 在后台运行
–auth 是否需要验证权限登录 (用户名和密码)
–bind_ip 限制访问的 ip
–shutdown 关闭数据库
7:MongoDB 的可执行文件位于 bin 目录下,需要将其添加到 PATH 路径中
vim /etc/profile
export MONGODB_HOME=/usr/local/mongo
export PATH=$PATH:$MONGODB_HOME/bin #末尾处添加
source /etc/profile #重新加载环境变量
8:启动 MongoDB
[root@mongodb ~]# cd /usr/local/mongo/bin
[root@mongodb ~]# mongod -f mongo.conf
9:访问
[root@mongodb bin]# mongo
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0f301b3f-000a-499a-abd7-7d334e274b4c") }
MongoDB server version: 5.0.6
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
>
[root@mongodb bin]# mongod -f mongo.conf --shutdown # 关闭服务
{"t":{"$date":"2021-12-28T05:47:34.870Z"},"s":"I", "c":"CONTROL", "id":20697, "ctx":"-","msg":"Renamed existing log file","attr":{"oldLogPath":"/usr/local/mongo/logs/mongodb.log","newLogPath":"/usr/local/mongo/logs/mongodb.log.2021-12-28T05-47-34"}}
killing process with pid: 11912
10:使用systemd管理
[root@mongodb bin]# cat /lib/systemd/system/mongod.service
[Unit]
Description=MongoDB database server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mongo/bin/mongod -f /usr/local/mongo/bin/mongo.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
11:重载启动
[root@mongodb bin]# systemctl daemon-reload
[root@mongodb bin]# systemctl start mongod.service
[root@mongodb bin]# systemctl enable mongod.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
[root@mongodb bin]# systemctl status mongod
● mongod.service - MongoDB database server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2021-12-28 00:50:32 EST; 14s ago
Main PID: 12264 (mongod)
CGroup: /system.slice/mongod.service
└─12264 /usr/local/mongo/bin/mongod -f /usr/local/mongo/bin/mongo.conf
Dec 28 00:50:31 mongodb systemd[1]: Starting MongoDB database server...
Dec 28 00:50:31 mongodb mongod[12262]: about to fork child process, waiting until server is ready for connections.
Dec 28 00:50:31 mongodb mongod[12262]: forked process: 12264
Dec 28 00:50:32 mongodb mongod[12262]: child process started successfully, parent exiting
Dec 28 00:50:32 mongodb systemd[1]: Started MongoDB database server.
[root@mongodb bin]# ss -lnt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:27017 *:*
4:数据库的启动与关闭
启动:mongod --dbpath=/usr/local/mongo/data --logpath=/usr/local/mongo/log/mongo.log --port=27017 --logappend --fork
关闭:mongod --shutdown --dbpath=/usr/local/mongo/data --logpath=/usr/local/mongo/log/mongo.log --port=27017 --logappend --fork
5:参数说明:
参数 参数说明
–dbpath 数据存放路径
–logpath 日志文件路径
–logappend 日志输出方式
–port 启用端口号
–fork 在后台运行
–auth 是否需要验证权限登录 (用户名和密码)
–bind_ip 限制访问的 ip
–shutdown 关闭数据库
6:使用配置文件的方式管理数据库
1:普通配置文件
[root@mongodb mongo]# mkdir conf
[root@mongodb mongo]# cat conf/mongod1.conf
dbpath=/usr/local/mongo/data
logpath=/usr/local/mongo/log/mongo.log
port=27017
logappend=1
fork=1
使用配置文件时的启动与关闭:
启动:mongod -f mongod1.conf
关闭:mongod -f mongod1.conf --shutdown
2:YAML 格式配置文件(官方推荐使用)
[root@mongodb mongo]# cat conf/mongo.conf
systemLog:
destination: file
path: "/usr/local/mongo/log/mongod.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/usr/local/mongo/data"
processManagement:
fork: true
net:
port: 27017
注:mongod 进程收到 SIGINT 信号或者 SIGTERM 信号,会做一些处理
关闭所有打开的连接
将内存数据强制刷新到磁盘
当前的操作执行完毕
安全停止
切忌 kill -9
如果使用 kill -9 直接关闭数据库,可能会造成数据丢失,数据文件损失等风险。
可通过如下安全的方式进行关闭。
kill -2 PID
原理:-2表示向mongod进程发送SIGINT信号。
kill -4 PID
原理:-4表示向mognod进程发送SIGTERM信号。
当然也可以通过使用systemctl stop mongod来关闭数据库。