AWS EC2 Ubuntu环境MongoDB安装后无法启动求助
Hey there, let’s work through this MongoDB problem step by step. The errors you’re hitting—command not found for mongod/mongo/apt-get, plus the unrecognized service—point to a mix of incomplete installation, missing system service configuration, and potentially a messed-up system PATH. Let’s fix this one piece at a time:
1. First, Fix the apt-get: command not found Error
Ubuntu 16.04 comes with apt-get pre-installed, so this issue is likely due to a broken PATH or corrupted apt package. Here’s how to resolve it:
- Check your current PATH to see if it includes essential system directories:
Look forecho $PATH/usr/binand/usr/sbin—if they’re missing, temporarily add them with:export PATH=$PATH:/usr/bin:/usr/sbin - Try running
apt-get --versionto confirm it works now. If not, use the absolute path to bypass PATH issues:sudo /usr/bin/apt-get update - If even the absolute path fails, repair your apt installation:
sudo /usr/bin/dpkg --configure -a sudo /usr/bin/apt-get install --reinstall apt
2. Reinstall MongoDB Properly (Your Initial Install Likely Failed)
Once apt-get is working, let’s do a clean install of MongoDB (Ubuntu 16.04 is end-of-life, so MongoDB 4.4 is the last supported version):
- Import MongoDB’s official GPG key:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4 - Add the MongoDB repository to your system’s sources list:
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list - Update your apt package index:
sudo apt-get update - Install the MongoDB package:
sudo apt-get install -y mongodb-org
3. Fix the mongod: unrecognized service Error
If the service still isn’t recognized after reinstalling, the systemd service file was probably not created correctly. Let’s set it up manually:
- Create the service file with
nano:sudo nano /etc/systemd/system/mongod.service - Paste the following content into the file:
[Unit] Description=MongoDB Database Server Documentation=https://docs.mongodb.org/manual After=network.target [Service] User=mongodb Group=mongodb ExecStart=/usr/bin/mongod --config /etc/mongod.conf PIDFile=/var/run/mongodb/mongod.pid # File size limits LimitFSIZE=infinity # CPU time limits LimitCPU=infinity # Virtual memory limits LimitAS=infinity # Open files limit LimitNOFILE=64000 # Processes/threads limit LimitNPROC=64000 # Locked memory limit LimitMEMLOCK=infinity # Total threads limit TasksMax=infinity TasksAccounting=false [Install] WantedBy=multi-user.target - Save and close the file (press
Ctrl+O, hit Enter, thenCtrl+X). - Reload systemd to recognize the new service:
sudo systemctl daemon-reload - Enable MongoDB to start on system boot:
sudo systemctl enable mongod - Start the service and check its status:
You should see "active (running)" in the output if everything works.sudo systemctl start mongod sudo systemctl status mongod
4. Fix mongod: command not found & mongo: command not found
These errors mean the MongoDB binaries aren’t in your system PATH. Let’s confirm they exist first:
ls /usr/bin | grep mongo
You should see mongod, mongo, mongos, and other related binaries listed. If they exist but aren’t accessible:
- Add
/usr/binto your PATH permanently by editing your bashrc:echo 'export PATH=$PATH:/usr/bin' >> ~/.bashrc - Reload your bashrc to apply the change immediately:
source ~/.bashrc - Test it with:
mongod --version mongo --version
5. Verify MongoDB Connection
Once the service is running, test connecting to the MongoDB shell:
mongo
Or specify the localhost address explicitly if needed:
mongo --host 127.0.0.1:27017
You should see the MongoDB shell prompt (>) if the connection succeeds.
Quick Note on AWS Security Groups
If you plan to connect to MongoDB from outside your EC2 instance, make sure your AWS security group allows inbound traffic on port 27017. For security, restrict this rule to only your local IP address instead of opening it to the world.
内容的提问来源于stack exchange,提问作者Шоира Ташпулатова

