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

AWS EC2 Ubuntu环境MongoDB安装后无法启动求助

Troubleshooting MongoDB Installation & Startup Issues on AWS EC2 Ubuntu 16.04

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:
    echo $PATH
    
    Look for /usr/bin and /usr/sbin—if they’re missing, temporarily add them with:
    export PATH=$PATH:/usr/bin:/usr/sbin
    
  • Try running apt-get --version to 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):

  1. Import MongoDB’s official GPG key:
    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
    
  2. 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
    
  3. Update your apt package index:
    sudo apt-get update
    
  4. 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:

  1. Create the service file with nano:
    sudo nano /etc/systemd/system/mongod.service
    
  2. 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
    
  3. Save and close the file (press Ctrl+O, hit Enter, then Ctrl+X).
  4. Reload systemd to recognize the new service:
    sudo systemctl daemon-reload
    
  5. Enable MongoDB to start on system boot:
    sudo systemctl enable mongod
    
  6. Start the service and check its status:
    sudo systemctl start mongod
    sudo systemctl status mongod
    
    You should see "active (running)" in the output if everything works.

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/bin to 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,提问作者Шоира Ташпулатова

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 15:22:58