- 下载MongoDB5.0.4
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/5.0/x86_64/RPMS/mongodb-org-server-5.0.4-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/5.0/x86_64/RPMS/mongodb-org-shell-5.0.4-1.el7.x86_64.rpm
- 如果遇到如下错误(非必须步骤)
wget: command not found
需要执行命令先安装wget
yum -y install wget
然后再下载MongoDB5.0.4
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/5.0/x86_64/RPMS/mongodb-org-server-5.0.4-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/5.0/x86_64/RPMS/mongodb-org-shell-5.0.4-1.el7.x86_64.rpm
- 安装MongoDB
rpm -ivh mongodb-org-server-5.0.4-1.el7.x86_64.rpm
rpm -ivh mongodb-org-shell-5.0.4-1.el7.x86_64.rpm
- 启动MongoDB服务
systemctl start mongod
- 设置账号密码
- 开启认证
/usr/bin/mongod --auth
- 连接
mongo --port 27017
- 进入Mongo命令行后操作,切换admin库,创建账号
use admin
db.createUser({user:"admin",pwd:"password",roles:["root"]})
- 进入Mongo命令行后操作,认证登录
db.auth("admin", "password")
- 开启远程访问
- 打开mongod.conf文件
vim /etc/mongod.conf
- 修改bindIp配置后保存
bindIp = 0.0.0.0
- 重启Mongo服务
systemctl restart mongod
- 重启Mongo服务时遇到mongodb-27017.sock文件未授权错误,删除文件再重启服务(非必须步骤)
rm -rf /tmp/mongodb-27017.sock
- 添加防火墙端口号
- 添加端口号
firewall-cmd --zone=public --add-port=27017/tcp --permanent
- 重载防火墙立即生效
firewall-cmd --reload