- MongoDB 下载链接:https://www.mongodb.com/try/download/community
- centos7 镜像下载链接:http://mirrors.aliyun.com/centos/7/isos/x86_64/
- 也可以在服务器上面:wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.0.tgz
- centos7两行命令部署(mysql+redis+nginx+jdk+mongodb):https://blog.csdn.net/itjavaee/article/details/108360851
1.创建基础目录(已经有的话,请跳过)
# 用来存储我们下载好的一些文件或者软件
mkdir -p /home/service
# 用来存储配置文件
mkdir -p /home/config
# 用来存储日志
mkdir -p /home/logs
# sh脚本
mkdir -p /home/sh
2. 下载解压mongo(复制全部,粘贴运行就好了)
# 没有wget 的话,yum install -y wget
yum install -y wget
cd /home/service
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.0.tgz
tar -xzvf mongodb-linux-x86_64-rhel70-4.4.0.tgz -C /home/service/
mv /home/service/mongodb-linux-x86_64-rhel70-4.4.0 /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/mongodb.conf
dbpath = /home/service/mongodb/data/db
logpath = /home/service/mongodb/data/logs/mongodb.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/mongodb.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/home/service/mongodb/bin/mongod --config /home/service/mongodb/myconfig/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/home/service/mongodb/bin/mongod --shutdown --config /home/service/mongodb/myconfig/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
cp /home/service/mongodb/myconfig/mongodb.service /lib/systemd/system/mongodb.service
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 firewalld.service
chkconfig --level 35 firewalld on
firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-ports
8. 测试访问