MongoDB 5.0 单机二进制安装

前言

以前基本用不到 MongoDB,所以没学习过,最近正好需要安装一套,研究了一下,本文记录一下安装过程。

MongoDB 安装

安装前卸载系统自带的 MongoDB 版本:

bash 复制代码
[root@lpc ~]# rpm -qa | grep mongodb
[root@lpc ~]# ps -ef | grep mongodb
root       12591   12530  0 13:42 pts/0    00:00:00 grep --color=auto mongodb
[root@lpc ~]# yum erase -y $(rpm -qa | grep mongodb-org)
No packages marked for removal.
Dependencies resolved.
Nothing to do.
Complete!

上传 MongoDB 安装包:

bash 复制代码
[root@lpc ~]# mkdir /soft
[root@lpc ~]# mv /tmp/mongodb-linux-x86_64-rhel80-5.0.0.tgz /soft/
[root@lpc soft]# cd /soft/
[root@lpc soft]# ls
mongodb-linux-x86_64-rhel80-5.0.0.tgz

解压二进制安装包:

bash 复制代码
# 解压
[root@lpc soft]# mkdir /data
[root@lpc soft]# tar -xzvf mongodb-linux-x86_64-rhel80-5.0.0.tgz -C /data
mongodb-linux-x86_64-rhel80-5.0.0/LICENSE-Community.txt
mongodb-linux-x86_64-rhel80-5.0.0/MPL-2
mongodb-linux-x86_64-rhel80-5.0.0/README
mongodb-linux-x86_64-rhel80-5.0.0/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel80-5.0.0/bin/install_compass
mongodb-linux-x86_64-rhel80-5.0.0/bin/mongo
mongodb-linux-x86_64-rhel80-5.0.0/bin/mongod
mongodb-linux-x86_64-rhel80-5.0.0/bin/mongos

重命名目录:

bash 复制代码
[root@lpc soft]# cd /data/
[root@lpc data]# mv /data/mongodb-linux-x86_64-rhel80-5.0.0 /data/mongodb-5.0.0

配置环境变量:

bash 复制代码
[root@lpc data]# cat<<-EOF>>/etc/profile
export MONGO_HOME=/data/mongodb-5.0.0
export PATH=/data/mongodb-5.0.0/bin:$PATH
EOF

[root@lpc data]# source /etc/profile

在 MongoDB 安装目录中创建数据、日志目录:

bash 复制代码
[root@lpc soft]# cd /data/mongodb-5.0.0
[root@lpc mongodb-5.0.0]# mkdir data logs
[root@lpc mongodb-5.0.0]# ls
bin  data  LICENSE-Community.txt  logs  MPL-2  README  THIRD-PARTY-NOTICES

编辑 MongoDB 配置信息:

bash 复制代码
[root@lpc mongodb-5.0.0]# echo $MONGO_HOME
/data/mongodb-5.0.0

[root@lpc mongodb-5.0.0]# cat<<-EOF>mongodb.conf
## 端口
port=3717
## 允许远程连接
bind_ip=0.0.0.0
## 数据文件存放路径
dbpath=$MONGO_HOME/data
## 日志文件存放路径
logpath=$MONGO_HOME/logs/mongodb.log
## 后台运行
fork=true
EOF

配置 MongoDB 为系统服务:

bash 复制代码
[root@lpc mongodb-5.0.0]# echo $MONGO_HOME
/data/mongodb-5.0.0

[root@lpc mongodb-5.0.0]# cat<<-EOF>/usr/lib/systemd/system/mongodb.service
[Unit]
Description=mongodb-server
After=network.target

[Service]
Type=forking
ExecStart=$MONGO_HOME/bin/mongod --config $MONGO_HOME/mongodb.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

[root@lpc mongodb-5.0.0]# chmod 755 /usr/lib/systemd/system/mongodb.service

设置开机自启动:

bash 复制代码
[root@lpc mongodb-5.0.0]# systemctl start mongodb
[root@lpc mongodb-5.0.0]# systemctl status mongodb
● mongodb.service - mongodb-server
   Loaded: loaded (/usr/lib/systemd/system/mongodb.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2025-09-25 13:55:28 CST; 628ms ago
  Process: 12722 ExecStart=/data/mongodb-5.0.0/bin/mongod --config /data/mongodb-5.0.0/mongodb.conf (code=exited, status=0/SUCCESS)
 Main PID: 12724 (mongod)
    Tasks: 34 (limit: 22548)
   Memory: 54.6M
   CGroup: /system.slice/mongodb.service
           └─12724 /data/mongodb-5.0.0/bin/mongod --config /data/mongodb-5.0.0/mongodb.conf

Sep 25 13:55:27 lpc systemd[1]: Starting mongodb-server...
Sep 25 13:55:27 lpc mongod[12722]: about to fork child process, waiting until server is ready for connections.
Sep 25 13:55:27 lpc mongod[12724]: forked process: 12724
Sep 25 13:55:28 lpc mongod[12722]: child process started successfully, parent exiting
Sep 25 13:55:28 lpc systemd[1]: Started mongodb-server.

连接 MongoDB:

bash 复制代码
[root@lpc mongodb-5.0.0]# mongo --port 3717
MongoDB shell version v5.0.0
connecting to: mongodb://127.0.0.1:3717/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("824697d1-a40a-42af-a6dd-a0a93bb441f7") }
MongoDB server version: 5.0.0
================
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.
We recommend you begin using "mongosh".
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
---
The server generated these startup warnings when booting: 
        2025-09-25T13:55:27.537+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2025-09-25T13:55:28.248+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2025-09-25T13:55:28.248+08:00: You are running this process as the root user, which is not recommended
        2025-09-25T13:55:28.248+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2025-09-25T13:55:28.248+08:00: Failed to read /sys/kernel/mm/transparent_hugepage/defrag
        2025-09-25T13:55:28.248+08:00:         error: ** WARNING: unrecognized transparent Huge Pages mode of operation in /sys/kernel/mm/transparent_hugepage/defrag: 'defer''
        2025-09-25T13:55:28.248+08:00: Soft rlimits for open file descriptors too low
        2025-09-25T13:55:28.248+08:00:         currentValue: 1024
        2025-09-25T13:55:28.248+08:00:         recommendedMinimum: 64000
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> db.version()
5.0.0

创建一个 root 用户:

bash 复制代码
> use admin
switched to db admin
> db.createUser({user:"root", pwd:"**********", roles:[{role:"dbAdminAnyDatabase", db:"admin"}]})
Successfully added user: {
        "user" : "root",
        "roles" : [
                {
                        "role" : "dbAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}
> exit
bye

至此,MongoDB 安装完成。


📚 推荐阅读:DBA 学习之路

如果这篇文章对你有帮助,推荐访问我的 Oracle DBA 系统学习站点,涵盖 100 天完整学习路线:

  • 🔧 Oracle 安装部署 · RMAN 备份恢复 · Data Pump 数据迁移
  • 🏗️ RAC 高可用 · DataGuard 容灾 · 多租户架构
  • 🔍 故障排查 · 升级迁移 · GoldenGate 数据同步

👉 立即访问 ora100.com →

相关推荐
IT_陈寒2 小时前
用了Proxy才发现以前的JavaScript白写了
前端·人工智能·后端
唐青枫2 小时前
别把 ArrayList 当成会自动管理内存的 List:Zig 动态数组从入门到实战
后端
鸿蒙开发3 小时前
我为 HarmonyOS 做了一个统一大模型 SDK:@hmkit/ai 正式开源
后端
猪是念来过倒3 小时前
Semaphore 与 RateLimiter:并发控制双雄详解
后端
掘金者阿豪3 小时前
异构数据同步最怕什么?不是同步慢,而是数据对不上
后端
程序员cxuan4 小时前
Claude 官方的学习教程,太强了。
人工智能·后端·程序员
老孙讲技术4 小时前
把工地遮挡和掉线接进项目部:setMessageCallback 订 alarm 与 deviceStatus
后端·物联网·音视频开发
老孙讲技术4 小时前
关店后有人进门,监控值班群却没响:用 setMessageCallback 订 human,再用 aiHuman 打开人形
后端·物联网·音视频开发
LEE4 小时前
原来 Claude Code 最厉害的工具,一行代码都不写
前端·后端
老孙讲技术4 小时前
把园区几百路摄像头收进值班台账:bindDevice 入账与 listDeviceDetailsByPage 翻页
后端·物联网·音视频开发