1、准备环境
installDir=/opt && \
rm -rf ~/mongodb-linux-x86_64-rhel70-3.2.4.tgz && \
wget -P ~ https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.4.tgz && \
tar -zxf ~/mongodb-linux-x86_64-rhel70-3.2.4.tgz -C ${installDir} && \
echo "
export MONGODB_HOME=${installDir}/mongodb-linux-x86_64-rhel70-3.2.4
export PATH=\$PATH:\$MONGODB_HOME/bin
" >> /etc/profile && source /etc/profile
2、创建配置文件
mkdir -p ${MONGODB_HOME}/data/db ${MONGODB_HOME}/logs/ && \
touch ${MONGODB_HOME}/logs/mongodb.log && \
echo "
dbpath = ${MONGODB_HOME}/data/db
logpath = ${MONGODB_HOME}/logs/mongodb.log
port = 27017
fork = true
nohttpinterface = true
" >> ${MONGODB_HOME}/bin/mongodb.conf
3、开机启动并运行服务
echo "
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecReload=/bin/kill -s HUP $MAINPID
ExecStart=${MONGODB_HOME}/bin/mongod --config ${MONGODB_HOME}/bin/mongodb.conf
ExecStop=${MONGODB_HOME}/bin/mongod --shutdown --config ${MONGODB_HOME}/bin/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
" > /etc/systemd/system/mongodb.service && \
systemctl daemon-reload && \
systemctl enable mongodb && \
systemctl start mongodb && \
systemctl status mongodb