原文: https://www.cnblogs.com/along21/p/10189072.html#auto_id_13
--------------------------------------------------------------
2.1 下载MongoDB
MongoDB 提供了 linux 各发行版本 64 位的安装包,你可以在官网下载安装包。
下载地址:https://www.mongodb.com/download-center#community,选择自己需要的版本
下载完安装包,并解压 tgz(以下演示的是 64 位 Linux上的安装)
(1)解压
[root@along ~]# cd /usr/local/ [root@along local]# tar -xzvf mongodb-linux-x86_64-4.0.5.tgz
(2)创建软连接,方便以后更换版本
[root@along local]# ln -s mongodb-linux-x86_64-4.0.5 mongodb
(3)配置环境变量
[root@along local]# vim /etc/profile.d/mongodb.sh
[root@along local]# cat /etc/profile.d/mongodb.sh
export MONGODB_HOME=/usr/local/mongodb export PATH=$PATH:$MONGODB_HOME/bin
[root@along local]# source /etc/profile.d/mongodb.sh
2.2 编写配置文件
(1)准备MongoDB所需的目录
[root@along ~]# cd /usr/local/mongodb
[root@along mongodb]# mkdir etc data log
[root@along mongodb]# touch log/mongod.log
(2)编写配置文件,借鉴gitlab
[root@along mongodb]# vim etc/mongodb.conf
# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # where to write logging data. systemLog: destination: file logAppend: true path: /usr/local/mongodb/mongod.log # Where and how to store data. storage: dbPath: /usr/local/mongodb/data journal: enabled: true # engine: # wiredTiger: # how the process runs processManagement: fork: true # fork and run in background pidFilePath: /usr/local/mongodb/mongod.pid # location of pidfile #timeZoneInfo: /usr/share/zoneinfo # network interfaces net: port: 27017 bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting. security: authorization: enabled
2.3 启动MongoDB
(1)使用命令启动MongoDB
[root@along mongodb]# mongod -f etc/mongodb.conf
[root@along mongodb]# mongod --shutdown -f /etc/mongodb.conf ---关闭服务命令
[root@along mongodb]# ss -nutlp |grep 27017
tcp LISTEN 0 128 *:27017 *:* users:(("mongod",pid=12961,fd=11))
(2)将MongoDB设为开机自启
[root@along ~]# vim /etc/init.d/mongodb
#!/bin/bash export MONGO_HOME=/usr/local/mongodb #chkconfig:2345 20 90 #description:mongod #processname:mongod case $1 in start) $MONGO_HOME/bin/mongod --config $MONGO_HOME/etc/mongodb.conf ;; stop) $MONGO_HOME/bin/mongod --shutdown --config $MONGO_HOME/etc/mongodb.conf ;; status) ps -ef | grep mongod ;; restart) $MONGO_HOME/bin/mongod --shutdown --config $MONGO_HOME/etc/mongodb.conf $MONGO_HOME/bin/mongod --config $MONGO_HOME/etc/mongodb.conf ;; *) echo "require start|stop|status|restart" ;; esac
[root@along ~]# chmod +x /etc/init.d/mongodb
[root@along ~]# chkconfig --add /etc/init.d/mongodb
可以使用systemctl 启动关闭MongoDB服务器
[root@along ~]# systemctl start mongodb
[root@along ~]# systemctl stop mongodb