当前位置: 首页 > news >正文

Ubuntu24安装MongoDB7

在 Ubuntu 中查看系统版本有多种方式,以下几种都可以用:


方式 1:使用 lsb_release 命令(最推荐)

lsb_release -a

输出示例:

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04.1 LTS
Release:        24.04
Codename:       noble

👉 这里的 Release 是版本号(24.04),Codename 是代号(noble)。


方式 2:查看 /etc/os-release 文件

cat /etc/os-release

输出示例:

NAME="Ubuntu"
VERSION="24.04.1 LTS (Noble Numbat)"
ID=ubuntu
VERSION_ID="24.04"
PRETTY_NAME="Ubuntu 24.04.1 LTS"

方式 3:使用 hostnamectl 命令

hostnamectl

输出中包含:

Operating System: Ubuntu 24.04.1 LTS
Kernel: Linux 6.8.0-36-generic
Architecture: x86-64

好的 👍,以下是在 Ubuntu 24.04(代号 noble)上安装 MongoDB 7.0 社区版的完整教程(官方源方式,兼容 apt 包管理系统)。
——这是当前推荐的稳定版本之一。


🧩 一、系统准备

sudo apt update
sudo apt upgrade -y
sudo apt install -y gnupg curl

🔹 Ubuntu 24.04 默认代号是 noble,安装 MongoDB 7 时要手动添加官方仓库,因为默认源中没有。


🧰 二、添加官方仓库

  1. 导入 MongoDB 7 的 GPG 公钥

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \--dearmor
  1. 添加 MongoDB 官方源

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

💡 说明:

  • noble 是 Ubuntu 24.04 的代号。

  • 如果报仓库找不到,可暂时改为 jammy(Ubuntu 22.04 源),MongoDB 7 仍能正常运行:

    https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse
    

⚙️ 三、安装 MongoDB 7

sudo apt-get update
sudo apt-get install -y mongodb-org

这会安装以下组件:

组件说明
mongod数据库服务主进程
mongos分片集群的路由进程
mongosh新版命令行客户端(替代旧版 mongo
mongodb-org-tools备份与导入工具(如 mongodump、mongorestore 等)

🚀 四、启动并设置开机自启

sudo systemctl start mongod
sudo systemctl enable mongod

检查状态:

sudo systemctl status mongod

若显示 active (running) 则安装成功。

pplsunny@pplsunny-VMware-Virtual-Platform:~$ sudo systemctl start mongod
pplsunny@pplsunny-VMware-Virtual-Platform:~$ sudo systemctl status mongod
● mongod.service - MongoDB Database ServerLoaded: loaded (/usr/lib/systemd/system/mongod.service; disabled; preset: enabled)Active: active (running) since Fri 2025-10-24 22:23:06 CST; 16s agoDocs: https://docs.mongodb.org/manualMain PID: 30559 (mongod)Memory: 73.3M (peak: 74.1M)CPU: 1.111sCGroup: /system.slice/mongod.service└─30559 /usr/bin/mongod --config /etc/mongod.confOct 24 22:23:06 pplsunny-VMware-Virtual-Platform systemd[1]: Started mongod.service - MongoDB Database Server.
Oct 24 22:23:06 pplsunny-VMware-Virtual-Platform mongod[30559]: {"t":{"$date":"2025-10-24T14:23:06.582Z"},"s":"I",  "c":"CONTR>
lines 1-12/12 (END)

🧪 五、验证安装

查看版本:

mongod --version

连接数据库:

mongosh

退出:

exit

🔐 六、安全配置(推荐)

  • 默认情况下 MongoDB 是不开启认证的。建议切换至 admin 数据库创建管理员用户:

    mongosh 
    use admin 
    db.createUser({user: "adminuser",pwd: "1qazxsw2",roles: [{ role: "userAdminAnyDatabase", db: "admin" },{ role: "readWriteAnyDatabase", db: "admin" }]
    })
    exit
  1. 创建管理员用户:

    pplsunny@pplsunny-VMware-Virtual-Platform:~$ mongosh
    Current Mongosh Log ID: 68fb8c75c96ef72013ce5f46
    Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.5.8
    Using MongoDB:          7.0.25
    Using Mongosh:          2.5.8For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
    You can opt-out by running the disableTelemetry() command.------The server generated these startup warnings when booting2025-10-24T22:23:07.131+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem2025-10-24T22:23:07.815+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
    ------test> use admin
    switched to db admin
    admin> db.createUser({
    ...   user: "adminuser",
    ...   pwd: "1qazxsw2",
    ...   roles: [{ role: "userAdminAnyDatabase", db: "admin" },
    ...           { role: "readWriteAnyDatabase", db: "admin" }]
    ... })
    { ok: 1 }
    admin> exit
    pplsunny@pplsunny-VMware-Virtual-Platform:~$
    
  2. 修改配置文件启用认证;  若需远程访问,还需修改 net.bindIp(例如 0.0.0.0)或添加防火墙规则

  3. sudo nano /etc/mongod.conf
    

    找到:

    security:authorization: enabled
    
  4. pplsunny@pplsunny-VMware-Virtual-Platform:~$ sudo nano /etc/mongod.conf
    pplsunny@pplsunny-VMware-Virtual-Platform:~$ cat /etc/mongod.conf
    # mongod.conf# for documentation of all options, see:
    #   http://docs.mongodb.org/manual/reference/configuration-options/# Where and how to store data.
    storage:dbPath: /var/lib/mongodb
    #  engine:
    #  wiredTiger:# where to write logging data.
    systemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod.log# network interfaces
    net:port: 27017bindIp: 0.0.0.0# how the process runs
    processManagement:timeZoneInfo: /usr/share/zoneinfosecurity:authorization: enabled
    #operationProfiling:#replication:#sharding:## Enterprise-Only Options:#auditLog:
    pplsunny@pplsunny-VMware-Virtual-Platform:~$
    

  5. 重启服务:

    sudo systemctl restart mongod
    
  6. 以后连接需使用:

    mongosh -u admin -p --authenticationDatabase admin
    

🌐 七、远程访问(如需)

编辑 /etc/mongod.conf

net:port: 27017bindIp: 0.0.0.0

保存后:

sudo systemctl restart mongod

⚠️ 建议使用防火墙限制访问:

sudo ufw allow from <your_ip> to any port 27017

🧹 八、卸载(可选)

如果你想完全移除 MongoDB 7:

sudo systemctl stop mongod
sudo apt purge -y mongodb-org*
sudo rm -r /var/log/mongodb /var/lib/mongodb

http://www.dtcms.com/a/523781.html

相关文章:

  • Ubuntu 自动挂载移动硬盘
  • 如何使用Postman做接口自动化测试及完美的可视化报告?
  • 配置Centos7.6 yum镜像源
  • Flink非对齐checkpoint踩坑记
  • 使用 WebSocket 实现手机控制端和电脑展示端的实时通信,包含断线重连功能。
  • 服装网站建设怎么写wordpress strip_tags
  • 一文讲清:数据清洗、数据中台、数据仓库、数据治理
  • 【C++ STL】探索STL的奥秘——vector底层的深度剖析和模拟实现!
  • STM32CUBEMX安装离线库
  • 体验 Suno v5:最新的 Suno AI 音乐模型
  • 2.4 欧拉集群安装Nova计算服务
  • 贵港网站建设兼职免费广告设计网站
  • Cell Mol Biol Lett|Runx2诱导超级沉默子形成下调Lpl表达:重塑雪旺细胞脂质代谢的新机制
  • 国自然·医工交叉热点|泛癌组织学重建AI模型
  • Python依赖管理与环境迁移实战:Poetry+Docker构建高效开发体系
  • 山西网站建设推荐景区网站如何建设
  • Flutter---CupertinoPicker滚动选择器
  • 全面掌握PostgreSQL关系型数据库,备份和恢复,笔记46和笔记47
  • Python SQLAlchemy模块:从入门到实战的数据库操作指南
  • 天津哪里有做网站的jquery wordpress
  • 流媒体网站建设规划亚马逊网站建设案例
  • PHP 异步IO扩展包 AsyncIO v2.0.0 发布
  • 《信息系统项目管理师》案例分析题及解析模拟题5
  • Jenkins上实现CI集成软件信息Teams群通知案例实现。
  • ZYNQ平台中断服务函数中的变量不加volatile修饰导致的奇怪问题解决
  • 2026年UX/UI五大趋势:AI、AR与包容性设计将重新定义用户体验
  • 网站做跳转自己建网站卖鞋
  • 百度网站服务器外贸网站优化
  • 应广单片机烧录跳线J7专用PCB使用说明
  • Java 前后端加密与编码技术:从概念到实战场景全解析