一. 下载安装包并解压至安装目录
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.10.tgz
tar -zxvf mongodb-linux-x86_64-rhel70-4.4.10.tgz -C /opt
mv /opt/mongodb-linux-x86_64-rhel70-4.4.10 /opt/mongodb-4.4.10
二. 创建mongodb用户、配置文件、数据和日志目录
groupadd mongodb
useradd -s /sbin/nologin -g mongodb -M mongodb
mkdir -p /opt/mongodb-4.4.10/{conf,data,logs}
三. 创建配置文件
vim /opt/mongodb-4.4.10/conf/mongodb.conf
net:
port: 27017
bindIp: 0.0.0.0
maxIncomingConnections: 65535
storage:
journal:
enabled: true
dbPath: "/opt/mongodb-4.4.10/data"
systemLog:
destination: file
path: "/opt/mongodb-4.4.10/logs/mongodb.log"
logAppend: true
processManagement:
fork: true
security:
authorization: disabled
四. 设置为系统服务开机自启
chown -R mongodb:mongodb /opt/mongodb-4.4.10
vim /usr/lib/systemd/system/mongodb.service
[Unit]
Description=mongodb
Documentation=https://docs.mongodb.org/manual
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
User=mongodb
Group=mongodb
LimitCORE=infinity
LimitNOFILE=655360
LimitNPROC=2056474
ExecStart=/opt/mongodb-4.4.10/bin/mongod -f /opt/mongodb-4.4.10/conf/mongodb.conf
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable mongodb
systemctl start mongodb
五. 添加环境变量并开启密码验证访问
echo "export PATH=/opt/mongodb-4.4.10/bin:$PATH" >> /etc/profile
source /etc/profile
mongo
use admin;
db.createUser(
{
user: "root",
pwd: "123456",
roles: [ { role: "root", db: "admin" } ]
}
);
vim /opt/mongodb-4.4.10/conf/mongodb.conf
将authorization: disabled改为authorization: enabled
systemctl restart mongodb