ampher911
ampher911

Reputation: 81

PHP mysqli cannot recognize SSL certificates to remote database

I want to connect with remote mysql database via mysqli function. Connection also requires SSL certificates in order to get access there. So my code looks like this:

$db = array(
            "host" => "host",
            "user" => "user",
            "password" => "password",
            "dbName" => "dbName"
        );

        ini_set ('error_reporting', E_ALL);
        ini_set ('display_errors', '1');
        error_reporting (E_ALL|E_STRICT);

        $connection = mysqli_init();
        mysqli_options ($connection, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);

        $connection->ssl_set('/usr/local/certs/client-key.pem',
 '/usr/local/certs/client-cert.pem', '/usr/local/certs/server-ca.pem', NULL, NULL);
        $link = mysqli_real_connect ($connection, $db['host'],
 $db['user'], $db['password'], $db['dbName'], 3306, NULL, MYSQLI_CLIENT_SSL);

 if (!$link)
        {
            die ('Connect error (' . mysqli_connect_errno() . '): '
 . mysqli_connect_error() . "\n");
        } 
        else 
        {
            $response = $connection->query('SHOW TABLES;');
            $this->output->writeln($response);
            $connection->close();
        }

And then I get this error:

PHP Warning:  mysqli_real_connect(): Peer certificate CN=`[project 
name]' did not match expected CN=`[IP address]`

I'm struggling with this error for few days. I'm also 100% sure that my certificates and paths are correct. How can I fix it and establish connection?

Upvotes: 0

Views: 778

Answers (1)

taiff
taiff

Reputation: 89

In my case I had to set MYSQLI_OPT_SSL_VERIFY_SERVER_CERT to false before it all worked.

Try the following instead:

mysqli_options ($connection, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, false);

Hope it works and also for the benefit of later readers.

Upvotes: 1

Related Questions