下载解压 | wgethttps://repo.mongodb.org/yum/redhat/7/mongodb-org/6.0/x86_64/RPMS/mongodb-org-server-6.0.6-1.el7.x86_64.rpm sudorpm-ivhmongodb-org-server-6.0.6-1.el7.x86_64.rpm |
安装 MongoDB | sudo mkdir -p /usr/local/mongodb/data /usr/local/mongodb/log /usr/local/mongodb/bin |
创建 MongoDB 用户和组 | sudo groupadd mongod sudo useradd -r -g mongod -s /sbin/nologin mongod |
赋予权限 | sudo chown -R mongod:mongod /usr/local/mongodb |
配置 MongoDB | sudo vi /etc/mongod.conf |
systemLog: destination: file path: /usr/local/mongodb/log/mongod.log logAppend: true storage: dbPath: /usr/local/mongodb/data journal: enabled: true processManagement: fork: true net: bindIp: 0.0.0.0 port: 27017 #security: # authorization: enabled |
|
创建启动脚本 | vi /usr/local/mongodb/bin/start.sh |
#!/bin/bash # 设置MongoDB的启动命令 #MONGO_CMD="/usr/bin/mongod --config /etc/mongod.conf" #nohup /usr/bin/mongod --config /etc/mongod.conf > /dev/null 2>&1 & /usr/bin/mongod --config /etc/mongod.conf # 启动MongoDB echo "正在启动MongoDB..." $sleep 5 echo "MongoDB启动完成." #exit 0 |
|
sudo chmod +x /usr/local/mongodb/bin/start.sh | |
创建 systemd 服务文件 | sudo vi /etc/systemd/system/mongodb.service |
[Unit] Description=mongodb-server After=network.target [Service] Type=forking ExecStart=/usr/local/mongodb/bin/start.sh [Install] WantedBy=multi-user.target |
|
启动并设置开机启动 | sudo systemctl daemon-reload sudo systemctl start mongodb sudo systemctl enable mongodb sudo systemctl status mongodb |
重启服务 | sudo systemctl restart mongodb |
开放27017 端口 | sudo firewall-cmd --permanent --add-port=27017/tcp |
重新加载防火墙规则 | sudo firewall-cmd --reload |