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 时要手动添加官方仓库,因为默认源中没有。
🧰 二、添加官方仓库
-
导入 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
-
添加 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
-
创建管理员用户:
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:~$ -
修改配置文件启用认证; 若需远程访问,还需修改
net.bindIp(例如0.0.0.0)或添加防火墙规则 -
sudo nano /etc/mongod.conf找到:
security:authorization: enabled -
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:~$ -
重启服务:
sudo systemctl restart mongod -
以后连接需使用:
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
