簡體   English   中英

Microsoft Azure for MySQL 無法使用 PHP 連接到數據庫

[英]Microsoft Azure for MySQL can't connect to DB using PHP

大家早上好,我正在嘗試使用 php 連接到 MySQL 的 Azure 服務器,但它給了我一個錯誤。 php 文件每天 00:00 在我的網站托管的服務器上運行,並且必須連接到 azure DB 才能在 Azure DB 上運行查詢。 我得到的錯誤是“連接超時”,我嘗試使用示例命令在我的防火牆中添加帶有服務器 IP 的規則:“az mysql server firewall-rule create --resource-group myresourcegroup --server mydemoserver --name AllowMyIP --start-ip-address 192.168.0.1 --end-ip-address 192.168.0.1 " 顯然改變了值但它失敗了......我附上下面的代碼:

$conAzure = mysqli_init();
        mysqli_ssl_set($conAzure, NULL, NULL, "pathToSSLCert",NULL, NULL);
        mysqli_real_connect($conAzure, "test.mysql.database.azure.com", "test@test", "test", "proj_sophie", 3306, MYSQLI_CLIENT_SSL);

        // Check connection
        if ($conAzure->connect_error) {
// THE PROGRAM STOPS HERE
            die("Connection failed: " . $conAzure->connect_error);
        }
        echo "Connected successfully";

        $sqlAzure1 = "TRUNCATE TABLE elenco_post;";
            if ($conAzure->query($sqlAzure1) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sqlAzure1 . "<br>" . $conAzure->error;
            }

// inserting in the selected table and fields the data (it's in a for-each so don't worry for the repetitions).
$sqlAzure2 = "INSERT INTO elenco_post ('ID', 'Titolo', 'Contenuto', 'Data', 'URL', 'Ultima_Modifica', 'URL_Immagine', 'Nome_immagine', 'Categoria') VALUES ('".get_the_ID()."',
                    '".get_the_title()."',
                    '".get_the_content()."',
                    '".get_the_date()."',
                    '".get_permalink()."',
                    '".get_the_modified_date()."',
                    '".get_the_post_thumbnail_url(get_the_ID(),'full')."',
                    '".get_post(get_post_thumbnail_id())->post_title."',
                    '".$category."');";
            if ($conAzure->query($sqlAzure2) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sqlAzure2 . "<br>" . $conAzure->error;
            }




謝謝,期待支持!

--start-ip-address 192.168.0.1

對於在 Azure 中運行的客戶端地址,這看起來不太可能。 你的 PHP 在哪里運行? 我懷疑你運行它在一個偽裝的局域網一台家用電腦上-在這種情況下,這不是它連接到Azure的IP地址,只是您使用的地址連接到你的路由器。

嘗試https://whatismyipaddress.com/以了解您現在的地址 - 但如果我是對的,它會定期更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM