How to connect to remote mongodb with php? - php

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?

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

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')

You'll have to ask them what the connection URI is, and then use:
$m = new Mongo("mongodb://username:password#hostname");
However, I am not sure if that option is available to you. Their website says you can access data via a REST API.
At any rate, you should ask them for help. There's a button on the left that reads "aide," if you click on it you'll get a form where you can fill in your email and your question.
Reference: Mongo - Connecting

Related

Can we connect a mongodb server and a mysql server available on same host using a single PHP file?

I want to update mongodb document on basis of selected data from mysql table inside the same php file. Kindly share how this could be achieved.
If i don't misunderstand you:
<?php
$mysqlDb = connectToMysql(); // fake code to connect to mysql
$mongoDb = connectToMongod(); // fake code to connect to mongodb
$data = $mysqlDb->getData(); // fake code to get data from mysql
$mongoDb->update($data); // fake code to update mongodb

How to get the version of mongodb from php

I need a way to display a version of Mongo database in the php website.
For example, I want to show something like "powered by MongoDB version 2.2.6"
I cannot find a way to find out the version of database that php mongoClient is connected to.
I'm sure that is a way to do this. Does anyone know how?
$mongo = new \Mongo();
$admin = $mongo->admin;
$infos= $admin->command(array('buildinfo'=>true));
$version = $infos['version'];
die($version);
(You need to be admin to show it)
UPDATE : Without authentication, you can check it by using a MongoDB instance.
$c = new \MongoClient();
$db = 'yourdbname';
$mongo = new \MongoDB($c, $db);
$mongodb_info = $mongo->command(array('serverStatus'=>true));
$mongodb_version = $mongodb_info['version'];
die($mongodb_version);
Check out the serverStatus command:
http://docs.mongodb.org/manual/reference/command/serverStatus/
If you're using the PHP driver, check out:
http://php.net/manual/en/mongodb.command.php
to see how you can run that command.
I am not sure, maybe this:
<?php
$v = `mongo --version`;
print_r($v);
?>
$server_status = db->command(array('serverStatus' => TRUE));
will give you lots of information including version
(you need to be admin to run it)

Cloudcontrol no MongoDB access

I recently discovered cloudcontrol and wanted to write a little mongoDB app to store some images. I enabled the free addon mongolab.free and followed the documentation to set up a connection.
$credfile = file_get_contents($_ENV['CRED_FILE'], false);
$credentials = json_decode($credfile, true);
$uri = $credentials["MONGOLAB"]["MONGOLAB_URI"];
$m = new Mongo($uri);
$db = $m->selectDB('test');
The problem is that the following line doesn't work because I "Need to login"
$db = $m->selectDB('test');
If I try to list all available DBs via listDBs() I get the same error "Need to login"
Even when connection via the terminal, the connection establishes but with the same "need to login" warning.
I've been trying to fix this problem since three days now but I cannot find the solution :(
Hope someone can help me
The name of your database is defined in MONGOLAB_URI as well (it has the form cloudcontrol_<deployment_id>). So you don't have to explicitely define another database name (and can't).
You can extract the database name from the uri with
preg_match("#/([^/\?]+)(\?|$)#", $uri, $match);
database = $match[1];

connecting to mongo replica set from EC2

I am trying to connect to a mongo replica set using PHP,
my code looks like this:
$options = array(
'replicaSet' => 'Repset',
'readPreference' => 'primaryPreferred',
);
$connection = new MongoClient("mongodb://ip-10-1-2-3.ec2.internal:27017,ip-10-1-2-4.ec2.internal:27017/", $options);
When I do this I get an error back:
No candidate servers found
if I simplify the connect string to :
$connection = new MongoClient("mongodb://ip-10-1-2-4.ec2.internal:27017/");
Then it connects to the server, but this is not the right way, given I am using replica Sets
What am I doing wrong, the connect script seems right but obviously isn't.
Would appreciate any help.
Have you tried to delete space before $options?

Connecting to 3rd party database in Joomla?

I need to connect to another database in Joomla! that's on another server. This is for a plugin and I need to pull some data from a table.
Now what I don't want is to use this database to run Joomla!, I already have Joomla! installed and running on its own database on its server but I want to connect to another database (ON TOP of the current one) to pull some data, then disconnect from that 3rd party database - all while keeping the original Joomla database connection in tact.
You can connect to an external database from your joomla instance without using the current ressource of your joomla DB.
Try this:
<?php
$option = array(); //prevent problems
$option['driver'] = 'mysql';
$option['host'] = 'dbase.host.com';
$option['user'] = 'login';
$option['password'] = 'pwd';
$option['database'] = 'anotherdb';
$db = & JDatabase::getInstance( $option );
?>
For more infromations regarding this, check the Joomla! Documentation
I had same problem before. Fond a good tutorial showing how to connect to multiple database and switch back and forth, it also has sample code. It explains how to connect to multiple (internal and external) databases factory style, without creating multiple connections per request. This means that if you create database instance in controller same connection will be used in the model. Improves performance.
Another good explanation is on Joomla Documentation site [http://docs.joomla.org/How_to_connect_to_an_external_database].
Can you create a generic mysql-php conection inside your plugin code to create a connection ?
like
mysql_connect("remot_server_ip:3306","user","pass");
mysql_select_db("your database");
//code goes here
:
:
:
mysql_close(connection);

Categories