开发者

MongoDB giving weird Connection Error

I'm having a problem with connecting to MongoDB with PHP. This is my code:

<?php
$server = new Mongo('localhost:27017'); 
$db = $server->sampleDB;
$coll = $db->sample;
?>

This produces the following error:

Fatal error: Uncaught exception 'MongoConnectionException' with message 'localhost:27017: couldn't get host info for localhost' in /var/www/example/index.php:2 Stack trace: #0 /var/www/example/index.php(2): Mongo->__construct('localho开发者_JAVA技巧st:27017') #1 {main} thrown in /var/www/wexample/index.php on line 2


Found the problem; Turns out for some reason Mongo was only listening on 127.0.0.1, not localhost.

So, using $server = new Mongo("mongodb://127.0.0.1:27017");, instead of $server = new Mongo("mongodb://localhost:27017"); fixed the problem :)

Thanks for your help guys, James


Following the PHP Manual

couldn't get host info for [server]

This indicated that DNS could not resolve the server address you gave. This could easily be caused by a typo, for example, "server" instead of "$server".

As well, you might want to read the Connecting part of the MongoDB section in PHP doc

EDIT

Have you tried this ?:

$server = new Mongo("mongodb://localhost:27017");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜