Skip to main content

MongoDB Deployment (Single Node)

  1. Download mongodb installation package

    wget https://pdpublic.mingdao.com/private-deployment/offline/common/mongodb-linux-x86_64-3.4.24.tgz
  2. Unzip to the installation directory

    tar -zxvf mongodb-linux-x86_64-3.4.24.tgz
    mv mongodb-linux-x86_64-3.4.24 /usr/local/mongodb
  3. Create a mongodb user

    useradd -M -s /sbin/nologin mongodb
  4. Create data and log directories and set permissions

    mkdir -p /data/mongodb/ /data/logs/mongodb
    chown -R mongodb.mongodb /usr/local/mongodb/ /data/mongodb/ /data/logs/mongodb
  5. 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
    ExecStop=/usr/bin/kill \$MAINPID
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
  6. Auto-start on power up

    # No users have been created after the installation, so don't start the service with systemctl start mongodb yet
    systemctl daemon-reload
    systemctl enable mongodb

Create Database User

  1. Temporarily start a mongodb service without connection authentication turned on

    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 user

    /usr/local/mongodb/bin/mongo <<<'use admin
    db.createUser({user:"root",pwd:"123456",roles:[{role:"root",db:"admin"}]})'
    • The password for the root user of the mongodb admin specified in the command is 123456, which needs to be replaced in the actual deployment.
  3. Shut down the temporarily started MongoDB

    kill $(pgrep -f 'mongod')

Start MongoDB

systemctl start mongodb