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 →

相关推荐
庞轩px1 小时前
第七篇:Spring扩展点——如何优雅地介入Bean的创建流程
java·后端·spring·bean·aware·扩展点
ltl1 小时前
Q/K/V 三件套:把 Bahdanau 抽象成一个公式
后端
千叶风行3 小时前
Text-to-SQL 技术设计与注意事项
前端·人工智能·后端
阿kun要赚马内4 小时前
后端数据操作组合:Pydantic与ORM
后端·python·orm·sqlalchemy
花米徐5 小时前
技术洞察精选 | 2026年4月28日 — 5月4日
后端·python·flask
阿维的博客日记5 小时前
Spring Cloud 为什么需要服务注册与发现中心这些东西?
后端·spring·spring cloud
笑而不语5 小时前
13|元数据过滤检索:UserContext 与按用户查知识
后端
用户095367515836 小时前
Go:浮点数如何进行比较?
后端·go
Zeus_6 小时前
如何更好的创建skill
后端
千云6 小时前
AI Coding 落地探索日志 · 初篇 · 启程记
后端·ai编程