PHP mysqli and SSL - php

I am trying to configure Galera Cluster over a WAN (VPN is not an option). For obvious reasons I am setting up the connections to require SSL keys, and am having difficulties getting the application to connect. I can successfully connect using PDO, but cannot get mysqli to work. The setup will be:
USA location:
mainWebServer: openSUSE Leap 15.2, Apache 2.4.33 (named virtual host myserver.mycompany.com)
myServer1: Ubuntu 20.04, MySQL Galera Cluster 8.0.19
myDevBox: openSUSE Tumbleweed, running PHP CLI to debug connection issues
China location:
myServer2: Ubuntu 20.04, MySQL Galera Cluster 8.0.19, Apache 2.4 current
myServer3: clone of myServer2
The Galera cluster name is myServer, and the node names are myServerX corresponding to their actual hostname.
PHP version info:
me#mydevbox:~> php --version
PHP 7.4.9 (cli) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.9, Copyright (c), by Zend Technologies
MySQL server version info:
me#myServer1:~$ mysql --version
mysql Ver 8.0.19-26.3 for Linux on x86_64 (MySQL Wsrep Server - GPL)
Code:
<?php
$conn = mysqli_init() or print("init failed\n");
mysqli_ssl_set(
$conn,
'/srv/www/vhosts/myProject/mysql_ssl_certs/client-key.pem',
'/srv/www/vhosts/myProject/mysql_ssl_certs/client-cert.pem',
'/srv/www/vhosts/myProject/mysql_ssl_certs/ca-cert.pem',
NULL,
NULL
);
mysqli_real_connect(
$conn,
'myServer1',
'myUser',
'myPassword',
'myDB',
3306,
MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT
) or print("connect failed to myServer1 [{$conn->connect_errno}: {$conn->connect_error}]\n");
print_r($conn);
Output:
me#mydevbox:~> php sqlssltest.php
PHP Warning: mysqli_real_connect(): Peer certificate CN=`myServer' did not match expected CN=`myServer1' in /home/me/sqlssltest.php on line 21
PHP Warning: mysqli_real_connect(): Cannot connect to MySQL by using SSL in /home/me/sqlssltest.php on line 21
PHP Warning: mysqli_real_connect(): [2002] (trying to connect via (null)) in /home/me/sqlssltest.php on line 21
PHP Warning: mysqli_real_connect(): (HY000/2002): in /home/me/sqlssltest.php on line 21
connect failed to myServer1 [2002: ]
mysqli Object
(
[client_info] => mysqlnd 7.4.9
[client_version] => 70409
[connect_errno] => 2002
[connect_error] =>
[errno] => 2002
[error] =>
)
By the looks of it mysqli is ignoring my MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT flag. I was able to successfully connect with PDO using PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false - is there a way to make mysqli work? Unfortunately this is to support legacy application code that still uses mysqli and there is no way to refactor by the time this needs to go live. I believe creating separate CA certs would break the replication connection as they all have different hostnames.

You are passing MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT as the 7th parameter, however that is the socket parameter, flags is the 8th. Try passing a null for the 7th and bumping MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT to the 8th.

Related

PDOException: could not find driver - pdo_mysql enabled [duplicate]

This question already has answers here:
PDOException “could not find driver”
(42 answers)
php - PDOException could not find driver [duplicate]
(2 answers)
Laravel 5 PDOException Could Not Find Driver [duplicate]
(11 answers)
Laravel: PDOException: could not find driver
(32 answers)
Fatal error: Uncaught PDOException: could not find driver - windows MAMP php. The PHP.ini file has the correct part uncommented
(1 answer)
Closed 29 days ago.
I’ve been struggling trying to connect PHP to MySQL using the PDO() constructor since yesterday, and despite all the answers I’ve found on this problem, none of them worked.
In phpMyAdmin I have a database called ‘database_test’ and I’m trying to connect to it.
I’ve tried with ‘localhost’ and with ‘localhost:3306’:
<?php $mysqlConnection = new PDO('mysql:host=localhost;dbname=database_test;charset=utf8','root','root');?>
Both returning the same error when accessing http://localhost:8080/home.php :
Fatal error: Uncaught PDOException: could not find driver in
C:\Users...\home.php:3 Stack trace: #0 C:\Users...\home.php(3):
PDO->__construct('mysql:host=loca...', 'root', 'root') #1 {main}
thrown in C:\Users...\home.php on line 3
Yet, the module pdo_mysql is enabled
Mamp says : “To connect to the MySQL server from your own scripts use the following connection parameters”:
Host : localhost
Port : 3306
User : root
Password : root
Which I did.
Configuration
System : Windows 11
MAMP 4.2.0
Apache 2.2.31
MySQL 5.7.24
PHP 7.4.1
PhpMyAdmin 4.8.3
In phpinfo():
mysqlnd
enabled
API extensions
mysqli,pdo_mysql
PDO support
enabled
PDO drivers
sqlite, mysql
PDO Driver for MySQL
enabled
Client API version
mysqlnd 7.4.1
Pdo_sqlite
enabled
SQLite Library
3.30.1
In my php.ini (located here : C:\MAMP\conf\php7.4.1\php.ini), among the extensions that are uncommented I have :
extension=php_mysqli.dll
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll
extension=php_pdo_mysql.dll
; ...
; Directory in which the loadable extensions (modules) reside.
extension_dir = "C:\MAMP\bin\php\php7.4.1\ext\"
; ps : I've checked, they're all here

PHP 64 bit - Teradata ODBC driver failure - system error 126 'specified module cannot be found' -

I have installed PHP 5.6.30 64 bit on Windows Server 2012 along with Teradata ODBC driver.
Whenever I test the PHP connection I receive an error
Warning: odbc_connect(): SQL error: Specified driver could not be loaded due to system error 126: The specified module could not be found. (Teradata, C:\Program Files\Teradata\Client\15.00\ODBC Driver for Teradata nt-x8664\Lib\tdata32.dll)., SQL state IM003 in SQLConnect
EDIT: C:\Program Files\Teradata\Client\15.00\ODBC Driver for Teradata nt-x8664\Lib\tdata32.dll does definitely exist on that specific path. Also, using tdxodbc.exe to test the ODBC connection works normally using tdata32.dll, so it appears to be some issue with PHP.
I have installed Teradata ODBC via GSS > ICU > ODBC from the Utilities package based on a related thread however still experiencing the same error. I can make a successful 32-bit ODBC connection via SQL assistant. I have tried a DSN / DSN-less ODBC connection via PHP but same error occurs.
I have been unable to find any clear indication of what the issue is - any idea?

Peer certificate CN=`stream.twitter.com' did not match expected CN=`199.16.156.217'

I was trying to develop a twitter streaming application on my AWS EC2 machine. The OS platform is Ubuntu 16.04.1 LTS and I have downgraded the PHP version to 5.6.28-1+deb.sury.org~xenial+1
When I run the twitter streaming application on this server, I am getting the following errors.
Warning: fsockopen(): Peer certificate CN=`stream.twitter.com' did not match expected CN=`199.16.156.217' in /var/www/html/myapp/streamer/twitterstreamer.php on line 620
Warning: fsockopen(): Failed to enable crypto in /var/www/html/myapp/streamer/twitterstreamer.php on line 620
Warning: fsockopen(): unable to connect to ssl://199.16.156.217:443 (Unknown error) in /var/www/html/myapp/streamer/twitterstreamer.php on line 620
The same code is running without any issues in another two machines (one is AWS EC2 and the another is a godaddy server).
All the ports in the current EC2 machine is open now and the SSL version is OpenSSL/1.0.2g the openssl section is having the following value.
openssl section - phpinfo
Can someone help me to find where the exact issue is ?
The issue is resolved with the clue fro the first error.
Warning: fsockopen(): Peer certificate CN='stream.twitter.com' did not match expected CN='199.16.156.217' in /var/www/html/myapp/streamer/twitterstreamer.php on line 620
In PHP 5.6.x, the value of openssl VERIFY_PEER is true by default. The system will first fetch the certificate from the peer and matches with ours. In my application, I was connecting with the IP and the URL in the fetched certificate is stream.twitter.com. This was the issue.
Changing IP in the fsockopen to stream.twitter.com solved my issue.
PHP versions prior to 5.6, the default value of VERIFY_PEER is false and this is why the same code running on my other instances.

Setup SSL for PDO-MySQL on Fedora

I want to setup SSL for MySQL on Fedora. I am using PDO as MySQL-connector in PHP. But it is not working.
The PHP-version (php -v) is:
PHP 5.6.6 (cli) (built: Feb 19 2015 13:46:39)
When I check php -m I get a long list, including openssl, PDO, pdo_mysql, mysql.
In /etc/my.cnf I added the "ssl" and the three certificates (CA-root, private-key, certificate). I restarted MySQL and Apache.
The pdo-connector is this:
function getDBConn() {
return new PDO(
'mysql:host=localhost; dbname=forum',
user,
pass,
array(
PDO::MYSQL_ATTR_SSL_KEY => path_to_same_as_in_my.cnf.key,
PDO::MYSQL_ATTR_SSL_CERT => path_to_same_as_in_my.cnf.crt,
PDO::MYSQL_ATTR_SSL_CA => path_to_same_as_in_my.cnf.crt
)
}
But on the pages that connect to the database I get the following errors:
Warning: PDO::__construct(): this stream does not support SSL/crypto in /var/www/html/functions.php on line 78
Warning: PDO::__construct(): cannot connect to MySQL by using SSL in /var/www/html/functions.php on line 78
Warning: PDO::__construct(): [2002] (trying to connect via unix:///var/lib/mysql/mysql.sock) in /var/www/html/functions.php on line 78
FATAL ERROR: Uncaught exception 'PDOException' with message 'SQL:STATE[HY000][2002]' in /var/www/html/functions.php:78 Stack trace .....
Anyone who has a solution for this?
All you need is this for PDO MySQL using SSL
<?php
$pdo = new PDO('mysql:host=ip;dbname=db', 'user', 'pass', array(
PDO::MYSQL_ATTR_SSL_KEY =>'/etc/mysql/ssl/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT=>'/etc/mysql/ssl/client-cert.pem',
PDO::MYSQL_ATTR_SSL_CA =>'/etc/mysql/ssl/ca-cert.pem'
)
);
$statement = $pdo->query("SHOW TABLES;");
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo htmlentities($row['_message']);
?>
Another biggest reason you're getting the error is because you might not have openssl installed.

Investigating mssql_connect problems in PHP

Today, I restarted my CentOS server and after that my PHP application started to fail in accessing our MsSQL server.
[Sun Mar 13 23:44:31 2011] [error] [client 88.154.*.*]
PHP Warning: mssql_connect()
[<a href='function.mssql-connect'>function.mssql-connect</a>]: Unable to connect to server: 85.*.*.109
MsSQL server uses port 1433.
I'm able to get a connection when I try telnet ip 1433 to MsSQL server.
Username and password combination is correct, I'm able to login from another machine.
Nothing has been changed in security settings of MsSQL server.
How can I see what's wrong?
did you double check and make sure that ALL of sql server's services have started?

Categories