在 Debian 上让 MongoDB 开机自启,主要取决于你安装方式和Debian 版本(systemd 还是 sysV)。现在绝大多数 Debian(8+)都用 systemd。
下面分情况说明:
如果你是通过 MongoDB 官方源安装的(如 mongodb-org):
systemctl list-unit-files | grep mongo
通常是:
mongod.service
sudo systemctl enable mongod
sudo systemctl start mongod
systemctl status mongod
✅ 这样就完成自启配置了。
如果你装的是:
apt install mongodb
服务名可能是:
mongodb.service
操作一样:
sudo systemctl enable mongodb
sudo systemctl start mongodb
sudo update-rc.d mongod defaults
或(某些版本):
sudo update-rc.d mongodb enable
如果你是自己解压 MongoDB 二进制文件的,需要自己写 systemd 服务:
sudo vim /etc/systemd/system/mongod.service
示例内容:
[Unit]
Description=MongoDB
After=network.target
[Service]
ExecStart=/usr/local/mongodb/bin/mongod --config /etc/mongod.conf
User=mongodb
Group=mongodb
Restart=on-failure
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable mongod
sudo systemctl start mongod
systemctl is-enabled mongodjournalctl -u mongod如果你愿意,可以告诉我:
cat /etc/debian_version)我可以给你精确命令。