我无法在下面的PHP代码中用SSL连接MySQL数据库(存在于另一服务器中)。 我尝试了以下链接中的步骤:https://docs.microsoft.com/en-us/azure/mysql/howto-configure-ssl
有人可以告诉我这些步骤应该怎么做吗?
仅供参考,我可以从命令行连接MySQL数据库。
无法连接到MySQL:php_network_getaddresses:getaddrinfo
失败:名称或服务未知
$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);
}
}
}
答案 0 :(得分:1)
您是否正在Azure中尝试此操作?请确保该pem文件具有正确的路径和权限
连接数据库时要解决的问题:
下载此pem密钥并将文件重命名为“ BaltimoreCyberTrustRoot.crt.pem ” https://www.digicert.com/CACerts/BaltimoreCyberTrustRoot.crt.pem
在azure服务器中启用SSL连接。
尝试以下代码示例:
{
$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);
}
}
}