- MongoDB 下载链接:
- centos7 镜像下载链接:
- 也可以在服务器上面:wget
1.创建基础目录(已经有的话,请跳过)
# 用来存储我们下载好的一些文件或者软件 mkdir -p /home/service # 用来存储配置文件 mkdir -p /home/config # 用来存储日志 mkdir -p /home/logs # sh脚本 mkdir -p /home/sh
安装wget:yum install wget
2. 下载解压mongo(复制全部,粘贴运行就好了)
# 没有wget 的话,yum install wget cd /home/service wget tar -xzvf mongodb-linux-x86_64-r -C /home/service/ mv /home/service /home/service/mongodb
3. 编写mongo配置(复制全部,粘贴运行就好了)
mkdir -p /home/service/mongodb/myconfig ln -s /home/service/mongodb/myconfig /home/config/mongodb cat <<'EOF'> /home/service/mongodb/myconfig dbpath = /home/service/mongodb/data/db logpath = /home/service/mongodb/data/log port = 27017 # 测试下可以0.0.0.0,正式环境:127.0.0.1 bind_ip=0.0.0.0 fork = true #nohttpinterface = true auth=true EOF # 软链 ln -s /home/service/mongodb/data/logs /home/logs/mongodb
4. 设置mongo自启文件(复制全部,粘贴运行就好了)
mkdir -p /home/service/mongodb/data/db mkdir -p /home/service/mongodb/data/logs cat <<'EOF'> /home/service/mongodb/myconfig [Unit] Description=mongodb After=ne remo n [Service] Type=forking ExecStart=/home/service/mongodb/bin/mongod --config /home/service/mongodb/myconfig ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/home/service/mongodb/bin/mongod --shutdown --config /home/service/mongodb/myconfig PrivateTmp=true [Install] WantedBy=mul EOF cp /home/service/mongodb/myconfig /lib/systemd/system systemctl enable mongodb.service systemctl start mongodb.service service mongodb status
5. 配置系统环境(复制全部,粘贴运行就好了)
cat <<'EOF'>> /etc/profile export MONGODB_HOME=/home/service/mongodb export PATH=$PATH:$MONGODB_HOME/bin EOF source /etc/profile
6.创建MongoDB管理员
mongo use admin #创建mongo管理员,并且赋予权限 db.createUser({ user: 'admin', pwd: 'mongoAdmin', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] }); #退出登录 exit
7. 防火墙开启27017端口(复制全部,粘贴运行就好了)
yum -y install firewalld systemctl start chkconfig --level 35 firewalld on firewall-cmd --zone=public --add-port=27017/tcp --permanent firewall-cmd --reload firewall-cmd --list-ports
8. 测试访问