MongoDB 与 Ubuntu 的兼容性与避坑要点
一 支持矩阵与系统要求
cat /etc/lsb-release。二 常见兼容性问题与对策
apt install mongodb 常提示“没有可安装候选”。原因是未添加 MongoDB 官方 APT 源或使用了系统自带旧包。解决:导入官方 GPG 公钥,添加对应 Ubuntu LTS 的官方源,再 apt update && apt install -y mongodb-org。避免使用 Ubuntu 自带仓库的旧版 mongodb 包。systemctl start mongod 报 “Unit mongod.service not found”。解决:先 systemctl daemon-reload,再启动;检查 /var/log/mongodb/mongod.log 定位配置或权限问题。/etc/mongod.conf 的 net.bindIp: 0.0.0.0(或加 :: 支持 IPv6),并放行防火墙端口(如 ufw allow 27017/tcp)。生产环境务必同时启用认证与最小暴露面。三 快速安装与验证步骤 Ubuntu 20.04 22.04 24.04
sudo apt-get update && sudo apt-get install -y gnupg curlcurl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-8.0.gpgecho "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.listsudo apt-get updatesudo apt-get install -y mongodb-orgsudo systemctl daemon-reloadsudo systemctl start mongod && sudo systemctl enable mongodsudo systemctl status mongod(应见 active (running))mongosh(本地连接默认 27017)mongosh --eval 'db.runCommand({ connectionStatus: 1 })'/etc/mongod.conf:net.bindIp: 0.0.0.0(或 0.0.0.0,::),port: 27017sudo systemctl restart mongodsudo ufw allow 27017/tcp && sudo ufw reloadmongosh 中创建管理员:use admin → db.createUser({user:"admin", pwd:"StrongPass!", roles:["root"]})/etc/mongod.conf 增加 security: authorization: enabled,重启生效。四 替代方案与升级建议
mongodb/mongodb-community-server:8.0),避免宿主机依赖冲突,便于多版本并存与回滚。