MongoDB Deployment (Single Node)
-
Download mongodb installation package
- Internet Access Available
- Internet Access Unavailable
wget https://pdpublic.nocoly.com/offline/common/mongodb-linux-x86_64-3.4.24.tgz
# Download link for the mongodb installation package. Once downloaded, upload it to the deployment server.
https://pdpublic.nocoly.com/offline/common/mongodb-linux-x86_64-3.4.24.tgz -
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 -
Create a mongodb user
useradd -M -s /sbin/nologin mongodb
-
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 -
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 -
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
-
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
-
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.
- The password for the root user of the mongodb admin specified in the command is
-
Shut down the temporarily started MongoDB
kill $(pgrep -f 'mongod')
Start MongoDB
systemctl start mongodb