#校驗依賴 num=`ldd /home/mongodb/bin/mongod|grep 'not found'|wc -l` if [ $num -nt 0 ]; then echo 'mongo缺少必要依賴,請確認:' echo `ldd /home/mongodb/bin/mongod|grep 'not found'` exit 1 fi # 資料存放目錄 sudo mkdir -p "/home/mongodb/data/db" # 日誌目錄 sudo mkdir -p "/home/mongodb/log/mongodb" sudo touch -c "/home/mongodb/log/mongodb/mongod.log" # 資料夾許可權 sudo chmod 755 "/home/mongodb/log/mongodb" "/home/mongodb/data/db" sudo chmod 755 -R "/home/mongodb/bin" # 設定配置檔案 cat << EOF > /etc/mongodb.conf storage: dbPath: /home/mongodb/data/db systemLog: destination: file logAppend: true path: /home/mongodb/log/mongodb/mongod.log net: port: 27019 bindIp: 0.0.0.0 processManagement: fork: true EOF # 設定執行服務 cat << EOF > /etc/systemd/system/mongod.service [Unit] Description=MongoDB Database Service After=network.target [Service] Type=forking ExecStart=/home/mongodb/bin/mongod --config /etc/mongodb.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/home/mongodb/bin/mongod --shutdown --config /etc/mongodb.conf Restart=always LimitNOFILE=64000 LimitNPROC=64000 [Install] WantedBy=multi-user.target EOF sudo systemctl daemon-reload sudo systemctl enable mongod.service sudo systemctl restart mongod.service