Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to remote mongodb with php?

Tags:

php

mongodb

Here is the php code that I'm working with in my local machine:

$m = new Mongo();
$db=$m->selectDB("def");
//then all in my code i use $db to select insert ... (as defined in php doc)

Now I want to connect my application to a remote server (hosted by mongood.com)

How can I do this?

like image 786
Hassan Avatar asked Dec 12 '22 07:12

Hassan


2 Answers

You can use mongoOd without the REST API But remember, it's a replica Set cluster so You need to configure your PHP for a ReplicaSet configuration...

I use mongoOd within ruby & mongoid (not the REST API)

Here a php example

<?php
  // connecting to mongood.com cluster
  $m = new Mongo("mongodb://94.23.54.103:27017,188.165.219.99:27017,94.23.220.151:27017", array("replicaSet" => "cluster"));
  var_dump($m);
  $db = $m->selectDB('my_database');
  $db->authenticate("my_login", "my_password");
  $collection = new MongoCollection($db, 'my_collection');
  $cursor = $collection->find();
  foreach ($cursor as $doc) { var_dump($doc); }
?>

Enjoy :)

A mongoOd Team member

like image 156
solisoft Avatar answered Dec 21 '22 03:12

solisoft


The constructor for the mongo object takes as its arguments connection parameters.

http://www.php.net/manual/en/mongo.construct.php

$m = new Mongo('mongodb://[username:password]@host:port')

like image 27
DeaconDesperado Avatar answered Dec 21 '22 04:12

DeaconDesperado