Azure:如何正确使用 PHP webjob 来更改 SQL 数据库?
Azure: How to correctly use a PHP webjob to make changes to SQL database?
目标:使用 PHP webjob 连接和更改 Azure 中的 SQL 数据库 table。
我正在尝试将包含 .php
和 .json
文件的 .zip
文件上传到我有 运行ning 的应用程序服务内的 webjobs 设置在 Azure 上。
我认为当我将 webjob 作为 .zip
上传到 webjobs 时,我在 PHP 文件中编码 PDO-SQL 连接的方式有问题, 状态总是 "Pending Restart".
这是我的 .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();
}
?>
那么我的 .json
文件只是一个调度程序:
{
"schedule": "0 */5 * * * *"
}
这是我正在上传的 .zip
文件中仅有的两个文件。
为了解释 PHP 代码,我正在尝试通过 windows 身份验证进行连接(不需要 user/pass)。也许我也做错了。
谁有办法做到这一点?我真的很感激提供有关如何更改我的代码以使该 webjob 实际上 运行.
的逐步或建议
考虑这个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);
?>
预期输出:
(可以在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). Maybe I'm doing this wrong too.
虽然 Azure SQL 确实支持 Windows 身份验证,但我不确定您所说的 "no need for user/pass" 是什么意思。 Windows 身份验证意味着 "take the credentials of whomever this process is running as and attempt authentication against the SQL server"。由于您是 运行 您的 Webjob 作为沙箱提供的随机用户,Windows 身份验证没有多大意义。
来自 Kudu 的 Process Explorer:
这是 Windows 使用 Azure SQL 进行身份验证的有效方案:
您有一个本地托管应用程序,它使用 Active Directory 对您的本地 SQL 服务器进行身份验证。您需要迁移到 Azure SQL。您没有更改 SQL 的身份验证方法的选项。因此,您 dirsync 您的目录到 Azure AD 并使用 Windows 身份验证连接到 Azure SQL.
有关 Windows Azure 身份验证的更多信息 SQL:
https://azure.microsoft.com/en-us/documentation/articles/sql-database-aad-authentication/
目标:使用 PHP webjob 连接和更改 Azure 中的 SQL 数据库 table。
我正在尝试将包含 .php
和 .json
文件的 .zip
文件上传到我有 运行ning 的应用程序服务内的 webjobs 设置在 Azure 上。
我认为当我将 webjob 作为 .zip
上传到 webjobs 时,我在 PHP 文件中编码 PDO-SQL 连接的方式有问题, 状态总是 "Pending Restart".
这是我的 .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();
}
?>
那么我的 .json
文件只是一个调度程序:
{
"schedule": "0 */5 * * * *"
}
这是我正在上传的 .zip
文件中仅有的两个文件。
为了解释 PHP 代码,我正在尝试通过 windows 身份验证进行连接(不需要 user/pass)。也许我也做错了。
谁有办法做到这一点?我真的很感激提供有关如何更改我的代码以使该 webjob 实际上 运行.
的逐步或建议考虑这个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);
?>
预期输出:
(可以在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). Maybe I'm doing this wrong too.
虽然 Azure SQL 确实支持 Windows 身份验证,但我不确定您所说的 "no need for user/pass" 是什么意思。 Windows 身份验证意味着 "take the credentials of whomever this process is running as and attempt authentication against the SQL server"。由于您是 运行 您的 Webjob 作为沙箱提供的随机用户,Windows 身份验证没有多大意义。
来自 Kudu 的 Process Explorer:
这是 Windows 使用 Azure SQL 进行身份验证的有效方案:
您有一个本地托管应用程序,它使用 Active Directory 对您的本地 SQL 服务器进行身份验证。您需要迁移到 Azure SQL。您没有更改 SQL 的身份验证方法的选项。因此,您 dirsync 您的目录到 Azure AD 并使用 Windows 身份验证连接到 Azure SQL.
有关 Windows Azure 身份验证的更多信息 SQL:
https://azure.microsoft.com/en-us/documentation/articles/sql-database-aad-authentication/