I'm using ubuntu 14.04 with AMPPS for my localserver, I've managed to enable mongoDB extenstion and I've created database and collections in rockmongo and even by using terminal using mongo.
But when I'm trying to use mongodb in my code I get the below error message :
( ! ) Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: localhost:27017: Connection refused' in /usr/local/ampps/www/TMongo.php on line 3
( ! ) MongoConnectionException: Failed to connect to: localhost:27017: Connection refused in /usr/local/ampps/www/TMongo.php on line 3
This is the peace of code I've tried to test mongodb :
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->laravel;
echo "Database mydb selected";
$collection = $db->laravel;
echo "Collection selected succsessfully";
$cursor = $collection->find();
// iterate cursor to display title of documents
foreach ($cursor as $document) {
echo $document["title"] . "\n";
}
?>
I've searched and tried a few methods to kill all mongodb process and start it again and ... but none of them worked for me.
when I run : ps -eF | grep 'mongo\|PID'
I get :
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
root 3516 3331 0 223796 118188 3 12:42 pts/22 00:00:01 /usr/local/ampps/mongodb/bin/mongod --journal --pidfilepath=/usr/local/ampps/mongodb/data/pid --config=/usr/local/ampps/mongodb/mongo.conf
server 7091 25150 0 2939 2280 2 12:47 pts/26 00:00:00 grep --color=auto mongo\|PID
when I run: sudo killall -15 mongod
And again i run above command I get :
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
server 8299 25150 0 2939 2268 3 12:49 pts/26 00:00:00 grep --color=auto mongo\|PID
When I run this command while my mongodb service is off : sudo netstat -tlnp | grep 27017
I get nothing but when I start mongodb service and again I run netstat command I get :
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 9956/mongod
mongodb.conf :
#Turn on simple rest api
rest=true
journal=true
#Storage Engine Set to mmapv1
storageEngine=mmapv1
#Directory for Data Files
dbpath={$path}/mongodb/data/db
#log file to send write to instead of stdout - has to be a file, not directory
logpath={$path}/mongodb/logs/mongo.log
#Append to logpath instead of over-writing
logappend=true
#Specify port number
#port=27017
#Full path to pidfile (if not set, no pidfile is created)
pidfilepath={$path}/mongodb/data/pid
#Each database will be stored in a separate directory
directoryperdb=true
#Run "{$path}/mongodb/bin/mongod.exe" --help in command prompt for more help.
FYI : I've installed ampps in it's default path for linux : /usr/local/ampps/
I'm using ubuntu 14.04 with AMPPS 3.6 for localserver. How can I fix this, Any suggestion ?
UPDATE I've added this parameter to mongodb.conf file and restarted mongodb
bind_ip = 127.0.0.1
Then i run this : sudo netstat -tlnp | grep 27017
Seems that mongodb is listening on 127.0.0.1:27017
And when I try to connect to it from command line using mongo, it works but in php page I still have the same problem.
any suggestion ?
In ampps 3.6 they added mongodb.so to the list of php extentions. And if you enable mongo and mongodb extentions you will be connected to mongodb.