Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: localhost:27017: Permission denied' in /var/www/html/test.php:8 Stack trace: #0 /var/www/html/test.php(8): MongoClient->__construct() #1 {main} thrown in /var/www/html/test.php on line 8
Hi Mongo experts...
I am a developer wanting to try out MongoDB. So installed centoOS 6.5 64bit in a test machine (Dell E520 Intel Dual Core 4GB Ram), installed PHP (Apache was already present).
Then installed MongoDB (yum install mongo-10gen mongo-10gen-server), then installed "pecl install mongo" (install ok: channel://pecl.php.net/mongo-1.4.5), added extension=mongo.so to php.ini.
To install pecl, I installed few other things like C++ compiler & php-pear. php5-dev & php5-cli was not available in yum so installed php-devel & php-cli (installed versions are php-devel-5.3.3-27.el6_5.x86_64 & php-cli-5.3.3-27.el6_5.x86_64
I turned off iptables firewall. Ran mongo --host localhost:27017 from shell & connected without problems
[root@localhost ~]# mongo --host localhost:27017 MongoDB shell version: 2.4.8 connecting to: localhost:27017/test >
These are the contents of test.php
<?php ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); // connect $m = new MongoClient(); //$m = new MongoClient("mongodb://localhost:27017"); // select a database $db = $m->comedy; // select a collection (analogous to a relational database's table) $collection = $db->cartoons; // add a record $document = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" ); $collection->insert($document); // add another record, with a different "shape" $document = array( "title" => "XKCD", "online" => true ); $collection->insert($document); // find everything in the collection $cursor = $collection->find(); // iterate through the results foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?>
As you can see I have tried both $m = new MongoClient(); & $m = new MongoClient("mongodb://localhost:27017");
But I am getting the same error. Where am I going wrong?