1.在MongoDB官网下载安装包:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.5.tgz
linux下载方式:
cd /usr/opt/
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.5.tgz
tar -zxvf mongodb-linux-x86_64-rhel70-3.6.5.tgz
mv mongodb-linux-x86_64-rhel70-3.6.5 mongodb
2.将文件上传到linux系统文件夹:/usr/opt 解压,解压后删除压缩包节ls省空间。将解压后的文件名重命名:
3.进入mongodb目录:
cd /usr/opt/mongodb
mkdir data
mkdir logs
4.进入bin目录:
cd /usr/opt/mongodb/bin
vi mongodb.conf
添加代码:
systemLog:
destination: file
path: "/usr/opt/mongodb/logs/mongodb.log"
logAppend: true
storage:
dbPath: "/usr/opt/mongodb/data/"
journal:
enabled: true
processManagement:
fork: true
pidFilePath: "/usr/opt/mongodb/mongodb.pid"
net:
port: 27017
bindIp: 0.0.0.0
setParameter:
enableLocalhostAuthBypass: false
5.创建mongodb启动脚本 vi /etc/init.d/mongodb
#!/bin/sh
#chkconfig: 2345 80 90
#description: mongodb
start() {
/usr/opt/mongodb/bin/mongod -f /usr/opt/mongodb/bin/mongodb.conf
}
stop() {
/usr/opt/mongodb/bin/mongod -f /usr/opt/mongodb/bin/mongodb.conf --shutdown
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
给脚本权限:chmod +x /etc/init.d/mongodb
将mongodb设置开机自启:chkconfig --level 35 mongodb on
将mongodb注册为系统服务:chkconfig --add mongodb
启动mongodb服务:/etc/init.d/mongodb start
停止mongodb服务:/etc/init.d/mongodb stop
重启mongodb服务:/etc/init.d/mongodb restart
开启端口:
firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --zone=public --add-port=27017/udp --permanent
firewall-cmd --reload
编辑环境变量:vi /etc/profile
末尾添加:export PATH=$PATH:/usr/opt/mongodb/bin
执行环境变量:source /etc/profile
通过命令:mongo进入mongodb
添加用户:db.createUser({user:"root",pwd:"123456",roles:[{role:"root",db:"admin"}]})
开启认证:
先停止服务:/etc/init.d/mongodb stop
编辑:vi /usr/opt/mongodb/bin/mongodb.conf 添加代码
security:
authorization: enabled
开启服务:/etc/init.d/mongodb start
连接mongodb:mongo -u 用户名-p 密码 数据库