MongoDB 3.2.7安装及用户角色配置

1、MongDB安装步骤

1.1、下载安装包

环境信息:
Linux:CentOS release 6.5 (Final)
MongoDB:3.2.7
MongoDB下载地址:
http://downloads.10gen.com/linux/mongodb-linux-x86_64-enterprise-rhel62-3.2.7.tgz

1.2、解压

tar -zxvf mongodb-linux-x86_64-enterprise-rhel62-3.2.7.tgz

1.3、指定安装目录

mv mongodb-linux-x86_64-enterprise-rhel62-3.2.7 /home/martin/

并重命名为mongodb

1.4、新建目录

新建MongoDB数据文件存放目录

mkdir -p /home/martin/mongodb/db

新建log文件存放目录

mkdir -p /home/martin/mongodb/logs

1.5、新建配置文件

MongoDB支持把参数写进配置文件,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: /home/martin/mongodb/logs/mongod.log #日志文件存放目录

# Where and how to store data.
storage:
  dbPath: /home/martin/mongodb/db #数据文件存放目录
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  #以守护程序的方式启用,即在后台运行
  pidFilePath: /home/martin/mongodb/mongod.pid  # location of pidfile

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

#security:
  #authorization: enabled 
#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

1.6、启动MongoDB服务器

使用配置文件mongod.conf定义的参数启动

/home/martin/mongodb/bin/mongod --config /home/martin/mongodb/bin/mongod.conf

1.7、配置开机启动

vi /etc/rc.d/rc.local
在文件末端加入:

/home/martin/mongodb/bin/mongod --config /home/martin/mongodb/bin/mongod.conf

1.8、安装防火墙服务并开启端口

安装防火墙服务

yum install iptables services

防火墙开启27017端口
打开配置文件

vi /etc/sysconfig/iptables  

加入如下语句

-A INPUT -p tcp -m state --state NEW -m tcp --dport 27107 -j ACCEPT

重启防火墙

service iptables start

1.9、测试连接

1、通过xshell连接对应linux服务器,到MongoDB安装的bin目录运行./mongo命令
2、telnet ip 27017

2、 MongoDB用户角色配置

2.1、基本知识介绍

MongoDB基本的角色
1.数据库用户角色:read、readWrite;
2.数据库管理角色:dbAdmin、dbOwner、userAdmin;
3.集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
4.备份恢复角色:backup、restore;
5.所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
6.超级用户角色:root
//这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)
其中MongoDB默认是没有开启用户认证的,也就是说游客也拥有超级管理员的权限。userAdminAnyDatabase:有分配角色和用户的权限,但没有查写的权限

2.2、操作步骤

2.2.1、连接到MongoDB服务器
./mongo
2.2.2、创建root/admin用户
use admin
db.createUser({user:"root",pwd:"password",roles:["root"]})

db.createUser(  
  {  
    user: "admin",  
    pwd: "password",  
    roles: [{role: "userAdminAnyDatabase", db: "admin"}]  
  }  
)
2.2.3、修改mongod.conf文件

在配置文件中增加如下配置

security:
  authorization: enabled //启用授权
2.2.4、重启MongoDB服务器
service mongod restart
2.2.5、创建数据库读写权限用户
use admin
db.auth("admin"," password");
use ballmatch
db.createUser({
    user: "football",
    pwd: "password",
    roles: [{role: "readWrite",db: "ballmatch"}]
})
2.2.6、Java程序连接MongoDB服务器
        //方式一
        MongoCredential credential = MongoCredential.createCredential("username", "dbName", "password".toCharArray());
        ServerAddress serverAddress = new ServerAddress("192.168.10.242", 27017);
        MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(credential));
        DB db = mongoClient.getDB("dbName");
        return db;

        //方式二
        String sURI = String.format("mongodb://%s:%s@%s:%d/%s", "username", "password", "192.168.10.242", 27017, "dbName"); 
        MongoClientURI uri = new MongoClientURI(sURI); 
        MongoClient mongoClient = new MongoClient(uri); 
        DB db = mongoClient.getDB("dbName");

3、命令参考

3.1、修改用户密码

db.updateUser( "admin",{pwd:"password"});

3.2、密码认证

db.auth("admin","password");

3.3、MongoDB连接信息查询

db.serverStatus().connections;

MongoDB实例接受的最多连接数,如果高于操作系统接受的最大线程数,设置无效。net.maxIncomingConnections默认(65536)

3.4、关闭MongoDB服务

use admin;
db.shutdownServer();

3.5、删除用户

删除用户(需要root权限,会将所有数据库中的football用户删除)

db.system.users.remove({user:"football"});

删除用户(权限要求没有那么高,只删除本数据中的football用户)

db.dropUser("football");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值