kintsukuroi
kintsukuroi

Reputation: 1479

How do I create certificates that allow PHP to connect to MySQL over SSL?

I am trying to connect to MySQL from PHP, but it fails with this error in the logs:

PHP Warning: mysqli::real_connect(): SSL operation failed with code 1. OpenSSL Error messages: error:14094418:SSL routines:ssl3_read_bytes:tlsv1 alert unknown ca

I already got a successful connection via command line flags:

mysql -u username -password -h host -P port -D dbname

So this means my server CAN connect and it's not a firewall issue or wrong passwords.

Here is my PHP code I use to connect:

// start connection
$new_connection = mysqli_init(); 

// set SSL
$new_connection->ssl_set('/etc/my.cnf.d/certs/client-key.pem','/etc/my.cnf.d/certs/client-cert.pem', NULL, NULL, NULL);

// set credentials
$new_connection->real_connect('host', 'user', ' password', 'dbname', port, NULL, MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT);

I think my problem might be my SSL certs are not valid. How do I create these certificates again?

Upvotes: 0

Views: 447

Answers (1)

kintsukuroi
kintsukuroi

Reputation: 1479

Finally fixed this. The solution might seem dumb, but it could help someone like me.

Just eliminated the ssl_set function and changed MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT to MYSQLI_CLIENT_SSL in real_connect.

Here is the working code:

// start connection
$new_connection = mysqli_init(); 

// set credentials over SSL
$new_connection->real_connect('host', 'user', 'password', 'database', port, NULL, MYSQLI_CLIENT_SSL);

Upvotes: 2

Related Questions