connecting to mongo replica set from EC2 - php

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?

Related

Alternative connection to database without PHP?

Objective: Update prices of products between databases: Shop's server DB has the latest prices and website's DB need to be updated accordingly with any "each 24 hours" script (I'll look this up later).
I'm using Ionos as hosting for the website, and The server is shared, so I can't touch php.ini or add files for php.
I'm trying to connect to a SQL server DB, but since it requires dll libraries to be installed and to modify the php.ini, I can't do that.
I can't either make it from the other side, If I make it from an external server in order to update the prices of the website, they don't allow to make connections out of the context of the server.
So, I know that the solution is to upgrade the hosting's plan and pay more and so on, so I have a virtual server for my own. But before doing that, is there any other way to establish this connection without using php? Is there something else that allows me to create a DB connection?
The fatal errors appears as soon as sqlsrv_connect is read as there is no library to load this function.
$serverName = "x, 0000";
$connectionInfo = array( "Database"=>"x", "UID"=>"x", "PWD"=>"xxx");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
Edit: Comes to my mind... Maybe a solution would be to tell this php file to load php.ini and so on from another server if that's possible?
You could possibly call a JSON endpoint on your DB server (secure the endpoint though (out of scope for my answer)) https://3v4l.org/Gpi28
<?php
// MSSQL server side
$data = [
1 => 'hello',
2 => 'world',
];
// Imagine $data above is the array of rows returned by the db query
header('Content-Type: application/json');
echo json_encode($data);
exit;
// IONOS Side
$json = file_get_contents('http://your-database-server/some/url/or/other');
$data = json_decode(true);
// Now do your updates
// NB This is an INSECURE example, people who know the URL can see this data!

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];

php mysql_connect - Lost connection to MySQL server

this post has been edited to reflect the findings thus far between myself and iamkrillin, as we have been the only two posters
I have the following VB.NET code connecting correctly, running from my PC
Dim strConnection As String = "Server=dev.xxxxx.vmc;Database=report1;integrated security=SSPI;" & _
"persist security info=False;Trusted_Connection=Yes;"
Dim ObjDa As SqlDataAdapter = New SqlDataAdapter(pStrQuery, strConnection)
Try
Dim dsReturn As DataSet = New DataSet
ObjDa.Fill(dsReturn)
ObjDa.Dispose()
Return dsReturn
Catch ex As Exception
Return Nothing
End Try
I have the following PHP code running from our iSeries
$conn = array( 'host' => 'dev.xxxxx.vmc',
'username' => 'vmc\adam',
'password' => 'xxxxxx)',
'dbname' => 'report1',
'pdoType' => 'dblib' );
try {
$db = new Zend_Db_Adapter_Pdo_Mssql($conn);
$db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
}
The getConnection function, is throwing an error:
SQLSTATE[] (null) (severity 0)
And when I look up this error HERE, it appears to be a bug PRE 5.2.10, and we are running 5.2.17. But, some of the other comments say it is still a bug in 5.3.
*edit
It seems that if using a domain account, windows auth must be enabled. However, it is not through our PHP. So I need to set up a database specific user for our PHP connection.
In your VB snippet, you are connecting to SQL Server, and in your PHP snippet you are connecting to MySQL. If you need to use SQL Server from PHP, look at this. If you are on a non windows platform you can try FreeTDS. Here is an example of how to get started with it

Connecting to mysql from your server

I am trying to upload a portfolio site which uses a mysql Database, I have been just Xampp for testing it, but now I wanna upload and I keep getting can't connect to DB when I upload to my site, let me know what I need to put instead of local to connect to schellshockdesign.com
thanks
<?php
//connect to database
$db = mysql_connect( 'localhost', 'blog_ss_user', 'secret' );
//choose the right DB
mysql_select_db( 'schells_portfolio', $db ) or die( 'could not connect to DB' );
http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
You have to enabled remote access so that you can connect remotely.
You have to do/ check two things:
Check that you have permissions to access the MySQL server remotely.
Type the correct domain name that the MySQL server is located at.
Start reading about json_encode. Crete a secret php file on your live server. Pass the data from local to live like http://www.domain.com/secretfile.php?data=$json; Now, catch with $_GET['data'], decode the json with json_decode($_GET['data']); and establish the Db connectiuon and insert your data.
Insert your website address into the connection process
$db = mysql_connect( 'schellshockdesign.com', 'blog_ss_user', 'secret' );

How to connect to remote mongodb with 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

Categories