通过php在没有管理员权限的情况下复制mongodb中的集合

时间:2015-04-21 13:23:32

标签: php mongodb

我试图让没有管理员权限的mongo用户在php应用程序中执行集合副本。我知道db.source.copyTo(target)需要管理员权限,而我使用了db.getCollection(coll).find().forEach( function(d) {db.getCollection(coll2).insert(d)});,而且它可以在mongo shell中找到它。但是在php中,我使用同一用户的mongo :: execute方法来运行这个脚本并返回{ "ok" : 0, "errmsg" : "unauthorized" }

$db->execute('function (coll, coll2) { return db.getCollection(coll).find().forEach( function(d) {db.getCollection(coll2).insert(d)});}', array( $from, $to ));

我做了一些搜索并发现它是因为php“execute”的底层执行也使用了需要管理员权限的“eval”。在PHP中有没有其他方法可以做mongo集合副本?

由于

1 个答案:

答案 0 :(得分:0)

我使用shell_exec来执行此shell命令来实现此功能。

$ cmd =" xxx / bin / mongo ip:port /' db' -u' uuu' -p' ppp' --eval \" db.getCollection(' hello')。find()。forEach(function(d){db.getCollection(' hello_copy')。insert(d )}); \"&#34 ;;

$了shell_exec($ CMD);

但是在许多生产环境中,php.ini禁用了shell_exec。如果是这样,你需要在php.ini上启用shell_exec并确保shell命令从恶意攻击中逃脱。