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 公钥
bash 复制代码
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 官方源
bash 复制代码
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

bash 复制代码
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) 则安装成功。

bash 复制代码
pplsunny@pplsunny-VMware-Virtual-Platform:~$ sudo systemctl start mongod
pplsunny@pplsunny-VMware-Virtual-Platform:~$ sudo systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/usr/lib/systemd/system/mongod.service; disabled; preset: enabled)
     Active: active (running) since Fri 2025-10-24 22:23:06 CST; 16s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 30559 (mongod)
     Memory: 73.3M (peak: 74.1M)
        CPU: 1.111s
     CGroup: /system.slice/mongod.service
             └─30559 /usr/bin/mongod --config /etc/mongod.conf

Oct 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)

🧪 五、验证安装

查看版本:

bash 复制代码
mongod --version

连接数据库:

复制代码
mongosh

退出:

复制代码
exit

🔐 六、安全配置(推荐)

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

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

    bash 复制代码
    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.8
    
    For 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 booting
       2025-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-filesystem
       2025-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)或添加防火墙规则

bash 复制代码
sudo nano /etc/mongod.conf

找到:

bash 复制代码
security:
  authorization: enabled
bash 复制代码
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: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
  authorization: enabled
#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:
pplsunny@pplsunny-VMware-Virtual-Platform:~$
  1. 重启服务:

    bash 复制代码
    sudo systemctl restart mongod
  2. 以后连接需使用:

    bash 复制代码
    mongosh -u admin -p --authenticationDatabase admin

🌐 七、远程访问(如需)

编辑 /etc/mongod.conf

复制代码
net:
  port: 27017
  bindIp: 0.0.0.0

保存后:

复制代码
sudo systemctl restart mongod

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

bash 复制代码
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

相关推荐
蜡笔小炘4 小时前
SQL sever数据库--第二次作业
数据库·sql·oracle
木易 士心4 小时前
软考数据库系统工程师知识点详解(高频考点与备考建议)
数据库·1024程序员节
MoonBit月兔5 小时前
MoonBit Pearls Vol.12:初探 MoonBit 中的 JavaScript 交互
开发语言·javascript·数据库·交互·moonbit
用户6279947182625 小时前
南大通用GBase 8s JDBC 中 ROW 类型的应用
数据库
wei_shuo5 小时前
平替 MongoDB 实践指南 | 金仓多模数据库助力电子证照系统国产化改造
数据库·1024程序员节·king base·金仓多模数据
best_virtuoso5 小时前
PostgreSQL ST_Intersects、ST_Crosses 和 ST_Overlaps的含义与区别
数据库·postgresql
Pocker_Spades_A5 小时前
金仓多模数据库平替MongoDB的电子证照国产化实践——从2TB数据迁移到1600+并发支撑
数据库·1024程序员节
用户6279947182625 小时前
南大通用GBase 8c MySQL迁移场景下的时区设置陷阱与解决方案
数据库