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

相关推荐
一氧化二氢.h22 分钟前
MySQL root用户连接错误解决方法
android·数据库·mysql
q***239237 分钟前
数据库操作与数据管理——Rust 与 SQLite 的集成
数据库·rust·sqlite
q***333741 分钟前
给SQL server数据库表字段添加注释SQL,附修改、删除注释SQL及演示
数据库·sql·oracle
百***22121 小时前
mysql 迁移达梦数据库出现的 sql 语法问题 以及迁移方案
数据库·sql·mysql
_Jimmy_1 小时前
ShardingSphere-JDBC 实现两个mysql数据库的不同表的关联查询
数据库·mysql
weixin_307779131 小时前
基于AWS的应用程序可靠性提升架构优化方案——RDS多可用区与EC2弹性架构实践
数据库·数据仓库·架构·云计算·aws
Chan161 小时前
【 Java八股文面试 | Redis篇 缓存问题、持久化、分布式锁 】
java·数据库·redis·后端·spring·缓存·面试
G***T6912 小时前
PostgreSQL全文搜索教程,中文分词配置
数据库·postgresql·中文分词
必胜刻2 小时前
MySQL复盘总结
数据库·mysql·oracle
百***79462 小时前
MySQL数据库(SQL分类)
数据库·sql·mysql