php - 连接字符串错误,PHP 与 Azure 中部署的 MySQL

标签 php mysql azure connection-string

我正在从事一个项目,该项目包括在 Azure 中部署 PHP 网站并与 Azure MySQL 连接。

我首先在 Azure MySQL 中创建和迁移数据。我还创建了 PHP 网站并部署了代码,但是当在 Web 应用程序的配置部分添加连接字符串时,出现错误:

value is a required property on all connection strings

连接字符串:

$con=mysqli_init(); [mysqli_ssl_set($con, NULL, NULL, {ca-cert filename}, NULL, NULL);] mysqli_real_connect($con, {your_host}, {username@servername}, {your_password}, {your_database}, {your_port});

我无法保存连接字符串,因为错误在连接字符串选项卡下方以红色显示。

因为我没有使用 PHP 和 MySQL 的经验,所以我用 google 搜索了该错误,但没有找到任何内容。

最佳答案

如果您想在Azure Web应用程序中添加我的sql连接字符串,请引用以下步骤

  1. 获取连接字符串 enter image description here

  2. 在 Azure Web 应用程序中添加连接字符串 enter image description here

  3. 启用从 Azure 应用服务访问 Azure Database for MySQL

    Azure Database for MySQL 使用防火墙提供访问安全性来保护数据。从 Azure 应用服务连接到 Azure Database for MySQL 服务器时,请记住应用服务的出站 IP 本质上是动态的。选择“允许访问 Azure 服务”选项将允许应用服务连接到 MySQL 服务器。 enter image description here

  4. 通过kudu上传perm文件 enter image description here

  5. 使用 php 连接 Azure MySQL

<?php
$connectstr_dbhost = '';
$connectstr_dbname = '';
$connectstr_dbusername = '';
$connectstr_dbpassword = '';
foreach ($_SERVER as $key => $value) {
    if (strpos($key, "MYSQLCONNSTR_") !== 0) {
        continue;
    }

    $connectstr_dbhost = preg_replace("/^.*Data Source=(.+?);.*$/", "\\1", $value);
    $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\\1", $value);
    $connectstr_dbusername = preg_replace("/^.*User Id=(.+?);.*$/", "\\1", $value);
    $connectstr_dbpassword = preg_replace("/^.*Password=(.+?)$/", "\\1", $value);
}

$conn = mysqli_init();
mysqli_ssl_set($conn,NULL,NULL, "D:/home/site/wwwroot/Cert/BaltimoreCyberTrustRoot.crt.pem", NULL, NULL) ;
mysqli_real_connect($conn, $connectstr_dbhost, $connectstr_dbusername, $connectstr_dbpassword, $connectstr_dbname, 3306, MYSQLI_CLIENT_SSL);
if (mysqli_connect_errno($conn)) {
die('Failed to connect to MySQL: '.mysqli_connect_error());
}
echo "Success: A proper connection to MySQL was made! The  database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($conn) . PHP_EOL;

?>

enter image description here

关于php - 连接字符串错误,PHP 与 Azure 中部署的 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57836723/

相关文章:

php - yii2 错误不在日志中

php - Codeigniter - 更新查询执行成功,但 $this->db->affected_rows() 返回 false

asp.net - 发布到 Azure 程序集 Microsoft.Owin 后无法加载

azure - 使用 Azure 托管标识或服务主体访问 O365 Exchange Online

Mysql喜欢多列搜索

azure - 由于未设置连接字符串,无法运行 azure 函数并出现错误

php - 如何从我的成员表中选择所有用户名?

php - 删除除 "."以外的所有非单词字符

javascript - 评论仅适用于第一篇文章

mysql select into outfile 没有任何行终止字符