您的位置:首页 > 数据库 > Mongodb

MongoDB 3.2.7安装及用户角色配置

2016-12-12 17:00 295 查看

转载自http://blog.csdn.net/yuechang5/article/details/51966125

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
1
[/code]

1.3、指定安装目录

mv mongodb-linux-x86_64-enterprise-rhel62-3.2.7 /home/martin/
1
1
[/code]
并重命名为mongodb

1.4、新建目录

新建MongoDB数据文件存放目录
mkdir -p /home/martin/mongodb/db
1
1
[/code]
新建log文件存放目录
mkdir -p /home/martin/mongodb/logs
1
1
[/code]

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
[/code]

1.6、启动MongoDB服务器

使用配置文件mongod.conf定义的参数启动
/home/martin/mongodb/bin/mongod --config /home/martin/mongodb/bin/mongod.conf
1
1
[/code]

1.7、配置开机启动

vi /etc/rc.d/rc.local

在文件末端加入:
/home/martin/mongodb/bin/mongod --config /home/martin/mongodb/bin/mongod.conf
1
1
[/code]

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

安装防火墙服务
yum install iptables services
1
1
[/code]
防火墙开启27017端口

打开配置文件
vi /etc/sysconfig/iptables
1
1
[/code]
加入如下语句
-A INPUT -p tcp -m state --state NEW -m tcp --dport 27107 -j ACCEPT
1
1
[/code]
重启防火墙
service iptables start
1
1
[/code]

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
1
1
[/code]


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"}]
}
)
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
[/code]


2.2.3、修改mongod.conf文件

在配置文件中增加如下配置
security:
authorization: enabled //启用授权
1
2
1
2
[/code]

2.2.4、重启MongoDB服务器

service mongod restart
1
1
[/code]

2.2.5、创建数据库读写权限用户

use admin
db.auth("admin"," password");
use ballmatch
db.createUser({
user: "football",
pwd: "password",
roles: [{role: "readWrite",db: "ballmatch"}]
})
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
[/code]


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");
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
[/code]

3、命令参考

3.1、修改用户密码

db.updateUser( "admin",{pwd:"password"});
1
1
[/code]

3.2、密码认证

db.auth("admin","password");
1
1
[/code]

3.3、MongoDB连接信息查询

db.serverStatus().connections;
1
1
[/code]
MongoDB实例接受的最多连接数,如果高于操作系统接受的最大线程数,设置无效。net.maxIncomingConnections默认(65536)

3.4、关闭MongoDB服务

use admin;
db.shutdownServer();
1
2
1
2
[/code]

3.5、删除用户

删除用户(需要root权限,会将所有数据库中的football用户删除)
db.system.users.remove({user:"football"});
1
1
[/code]
删除用户(权限要求没有那么高,只删除本数据中的football用户)
db.dropUser("football");
1


1
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: