DBAL: Pass extra connection options to pdo_pgsql - php

How do I pass extra connection options, like connect_timeout, keepalives, etc.. with DBAL?
Should it be passed as driverOptions or a pull request created for explicit support at Doctrine\DBAL\Driver\PDOPgSql\Driver ?
I tried passing via driverOptions => ['connect_timeout' => 1], but not sure whether these settings are effective. When I do that with plain pdo_connect call:
$connectionString = 'host=... connect_timeout=1 keepalives=1 keepalives_idle=2 keepalives_interval=1 keepalives_count=2'
$connection = pg_connect($connectionString);
I know that these settings are at least applied because if I misspell any of those extra parameters an exception is raised:
$connectionString = 'host=... connection_timeout=1'
$connection = pg_connect($connectionString);
PHP Warning: pg_connect(): Unable to connect to PostgreSQL server: invalid connection option "connection_timeout" in /troubleshoot/psql.php on line 18
And this exception is the way I check that this setting has been applied.
I do not get such error if I misspell configuration passed to DBAL though.

Apparently this is not supported at the moment. All the extra options should be explicitly added to the PDOPgSql/Driver.php.

Related

Can't connect custom script to cosmos db using brightzone gremlin-php

I'm trying to build an app around the brightzone gremlin php api for cosmos db (free tier). I can get the api to work through the console (with the provided app) but when I try to use my own custom functions to call the exact same methods available in the API class – practically copying the code, which worked through the console) – I get this...
Fatal error: Uncaught TypeError: fwrite(): Argument #1 ($stream) must be of type resource, null given {...}
on line 174 in vendor/brightzone/gremlin-php/src/Connection.php
I'm new to Gremlin in general, and have limited experience using APIs.
Is this some limitation to the free tier, or have I completely misunderstood something?
edit
I decided to test the api with varying degrees of extra setup on my part...
Running the PHP CLI script: connect.php (included in cosmos graph php getting started quickstart) from a browser through xampp gave no errors. I'm seeing a spike in requests in cosmos' dashboard when I do this, so I know it's hitting the database.
The same test through a vhost alias also gave no errors, while hitting the database.
Creating my own function that calls the class and methods results in the fwrite error above.
Initializing the object
This is the same on both the CLI version and my script.
<?php
require_once('define.php');
require_once('vendor/autoload.php');
use \Brightzone\GremlinDriver\Connection;
$db = new Connection([
'host' => HOST,
'username' => USER,
'password' => PASS
,'port' => PORT
// Required parameter
,'ssl' => TRUE
]);
code comparison
Here's the provided CLI script I'm trying to emulate...
function countVertices($db)
{
$query = "g.V().count()";
printf("\t%s\n\tQuery: %s\n", "Counting all the vertices.", $query);
$result = $db->send($query);
if($result)
{
printf("\tNumber of vertices in this graph: %s\n\n", $result[0]);
}
}
This results in an instant readout of the printf commands meant for command line responses, with correct data from the result property of the object, showing it was initialized, and then functioned properly.
but
Here's how I'm trying to call the method...
$query = "g.V().count()";
$result = $db->send($query);
var_dump($result);
The object initializes fine, but calling the send method throws the fwrite error.
Seriously... what am I missing?
I failed to check the END of the CLI php script, which had a "try" catch statement running all the functions. If I had checked, I'd have seen that the open and close methods...
$db->open();
//<--whatever querying here-->
$db->close();
are necessary to get the gremlin driver connected.
It's all working now with:
<?php
require_once('define.php'); //custom script defining credentials as constants
require_once('vendor/autoload.php');
use \Brightzone\GremlinDriver\Connection;
error_reporting(E_ALL ^ E_WARNING);
$db = new Connection([
'host' => HOST,
'username' => USER,
'password' => PASS,
'port' => '443'
// Required parameter
,'ssl' => TRUE
]);
$db->timeout = 0.5;
$db->open();
$query = "g.V().count()";
var_dump($db->send($query));
$db->close();
?>
This prints the correct response.

Message: Configured database connection is persistent. Aborting

Codeigniter 2 to 3 version after upgrading, I get this error..
Why would that be?
An uncaught Exception was encountered
Type: Exception
Message: Configured database connection is persistent. Aborting.
Filename: /var/www/vhosts/xxx.com/app/system/libraries/Session/drivers/Session_database_driver.php
Line Number: 94
Backtrace:
File: /var/www/vhosts/xxx.com/app/application/core/MY_Controller.php
Line: 11
Function: __construct
File: /var/www/vhosts/xxx.com/app/application/core/MY_Controller.php
Line: 52
Function: __construct
File: /var/www/vhosts/xxx.com/app/application/controllers/Dashboard.php
Line: 7
Function: __construct
File: /var/www/vhosts/xxx.com/application/index.php
Line: 293
Function: require_once
I had the same issue, and found that it was just a matter of changing a setting:
Modify your database.php config file and turn 'pconnect' to false. As part of the CI 3 Framework, it would be part of this array:
$db['default'] = array(
'pconnect' => FALSE // This value
);
Or if your config file looks more like the CI 2 version:
$db['default']['pconnect'] = FALSE;
A bit of searching seems to suggest that the database doesn't like a persistent connection, possible because of security reasons.
Disable caching in database.php file, define caching folder in database.php by
'cachedir' => APPPATH.'cache/db/',
setting and only use
$this->db->cache_on();
command where you want your database query being cached.
Don't forget to use
$this->db->cache_off();
after select queries for unwanted cached results.
It seems like codeigniter 3.0 doesn't support sessions using database, when persistent is enabled.
form: http://www.codeigniter.com/user_guide/libraries/sessions.html?highlight=session#session-preferences
However, there are some conditions that must be met:
Only your default database connection (or the one that you access as
$this->db from your controllers) can be used. You must have the Query
Builder enabled. You can NOT use a persistent connection. You can NOT
use a connection with the cache_on setting enabled.
You need to ensure that FALSE goes out without quotes.If you use 'FALSE', the database driver will take that as a true boolean. The system expects you to use FALSE directly, without quotes. So, unset pconnect instead of using 'FALSE' in order to default to FALSE, or use FALSE as a value if you like to keep things ordered :)
* Persistent connection flag
*
* #var bool
*/
public $pconnect = FALSE;

mongo authorization zend shanty

I trying to add authorization to mongoDB.
I use mongodb 2.2.4, php-mongo 1.4.1 stable, apache2, php 5.4, ZendFramework 1.12 + ShantyMongo
Also I use replicaset with 3 instances.
When I set auth = true mongo create admin database, and when I add user to this database I got this error:
Fatal error: Uncaught exception 'MongoCursorException' with message 'test.server.com:27017: unauthorized db:database_test ns:database_test.options lock type:0 client:127.0.0.1' in /apps/test_app/libs/Shanty/Mongo/Collection.php:3501
If I add user to my database_test all work fine.
<?php
/* Configure logging */
MongoLog::setModule( MongoLog::ALL );
MongoLog::setLevel( MongoLog::ALL );
$m = new MongoClient(); // connect
$db = $m->selectDB("database_test");
$db->authenticate('admin','12345');
this code work fine even if I add user to both admin and database_test.
Maybe someone has similar problem and know what I need to do.
First of all, you should use the following syntax for specifying authentication:
<?php
$m = new MongoClient("mongodb://admin:12345#localhost/database_test");
$db = $m->database_test;
It's a bit difficult to answer your question though, as "when I add user to this database" is not really descriptive. In order to get a proper answer, you need to learn to list the exact steps or code that you used to do this.
I am thinking that you were trying to auth against a normal database (database_test), where you would only have a user/pass set for the admin database. In that case, you need to auth to the admin database, and then select your database_test:
<?php
$m = new MongoClient("mongodb://admin:12345#localhost/admin");
$db = $m->database_test;
Thank you for your answer I figured out this.
Maybe it's will be heplful for some one on future, Shanty-Mongo require username, password and port in array of servers.
It's should looks like:
; Shanty Mongo Config
hosts.0.host = '127.0.0.1'
hosts.0.port = '27017'
hosts.0.username = 'root'
hosts.0.password = 'pass'
hosts.1.host = '127.0.0.1'
hosts.1.port = '27018'
hosts.1.username = 'root'
hosts.1.password = 'pass'
hosts.2.host = '127.0.0.1'
hosts.2.port = '27019'
hosts.2.username = 'root'
hosts.2.password = 'pass'
database = 'database_test'
replicaSet = true
Also what I do:
in mongo:
1. edit config file, enable auth (auth = true)
2. create user in admin database
3. connect as new user
4. create user in target database
in ZF config
update config file, as I describe above.
Also some times I get error: Fatal error: php_network_getaddresses, I think it's because I have some trouble with DNS or php trying resolve address: mongo://root:pass#127.0.0.1:27017,root:pass#127.0.0.1:27018,root:pass#127.0.0.1:27019/database_test

How can I confirm PHP MongoDB connection is properly recognizing replica sets?

Using the example from the manual:
$mongo = new Mongo("mongodb://sf2.example.com,ny1.example.com", array("replicaSet" => "myReplSet"));
When I check $mongo, it says it is indeed connected. I thought I could then call $mongo->isMaster() to get replica set details, but that doesn't work. Is that not a proper way of doing this?
isMaster isn't a PHP function (see http://www.php.net/manual/en/class.mongo.php for a list of functions available in the Mongo class). You can do:
$result = $mongo->myDb->command(array("isMaster" => 1));
This runs the isMaster command on the myDb database (it doesn't matter what db you run it on).

SSL Connections with ADODB

I have a project we are about to wrap up, but just got a request from the DBA to make all our connections SSL. So I switched the driver to mysqli in the object that turns out ADODB instances, but I don't see any native method to create secure connections.
To make things more difficult, there is a different set of certs and keys per connection type (read and write).
Anyone have any ideas?
The trick is to use a DSN in the
NewADOConnection() call (rather than
authenticating with a Connect() call)
and to use the mysqli driver. The DSN
syntax allows you to supply client
flags, and there's a mysqli flag for
using SSL certificates.
$dsn = 'mysqli://ssluser:sslpass#dbhost/test?clientflags=2048';
$dbh = NewADOConnection($dsn);
$sql = "show status like 'ssl_cipher'";
$res =& $dbh->Execute($sql);
print_r( $res->fields );
$res->Close();
$dbh->Close();
The answer to this question is found at:
http://mbrisby.blogspot.com/2008/06/adodb-php-mysql-ssl.html
Here is the reference to MySQL Client Flags:
http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol
I prefer the one with the Connect method, and clientFlags property.
$dbc = NewADOConnection('mysqli');
$dbc->clientFlags = CLIENT_SSL;
$this->dbc->Connect('dbhost', 'ssluser', 'sslpass', 'test');
Either of them would work.

Categories