I'm trying to connect from PHP PDO to Amazon's PostgreSQL DB instance using SSL. I'm using the following DSN:
$db = new PDO('pgsql:host=[host];port=[port];dbname=[db];options=\'--client_encoding=utf8\';sslmode=require;sslcert=/path/domain.crt;sslkey=/path/private.key;sslrootcert=/path/intermediate.ca-bundle;', '[user]', '[pass]');
I'm using the same sslcert, sslkey and sslrootcert that are used by Apache on the same client server - they are in PEM format. I can connect using SSL to MySQL with them using following PDO options.
$options = [
PDO::MYSQL_ATTR_SSL_KEY => '/path/private.key',
PDO::MYSQL_ATTR_SSL_CERT => '/path/domain.crt',
PDO::MYSQL_ATTR_SSL_CA => '/path/intermediate.ca-bundle',
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => true,
];
But for PG, I'm getting:
SQLSTATE[08006] [7] SSL error: certificate verify failed
Without SSL, I can connect with no problem.
Any help would be appreciated!
Related
I'm trying to create a connection to a local Database Server through a VPN connection first.
From the VPN connection I receive this IP for my network:
192.168.30.2
DB-Server:
192.168.40.150
If I try to ping the DB Server IP via PHP, I also get a response from the Server. When, however, I try to make the PDO connection or with "mysqli", I receive the following error:
lluminate \ Database \ QueryException: SQLSTATE [HY000] [1045] Access denied for user 'xxxx'#'192.168.30.2' (using password: YES) (SQL: select * from `xxxx`) in file
If I use a Mysql Client from my PC it's working fine!
Any idea?
SOLVED: the DB server required a SSL Connection (without Cert). These options was required in the PDO connection:
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT
PDO::MYSQL_ATTR_SSL_CA
If you use Laravel Illuminate for the connection you need to declerate the options in the connection, example:
...
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => env('MY_SSL_VERIFY', ' '),
PDO::MYSQL_ATTR_SSL_CA => env('MY_SSL_CA', ' '),
]) : [],
...
In this case is really important to give a Value with empty space ' ' otherwise will the Illuminate PDO Connector not get the option values.
I am using mongoDB with PHP. on local system it works fine. Now I am trying to access mongoDB cluster on AWS which is secured by SSH. I am using below code
$conn = new MongoClient('mongodb://SSH Hostname', [
'username' => 'username',
'password' => '',
'db' => 'DBname'
]);
print_r($conn);
It gives below error
Type: MongoConnectionException
Message: Failed to connect to: SSH Hostname :27017: Connection refused
I can connect DB from MongoDB compass or Studio 3T. How I can use SSH while connecting using PHP
I am trying to connect to an FTP service which requires the user to connect via Implicit SSL. The details are working in CuteFTP so my credentials are 100%.
I am trying to use this class and the connection method below but it just times outs.
Class: https://gist.github.com/maxrice/4544344
<?php
include "../objects/shared/ftp-implicit-ssl-tls.php";
$FTP_Implicit_SSL = new FTP_Implicit_SSL("myuser", "mypass", "rapidata.sharefileftp.com", 990, "Organisations/Avid/RDS_Data", true);
$FTP_Implicit_SSL->upload("text.txt", "text.txt");
?>
There is a simple text.txt file in the same directory as the script.
Fatal error: Uncaught exception 'Exception' with message 'Could not upload file. cURL Error: [7] - Failed to connect to rapidata.sharefileftp.com port 990: Connection timed out
My web host reports the script should be able to connect and this is what they see:
Last login: Wed Apr 29 16:14:57 2015 from 79.174.170.1
root#server-1:~# telnet rapidata.sharefileftp.com 990
Trying 54.194.108.219...
Connected to rapidata.sharefileftp.com.
Escape character is '^]'.
Then eventually
Connection closed by foreign host.
Pleaase - any ideas what is going on here or how to find out?
what about these settings in the class?
$options = array(
CURLOPT_USERPWD => $username . ':' . $password,
CURLOPT_SSL_VERIFYPEER => false, // don't verify SSL
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_FTP_SSL => CURLFTPSSL_ALL, // require SSL For both control and data connections
CURLOPT_FTPSSLAUTH => CURLFTPAUTH_DEFAULT, // let cURL choose the FTP authentication method (either SSL or TLS)
CURLOPT_UPLOAD => true,
CURLOPT_PORT => $port,
CURLOPT_TIMEOUT => 300,
);
When i try connect to mysql with clear PHP, its working fine.
My code
$link = mysql_connect('hostname', 'username', 'password');
if (!$link) {
die('Could not connect');
}
if(mysql_select_db('dbname')){
echo 'Connected successfully';
}
But when im trying to connect with yii, then getting the error
My config/main.php
'db'=>array(
'class'=>'CDbConnection',
'connectionString' => 'mysql:host=hostname;dbname=dbname',
'emulatePrepare' => true, /* I try false too*/
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
),
This is output for exception what i print in open() function framework/db/CDbConnection.php
Exception handle here
protected function open()
{
if($this->_pdo===null)
{
if(empty($this->connectionString))
throw new CDbException('CDbConnection.connectionString cannot be empty.');
try
{
Yii::trace('Opening DB connection','system.db.CDbConnection');
$this->_pdo=$this->createPdoInstance();
$this->initConnection($this->_pdo);
$this->_active=true;
}
catch(PDOException $e)
{
echo '<pre>';
var_dump($e); die;
if(YII_DEBUG)
{
throw new CDbException('CDbConnection failed to open the DB connection: '.
$e->getMessage(),(int)$e->getCode(),$e->errorInfo);
}
else
{
Yii::log($e->getMessage(),CLogger::LEVEL_ERROR,'exception.CDbException');
throw new CDbException('CDbConnection failed to open the DB connection.',(int)$e->getCode(),$e->errorInfo);
}
}
}
}
Exception text
"SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client"
I see in display
CDbException
CDbConnection failed to open the DB connection.
My PHP VERSION 5.5.36 Mysql version 5.5.35
My Hosting is i-page dot com
Yii Version '1.1.13'
Thanks for help.
It appears to be a problem with the way the passwords are hashed and the version of MySQL and the MYSQL_PDO library.
Yii uses PDO to query the database, thats why clear PHP works like a charm and Yii doesn't.
To verify this, try this:
$mysqlConnection = new PDO("mysql:host=hostname;dbname= dbname", "username", "password");
this line should throw the following error:
PDO::__construct(): The server requested authentication method unknown to the client [mysql_old_password].
This error is the equivalent to the MySQL 2045:
"SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
If you confirm that the problem is related to PDO you have several options but you need to access the hosting system (or ask them to fix the problem):
Sign in to MySQL and execute the following command SET PASSWORD FOR 'username'#'hostname' = OLD_PASSWORD('password'); (this will fix the hashing of the pasword)
Upgrade the MYSQL PDO Library (PDO_MYSQL) to match the version of MYSQL on the server.
I got this problem with Yii 1.1.x project and almost got crazy:
First make sure you have
sudo apt install php-xml php-mbstring php-pdo php-mysql
installed, and restart apache
sudo apachectl restart
or:
sudo service apache2 restart
Detailed error was that database library cannot be found.
My solution was related to caching:
cache' => array(
'class' => 'system.caching.CDbCache',
//'class' => 'system.caching.CFileCache',
'connectionID'=>'db', // <<< THIS IS THE ISSUE
),
if connectionID is not set, db caching defaults to mysqli database in /protected/data directory which cannot be accessed if mysqli driver is not installed on system (common issue with dedicated servers, DO droplets, xampp/wamp...)
or, you can disable db caching and enable fileCache instead.
I'm trying to connect to pgsql via laravel and finally got everything setup (pgsql server running, pdo installed, all libs installed). I'm running on a VPS (CentOS) managed via CPanel/WHM.
Here's what I'm doing:
I'm trying to create a user database via artisan (Laravel's command line) using migrate:install.
For those that don't use Laravel, artisan uses php's PDO for pgsql to connect. Here are my settings:
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'prefix' => '',
),
I've also setup a test.php file:
$dbconn = pg_connect("host=localhost port=5432 dbname=dbname user=username password=password");
which also fails. I used phpPgAdmin to see what's up and all of the permissions are set correctly, the database shows up, the username is correct, same with password. I checked where postgre (version 8.4.12 btw) was running and phpPgAdmin tells me "localhost:5432".
The error I get via the command line is the following:
SQLSTATE[08006] [7] FATAL: no pg_hba.conf entry for host "::1", user "myusername", database "my_database", SSL OFF
Now, I tried to find the pg_hba.conf file but I'm not entirely sure where to look for it. Same goes for the error/access logs for pg and google hasn't been any help as far as this goes.
Any idea on what I can do?
localhost points to IPV6 ::1 address on your system. Postgresql makes the difference between ipv6 and ipv4 addresses when dealing with access list.
I was able to install/configure everything correctly. I changed the "host" to 127.0.0.1, not sure why that made a difference but it did :)