`
alfred_long
  • 浏览: 559128 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

mongodb访问权限设置及PHP连接

阅读更多

默认mongodb链接,是可以访问任何数据的.

如果是在生产环境中,这样做必然导致很多问题,通过尝试,和查找资料,整理如下(参考http://www.php.net/manual/en/mongo.connecting.php):

 

常用命令:

 

show dbs     相当于   show databases

 

show collections  相当于  show tables 

 

use  用法相同

 

在添加用户以后每个数据库中都会有

 

system.indexes

system.users

但是,admin中设置的user为超级管理员,可以查看其他的数据库(必须在use admin 数据库后,登录,可以查看其他数据库的数据。直接use test数据库,用超级管理员是无法登录的)

单独的数据库也可以设置单独的访问用户;具体如下

 

 

 

1 可以设置超级管理员(对所有的数据库都有权限):

 

>show dbs
##看到有如下数据
admin	(empty)
comment	0.203125GB
dbtest	(empty)
foo	0.203125GB
local	(empty)
test	0.203125GB
>use admin
switched to db admin
> db.addUser('admin','12345678') ##添加用户
Mon Nov  5 23:40:00 [FileAllocator] allocating new datafile /data/db/admin.ns, filling with zeroes...
{
	"user" : "admin",
	"readOnly" : false,
	"pwd" : "89e41c6c28d88d42c21fe501d82969ea",
	"_id" : ObjectId("5097ddd00342c63efff3fbfb")
}
##之后运行
>showdbs 
Mon Nov  5 23:45:13 uncaught exception: listDatabases failed:{ "errmsg" : "need to login", "ok" : 0 } ##提示需要登录

添加--auth 启动
./mongod -auth
./mongo 

>use admin
switched to db admin
> db.auth('admin','12345678') ##用添加的账户密码查看
Mon Nov  5 23:49:32 [conn56]  authenticate db: admin { authenticate: 1, nonce: "304f5242601fafa4", user: "admin", key: "58260df384b1146466efca5c90a5ff05" }
1
#1 说明登录成功
> show dbs
admin	0.203125GB
comment	0.203125GB
dbtest	(empty)
foo	0.203125GB
local	(empty)
test	0.203125GB
> use admin
switched to db admin
> show collections;
system.indexes
system.users
> db.system.users.find() ##查找数据
{ "_id" : ObjectId("5097ddd00342c63efff3fbfb"), "user" : "admin", "readOnly" : false, "pwd" : "89e41c6c28d88d42c21fe501d82969ea" }
 

 

2 对某个数据库设置用户权限:

 

###设置了用户和密码以后只能用用户名和密码登录
[root@melon bin]# ./mongo
MongoDB shell version: 2.2.1
connecting to: test
> use admin
switched to db admin
> db.auth('admin','12345678')
1
##使用新的数据库
>use melon ##没有会自动建立
> use melon
switched to db melon
> db.addUser('melon','melon') ##添加新用户和密码
{
	"user" : "melon",
	"readOnly" : false,
	"pwd" : "88e56de28b76bea44329bb61b0832e2f",
	"_id" : ObjectId("5097e906931f14f0bedfde75")
}
> show collections
system.indexes
system.users
>exit
##重新用melon登录

[root@melon bin]# ./mongo
MongoDB shell version: 2.2.1
connecting to: test
> use melon
switched to db melon
> show collections
Tue Nov  6 00:29:48 uncaught exception: error: {
	"$err" : "unauthorized db:melon ns:melon.system.namespaces lock type:0 client:127.0.0.1",
	"code" : 10057
}
##发现无法访问
> db.auth('admin','12345678')
Error: { errmsg: "auth fails", ok: 0.0 }
0 ##超级用户访问也不成
> db.auth('melon','melon')
1  ##单独设置的用户访问成功
> show collections
system.indexes
system.users  
##使用超级用户访问 melon
> use admin
switched to db admin
> db.auth('admin','12345678')
1
> use melon
switched to db melon
> show collections
system.indexes
system.users
##必须先用admin数据库登录,登录成功以后才可以访问melon
  

 在PHP中访问方式

 

 

<?php
##1 使用超级用户连接mongodb
/*mongodb连接*/
$m = new Mongo("mongodb://admin:12345678@192.168.138.35:27017");
/*选择melon数据库*/
$db = $m->melon;
/*集合*/
$collection = melonco;
/*选择数据库里面的集合,相当于表*/
$collection = $db->$collection;
$array = array('name'=>'melon','age'=>'24','sex'=>'Male','birth'=>array('year'=>'1988','month'=>'07','day'=>'13'));
$collection->insert($array);
$cursor = $collection->find();
foreach ($cursor as $id => $value) {
    echo "$id: "; var_dump($value); echo "<br>";     
}


###2 使用数据库用户
/*mongodb连接*/
$m = new Mongo("192.168.138.35:27017");
/*选择comment*/
$db = $m->melon;
/*连接数据库*/
$db->authenticate("melon", "melon");
/*选择t数据库里面集合,相当于表*/
$collection = $db->melonco;
$array = array('name'=>'melon_son','age'=>'0','sex'=>'Male','birth'=>array('year'=>'201X','month'=>'07','day'=>'13'));
$collection->insert($array);
$cursor = $collection->find();

foreach ($cursor as $id => $value) {
    echo "$id: "; var_dump($value); echo "<br>";     
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics