drjyvoi734793 2017-01-11 22:28
浏览 170
已采纳

Azure:使用MySQL in-app(预览版)的Wordpress数据库的默认用户/通行证在哪里?

I just started an Azure hosted WordPress App Service and chose to do the MySQL in-app(preview) option for the database. For those that aren't aware, this allows me to run the MySQL server side-by-side with my Web application within the same environment.

However, I'm running into a problem with the way I am choosing to make MySQL queries.

I want to reuse code from a different PHP project where the MySQL calls are in the form of PDO statements, like so:

try {
    $db = new PDO('mysql:host=localhost;dbname=localdb;charset=utf8',
                    'user',
                    'pass');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $ex) {
    echo "did not connect...";
}

$sth = $db->prepare("SELECT *FROM MyTable;");
$sth->execute();

I can't make these calls unless I have a username and password to do so.

The PHPMyAdmin side-panel looks like this in the MySQL in-app(preview):

PHPMyAdmin side-panel for MySQL in-app preview

And if I pull up the user accounts, this is what I see:

PHPMyAdmin User Accounts

I'm lost when it comes to what user and pass I should use, and if I should even be using localdb as my db (that's where all the wordpress tables are listed).

All in all, I'm just trying to pull information from the database using PDO statements and need to know how to go about it.

  • 写回答

3条回答 默认 最新

  • duanfu2562 2017-01-12 05:13
    关注

    The connection string can be seen in D:\home\data\mysql\MYSQLCONNSTR_localdb.txt. You can locate this file through Kudu Debug Console which could be accessed via https://<yourwebsitename>.scm.azurewebsites.net/DebugConsole.

    The file content looks something like:

    Database=localdb;Data Source=127.0.0.1:54306;User Id=azure;Password=6#vWHD_$
    

    Following is a sample code snippet using PDO to connect MySQL in-app.

    $dsn = 'mysql:dbname=localdb;host=127.0.0.1:54306;charset=utf8';
    $user = 'azure';
    $password = '6#vWHD_$';
    
    try {
        $dbh = new PDO($dsn, $user, $password);
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
        exit;
    }
    
    echo "Success: A proper connection to MySQL was made!";
    

    Important update:

    From https://social.msdn.microsoft.com/Forums/azure/en-US/4c582216-bc1b-48b0-b80b-87ae540c3d05/php-azure-mysql-inapp-changed-ports-randomly

    A VM can host multiple WebApps; hence multiple in-app MySQL processes. When we start MySql process, we attempt to use the same port as before. However, it may be taken by other service or other in-app MySQL. As a result, the port may change. In addition, web app can be moved from one VM to another and the set of available ports will be different.

    In order to write the stable client app, do make sure you read the connection info from env variable. See this for more details.

    So we should get the connection string from env variable in PHP like below:

    $connectstr_dbhost = '';
    $connectstr_dbname = '';
    $connectstr_dbusername = '';
    $connectstr_dbpassword = '';
    
    foreach ($_SERVER as $key => $value) {
        if (strpos($key, "MYSQLCONNSTR_localdb") !== 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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题