1.下載mongodb安裝版本至linux服務器,我這邊是/usr/local/src下面
2.解壓安裝包,將安裝包里面的內容移動至/usr/local/mongodb下面
# tar zxvf mongodb-linux-x86_64-rhel70-4.2.0.tgz # mkdir /usr/local/mongodb # mv mongodb-linux-x86_64-rhel70-4.2.0/* /usr/local/mongodb/
3.切換至/usr/local/mongodb下,新建data文件夾,log文件夾
# cd mongodb/ # mkdir data # mkdir log
4.vim編輯mongodb.conf文件
bind_ip=0.0.0.0 port=27017 dbpath=/usr/local/mongodb/data/ logpath=/usr/local/mongodb/log/mongodb.log pidfilepath =/usr/local/mongodb/run/mongodb.pid logappend=true fork=true journal=true maxConns=2000 auth = true
5.執行vim /etc/profile命令后按i鍵編輯,在文件末尾加上如下配置
export PATH=/usr/local/mongodb/bin:$PATH
保存退出后執行:source /etc/profile 重啟配置文件
6.設置mongodb.service啟動服務
cd /lib/systemd/system
vi mongodb.service
[Unit] Description=MongoDB Database Server Documentation=https://docs.mongodb.org/manual After=network.target remote-fs.target nss-lookup.target [Service] Type=forking User=root Group=root ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/mongodb.conf ExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /usr/local/mongodb/mongodb.conf [Install] WantedBy=multi-user.target
mongodb.service服務權限修改
chmod 754 mongodb.service
系統mongodb.service操作命令
#啟動服務
systemctl start mongodb.service
#關閉服務
systemctl stop mongodb.service
#開機啟動
systemctl enable mongodb.service