如题,php mongodb扩展装好了。
$m = new MongoClient();//默认连接本地localhost
$db = $m->mlab;
$collection = $db->whois;
这样是没问题的。问题出在远程连接别的机器上的数据库,代码如下:
try{
$m = new MongoClient("10.100.0.159:27017");
$db = $m->smaap;
$db-> authenticate("xxxxx","xxxxx");//密码和用户名
}
catch (Exception $e){
echo $e->getMessage();
exit();
}
try{
$collection = $db->ott_video_dns;
$cursor = $collection->find();
}
catch (Exception $e){
echo $e->getMessage();
exit();
}
try{
foreach ($cursor as $id => $value) {
echo "$id: "; var_dump($value); echo "<br>";
}
}
catch (Exception $e){
echo $e->getMessage();
exit();
}
这样会报错:
10.100.0.159:27017: not authorized for query on smaap.ott_video_dns
但是用同样用户名和密码用python脚本去跑,能访问数据库,也没问题。本机和数据库所在的机器在同一个网段,用studio 3T也能访问。
求大神帮忙看一下,问题出在什么地方?
<?php //è¿ééç¨é»è®¤è¿æ¥æ¬æºç27017端å£ï¼å½ç¶ä½ ä¹å¯ä»¥è¿æ¥è¿ç¨ä¸»æºå¦192.168.0.4:27017,å¦æ端å£æ¯27017ï¼ç«¯å£å¯ä»¥çç¥ $m = new Mongo(); // éæ©comedyæ°æ®åºï¼å¦æ以å没该æ°æ®åºä¼èªå¨å建ï¼ä¹å¯ä»¥ç¨$m->selectDB("comedy"); $db = $m->comedy; //éæ©comedyéé¢çcollectionéåï¼ç¸å½äºRDBMSéé¢ç表ï¼ä¹-å¯ä»¥ä½¿ç¨ $collection = $db->collection; $db->selectCollection("collection"); //æ·»å ä¸ä¸ªå
ç´ $obj = array( "title" => "Calvin and Hobbes-".date('i:s'), "author" => "Bill Watterson" ); //å°$obj æ·»å å°$collection éåä¸ $collection->insert($obj); //æ·»å å¦ä¸ä¸ªå
ç´ $obj = array( "title" => "XKCD-".date('i:s'), "online" => true ); $collection->insert($obj); //æ¥è¯¢ææçè®°å½ $cursor = $collection->find(); //éåææéåä¸çææ¡£ foreach ($cursor as $obj) { echo $obj["title"] . "<br />\n"; } //å é¤æææ°æ® //$collection->remove(); //å é¤ name 为hm //$collection->remove(array('name'=>'hm')); //æå¼MongoDBè¿æ¥ $m->close(); ?>
ä½ å¯ä»¥å»åç¾äººå¹³å°ççï¼éé¢çä¸è¥¿ä¸é
温馨提示:答案为网友推荐,仅供参考
相关了解……