Skip to main content

MongoDB Deployment (Single Node)

Start Deployment

  1. Download the MongoDB installation package and unzip it to the installation directory

    wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.29.tgz
    tar -zxvf mongodb-linux-x86_64-rhel70-4.4.29.tgz
    mv mongodb-linux-x86_64-rhel70-4.4.29 /usr/local/mongodb
  2. Create MongoDB user

    useradd -M -s /sbin/nologin mongodb
  3. Create data and log directories and assign permissions

    mkdir -p /data/mongodb/ /data/logs/mongodb
    chown -R mongodb:mongodb /usr/local/mongodb/ /data/mongodb/ /data/logs
  4. Configure the systemd management file

    cat > /etc/systemd/system/mongodb.service <<EOF
    [Unit]
    Description=MongoDB
    [Service]
    User=mongodb
    Group=mongodb
    LimitNOFILE=1000000
    LimitNPROC=1000000
    ExecStart=/usr/local/mongodb/bin/mongod --logpath /data/logs/mongodb/mongodb.log --dbpath /data/mongodb --auth --port 27017 --bind_ip 0.0.0.0
    ExecStop=/usr/bin/kill \$MAINPID
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
  5. Auto-start on boot

    # After installation, no user has been created yet, so don't use systemctl start mongodb to start the service first
    systemctl daemon-reload
    systemctl enable mongodb

Create Database User

  1. Temporarily start a mongodb service without connection authentication enabled

    su -c '/usr/local/mongodb/bin/mongod --fork --logpath /usr/local/mongodb/mongodb.log --dbpath /data/mongodb --noauth  --port 27017' -s /bin/bash mongodb
  2. Create a user

    /usr/local/mongodb/bin/mongo <<<'use admin
    db.createUser({user:"root",pwd:"bxfC5J3HuYaY",roles:[{role:"root",db:"admin"}]})'
    • The root user specified in the command is the administrator of MongoDB, with the password bxfC5J3HuYaY. Please replace it with the actual password during deployment.
  3. Shut down the temporarily started MongoDB

    kill $(pgrep -f 'mongod')

Start MongoDB

systemctl start mongodb