1.添加 yum repo
vi /etc/yum.repos.d/mongodb-org-4.0.repo
添加如下内容
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
保存退出
2.查看 repolist
yum repolist all|grep mongodb
3.yum安装
yum install -y mongodb-org
4.启动/查看服务
systemctl start mongod.service
systemctl status mongod.service
5.查看端口状态
netstat -ntlp
6.运行
mongo --host 127.0.0.1:27017
db
show dbs
7.配置远程连接
编辑 mongod.conf 配置
cp /etc/mongod.conf /etc/mongod.conf.bak
vi /etc/mongod.conf
将 bindIp: 127.0.0.1 改成 bindIp: 0.0.0.0,注意 tab 和空格对齐
开放端口
firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --query-port=27017/tcp
重启服务
systemctl restart mongod.service
8.设置登录认证
创建管理员用户
use admin
db.createUser(
{
user: "user",
pwd: "pwd",
roles: [ { role: "root", db: "admin" } ]
}
)
创建普通用户
use demo
db.createUser(
{
user: "user",
pwd: "pwd",
roles: [ { role: "readWrite", db: "demo" } ]
}
)
创建访客
use demo
db.createUser(
{
user: "guest",
pwd: "pwd",
roles: [ { role: "read", db: "demo" } ]
}
)
编辑 mongod.conf 配置,开启认证
vi /etc/mongod.conf
找到 #security: ,改成如下开启安全认证:
security:
authorization: enabled
重启服务
systemctl restart mongod.service
参考文章:
https://blog.csdn.net/u010279498/article/details/89556660 yum安装及配置
https://blog.csdn.net/jianleking/article/details/79715097 开启安全认证
https://www.cnblogs.com/niwotaxuexiba/p/10642202.html 开启安全认证