1)设置mongodb.service开机服务启动
cd /lib/systemd/system
cat >>mongodb.service<<"EOF"
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
然后设置mongodb.service权限
chmod +x mongodb.service
#启动服务
systemctl start mongodb.service
#停止服务
systemctl stop mongodb.service
#添加开机自启动
systemctl enable mongodb.service
#重启服务
systemctl restart mongodb.service
2)添加环境变量
1、直接用export命令:
export PATH=$PATH:/usr/local/mongodb/bin
2、修改profile文件:
cat >>/etc/profile<<"EOF"
export PATH="$PATH:/usr/local/mongodb/bin"
EOF
#刷新profile文件:
source /etc/profile
3、 修改.bashrc文件:
cat >>/root/.bashrc<<"EOF"
export PATH="$PATH:/usr/local/mongodb/bin"
EOF
上述三步依次执行完毕,环境变量配置完成!!!!
重启服务器,输入mongo 回车就可以了