简体   繁体   English

Azure:如何正确使用PHP Web作业对SQL数据库进行更改?

[英]Azure: How to correctly use a PHP webjob to make changes to SQL database?

GOAL : To use a PHP webjob to connect and alter SQL database table in Azure. 目标 :使用PHP网络作业连接和更改Azure中的SQL数据库表。

I'm trying to upload a .zip file containing a .php and a .json file to the webjobs settings inside of an app service I have running on Azure. 我想上传一个.zip包含文件.php.json我已经在Azure上运行的应用程序服务的内部文件到webjobs设置。

I believe there's something wrong with the way I'm coding the PDO-SQL connection inside of the PHP file, when I upload the webjob as a .zip into webjobs, the Status is always "Pending Restart". 我相信我在PHP文件中编码PDO-SQL连接的方式有问题,当我将Webjob作为.zip上载到webjobs中时,状态始终为“待处理的重新启动”。

Here's what I have in my .php file: 这是我的.php文件中的内容:

<?php

$conn = new PDO ( "sqlsrv:server = mydb.database.windows.net,1433; Database = myappservices");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
catch ( PDOException $e ) {
  print( "Error connecting to SQL Server." );
}
$connectionInfo = array("Database" => "myappservices");
$serverName = "mydb.database.windows.net,1433";
$conn = sqlsrv_connect($serverName, $connectionInfo);

if ($conn) {
  $stf = $conn->prepare("INSERT INTO MyTable
                        VALUES ('boom', 1, 2);");
  $stf->execute();
}

?>

Then my .json file is just a scheduler: 然后我的.json文件只是一个调度程序:

{
    "schedule": "0 */5 * * * *"
}

These are the only two files in my .zip file I'm uploading. 这是我要上传的.zip文件中仅有的两个文件。

To explain the PHP code, I'm trying to connect via windows authentication (no need for user/pass). 为了解释PHP代码,我试图通过Windows身份验证进行连接(不需要用户/密码)。 Maybe I'm doing this wrong too. 也许我也做错了。

Anyone have any ways to do this? 有人有办法吗? I would really appreciate giving a step by step or suggestions as to how to change my code to get this webjob to actually run. 我真的很乐意就如何更改代码以使此webjob实际运行给出逐步或建议。

Consider this job.php : 考虑一下这个job.php

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

try {
    // DON'T HARDCODE CREDENTIALS, pull from Application Settings.
    // In Azure App Service, Application Settings are exposed as
    // environment variables.
    //
    // i.e. $db_user = getenv("DB_USER");
    //
    $conn = new PDO ("sqlsrv:server = poqfsXXXX.database.windows.net,1433;
                     Database = MobileApp_db",
                     "Username", "P@ssw0rd");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
    print("Error connecting to SQL Server: " + $e);
}

$stmt = $conn->prepare("select * from todoitems");
$stmt->execute();
while ($row = $stmt->fetch()) {
    print_r($row);
}

unset($conn);
unset($stmt);

?>    

Expected output: 预期产量:
(You can test in Kudu: https://{sitename}.scm.azurewebsites.net/DebugConsole ) (您可以在Kudu中进行测试: https:// {sitename} .scm.azurewebsites.net / DebugConsole

d:\home\site\tests> "d:\program files (x86)\php\v5.6\php.exe" job.php

Array
(
    [Id] => d4657cff-09a2-4a8a-b1d2-462c2c42a9f0
    [0] => d4657cff-09a2-4a8a-b1d2-462c2c42a9f0
    [Text] => From Azure SQL
    [1] => From Azure SQL
    [Complete] => 0
    [2] => 0
    [Version] => 0000000000001825
    [3] => 0000000000001825
    [CreatedAt] => 2016-06-17 10:11:17.1167267 +00:00
    [4] => 2016-06-17 10:11:17.1167267 +00:00
    [UpdatedAt] => 2016-06-17 10:11:17.1167267 +00:00
    [5] => 2016-06-17 10:11:17.1167267 +00:00
    [Deleted] => 0
    [6] => 0
)
Array
(
    ...
)
...

To explain the PHP code, I'm trying to connect via windows authentication (no need for user/pass). 为了解释PHP代码,我试图通过Windows身份验证进行连接(不需要用户/密码)。 Maybe I'm doing this wrong too. 也许我也做错了。

While Azure SQL does support Windows Authentication, i'm not exactly sure what you mean by "no need for user/pass". 尽管Azure SQL确实支持Windows身份验证,但我不确定“不需要用户/通过”的含义。 Windows Authentication means "take the credentials of whomever this process is running as and attempt authentication against the SQL server" . Windows身份验证的意思是“获取运行此过程的任何人的凭据,然后尝试对SQL Server进行身份验证” Since you're running your Webjob as a random user provided by the sandbox, Windows Authentication doesn't make much sense. 由于您正在以沙箱提供的随机用户身份运行Webjob,因此Windows身份验证没有多大意义。

From Kudu's Process Explorer: 从Kudu的Process Explorer:

流程浏览器

Here's one valid scenario for Windows Authentication with Azure SQL: 这是使用Azure SQL进行Windows身份验证的一种有效方案:

You have an on-prem hosted application that uses Active Directory to authenticate to your on-prem SQL Server. 您有一个本地托管的应用程序,该应用程序使用Active Directory来对本地SQL Server进行身份验证。 You have a requirement to move to Azure SQL. 您需要迁移到Azure SQL。 You do not have the option to change the authentication method for SQL. 您没有选择更改SQL的身份验证方法的选项。 So you dirsync your directory to Azure AD and use Windows Authentication to connect to Azure SQL. 因此,您将目录直接同步到Azure AD,并使用Windows身份验证连接到Azure SQL。

More on Windows Authentication in Azure SQL: 有关Azure SQL中Windows身份验证的更多信息:
https://azure.microsoft.com/en-us/documentation/articles/sql-database-aad-authentication/ https://azure.microsoft.com/zh-CN/documentation/articles/sql-database-aad-authentication/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM