Ubuntu安装MongoDB时遇mongod.service无法停止问题求助
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):
- Import the MongoDB GPG key:
Note: Replacecurl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor7.0with your desired MongoDB version if needed. - Add the official repository to your system:
Replaceecho "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.listjammywith your Ubuntu codename (e.g.,focalfor 20.04,noblefor 24.04). - Update your package list and install MongoDB:
sudo apt update sudo apt install -y mongodb-org
- Import the MongoDB GPG key:
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

