MongoDB安装及添加用户并授权

MongoDB安装及添加用户并授权

一、MongoDB安装

1、配置yum源

vi /etc/yum.repos.d/mongodb.repo
[MongoDB]
name=MongoDB Repository
baseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=0
enabled=1

2、yum下载安装

yum install mongodb-org

3、启动mongod服务?

/etc/init.d/mongod restart

4、随机自启动

chkconfig mongod on

5、查看mongoDB版本

mongod --version

二、添加用户及授权

方法1:

安装完后
1、vi /etc/mongod.conf

security:
  authorization: 'disabled'

2、进入mongoDB

mongo 
或者
mongo –port 27017
use admin

3、执行这段指令,添加用户

db.createUser(
{
 user: "root",
 pwd: "password",
 roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
})

4、退出交互界面后

vi /etc/mongod.conf

security:
  authorization: 'enabled'

重启mongod服务

/etc/init.d/mongod restart

再进入mongoDB

mongo --port 27017 -u "root" -p "password" --authenticationDatabase "admin"

方法2

Mongodb 授权的流程:权限认证。添加nml用户并授权nml读写数据库events_db权限

use admin
db.auth('root','password')
use events_db
db.createUser({user:"nml",pwd:"123",roles:[{role:"readWrite",db:"events_db"}]})
# 能这样登录,即代表授权成功
mongo --port 27017 -u "nml" -p "123" --authenticationDatabase "events_db"
发布了38 篇原创文章 · 获赞 46 · 访问量 1408

猜你喜欢

转载自blog.csdn.net/weixin_45568892/article/details/105585136