linux(Centos7)下mongodb 安装与设置用户权限

一、安装
1、使用 yum 安装 ;配置 MongoDB 添加repo

vim /etc/yum.repos.d/mongodb-org-3.6.repo
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
yum install -y mongodb-org

2、开放端口

firewall-cmd --permanent --add-port=27017/tcp

2.1 重启防火墙

service firewalld restart

3、MongoDB 基本配置,根据使用者情况配置

vim /etc/mongod.conf
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
 timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.

二、创建数据库权限用户

启动MongoDB

sudo service mongod start

进入mongodb的安装目录的bin下

./mongo

linux(Centos7)下mongodb 安装与设置用户权限_第1张图片

use admin
db.createUser({user:'admin',pwd:'password',roles:[{role:'readWriteAnyDatabase',db:'admin'}]})

创建指定数据库testdb的用户,并且分配权限

use testdb

db.createUser({user:'test',pwd:'test123',roles:[{role:'readWrite',db:'testdb'}]})

db.auth('test', 'test123') 

用户与数据库创建好后 重启MongoDB

重启命令

sudo service mongod restart

停止MongoDB

sudo service mongod stop

设置开机启动

sudo chkconfig mongod on

启动MongoDB

sudo service mongod start

连接MongoDB

mongo 172.16.0.13:27017/admin -u admin -p password

linux(Centos7)下mongodb 安装与设置用户权限_第2张图片

删除用户

db.dropUser(“admin”)

修改密码

db.changeUserPassword('user','new password');  

忘记数据库密码 重置

关闭MongoDB服务

mongod --config /etc/mongodb.conf -shutdown

执行非权限认证

mongod --config /etc/mongodb.conf 
use admin
show collections

再进行用户的权限设置

你可能感兴趣的:(Linux)