I'm unable to connect MySQL database(that exists in another server) with SSL in below PHP code. I tried the steps from the link: https://learn.microsoft.com/en-us/azure/mysql/howto-configure-ssl
Can anyone advise me what should be done from those steps?
FYI, I can connect the MySQL database from the command line.
Failed to connect to MySQL: php_network_getaddresses: getaddrinfo
failed: Name or service not known
$db = mysqli_init();
//mysqli_options ($db, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
mysqli_ssl_set($db,"/var/www/html/path/cert/client-key.pem","/var/www/html/path/cert/client-cert.pem","/var/www/html/path/BaltimoreCyberTrustRoot.crt.pem",NULL,NULL);
$c=mysqli_real_connect($db, 'example.com','uname@uname','mypassword','database',3306,NULL, MYSQLI_CLIENT_SSL);
if(mysqli_connect_errno($db)){
echo "<br>Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
$sql = 'show tables ';
echo $sql.'<br>';
$result = mysqli_query($db,$sql);
$rows = array();
if(!empty($result)){
while ($row = mysqli_fetch_assoc($result)){
$rows[] = $row;
echo '<pre>'; echo json_encode($row, JSON_PRETTY_PRINT);
}
}
}
Are you trying this in Azure? Please make sure the pem file has correct path and permission
Things to fix while connecting the database:
Download this pem key and rename the file "BaltimoreCyberTrustRoot.crt.pem" https://www.digicert.com/CACerts/BaltimoreCyberTrustRoot.crt.pem
Enable SSL connection in azure server.
Try the following code sample :
{
$db = mysqli_init(); mysqli_ssl_set($db,NULL,NULL,"/var/www/html/path/BaltimoreCyberTrustRoot.crt.pem",NULL,NULL);
$connection = mysqli_real_connect($db, 'example.com','uname@uname','mypassword','database_name',3306,NULL, MYSQLI_CLIENT_SSL);
if(mysqli_connect_errno($db))
{
echo "<br><b>Failed to connect to MySQL: " . mysqli_connect_error()."<b>";
}
else
{
$sql = 'show tables ';
$result = mysqli_query($db,$sql);
$rows = array();
if(!empty($result))
{
while ($row = mysqli_fetch_assoc($result)){
$rows[] = $row;
echo '<pre>'; print_R($row);
}
}
}