You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu安装MongoDB时遇mongod.service无法停止问题求助

Fixing "Unit mongod.service not loaded" When Installing MongoDB on Ubuntu

Hey there, let's work through this issue together. The error you're seeing means your system doesn't have a registered mongod.service (or mongodb.service) for systemd to manage—so trying to stop it via systemctl won't work because it doesn't exist yet. Here's how to resolve this:

Step 1: Check for running mongod processes

First, MongoDB might be running directly (not via systemd), which is why you need to stop it before installation. Run this command to find any active mongod processes:

ps aux | grep mongod

Look for lines that show mongod (ignore the grep line itself). You'll see a process ID (PID) in the second column.

Step 2: Terminate the running mongod process

Use the PID from the previous step to kill the process:

sudo kill <your-mongod-pid>

If that doesn't work (the process won't stop), use the force kill command:

sudo kill -9 <your-mongod-pid>

Run the ps aux | grep mongod command again to confirm the process is gone.

Step 3: Clean up and install MongoDB properly

Since the systemd service wasn't present, chances are you had an incomplete or outdated installation. Let's fix that:

  • Remove any existing MongoDB packages and their configuration:
    sudo apt remove --purge mongodb*
    sudo apt autoremove && sudo apt autoclean
    
  • Install the official MongoDB package (this will set up the correct systemd service):
    1. Import the MongoDB GPG key:
      curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
      
      Note: Replace 7.0 with your desired MongoDB version if needed.
    2. Add the official repository to your system:
      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
      
      Replace jammy with your Ubuntu codename (e.g., focal for 20.04, noble for 24.04).
    3. Update your package list and install MongoDB:
      sudo apt update
      sudo apt install -y mongodb-org
      

Step 4: Verify the systemd service works

Now you can manage the mongod service with systemctl:

  • Start the service:
    sudo systemctl start mongod
    
  • Check its status:
    sudo systemctl status mongod
    
  • Enable it to start on boot (optional but recommended):
    sudo systemctl enable mongod
    

This should resolve the "unit not loaded" error and let you complete your MongoDB installation smoothly.

内容的提问来源于stack exchange,提问作者Arunkumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:02:12