如何通过 Api 为 Azure Sql 数据库设置 DTU?
How to set DTU for Azure Sql Database via Api?
我可以通过提及定价层来创建 Azure Sql 数据库。
我正在尝试为数据库设置内存和 DTU。
我找不到正确的 Api ,这是我试过的
PUT : https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<Resource-group-Name>/providers/Microsoft.Sql/servers/<Server-name>/databases/<Database-name>/?api-version=2014-04-01
请求正文:
{
"location": "East Asia",
"properties": {
"edition": "Premium",
"collation":"SQL_Latin1_General_CP1_CI_AS",
"sampleName": "blank database",
"serviceTierAdvisors":[
{
"maxSizeInGB":"150",
"maxDtu":"500"
}
]
}
}
我也没有收到正确的错误消息,谁能指导我在数据库级别设置 DTU 的参数??
您需要使用 API 更新数据库,如 this 文章中所述。
例如:
{
"parameters": {
"subscriptionId": "00000000-1111-2222-3333-444444444444",
"resourceGroupName": "sqlcrudtest-4799",
"serverName": "sqlcrudtest-5961",
"databaseName": "testdb",
"api-version": "2014-04-01",
"parameters": {
"properties": {
"edition": "Standard",
"status": "Online",
"createMode": "Default",
"serviceLevelObjective": "S0",
"collation": "SQL_Latin1_General_CP1_CI_AS",
"maxSizeBytes": "268435456000",
"currentServiceObjectiveId": "f1173c43-91bd-4aaa-973c-54e79e15235b",
"requestedServiceObjectiveId": "dd6d99bb-f193-4ec1-86f2-43d3bccbc49c",
"requestedServiceObjectiveName": "Basic",
"defaultSecondaryLocation": "Japan West",
"earliestRestoreDate": "2017-02-10T01:52:52.923Z",
"containmentState": 2,
"readScale": "Disabled"
}
}
},
"responses": {
"200": {
"body": {
"id": "/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/sqlcrudtest-4799/providers/Microsoft.Sql/servers/sqlcrudtest-5961/databases/testdb",
"name": "testdb",
"type": "Microsoft.Sql/servers/databases",
"location": "Japan East",
"kind": "v12.0,user",
"properties": {
"edition": "Standard",
"status": "Online",
"serviceLevelObjective": "S0",
"collation": "SQL_Latin1_General_CP1_CI_AS",
"creationDate": "2017-02-24T22:39:46.547Z",
"maxSizeBytes": "268435456000",
"currentServiceObjectiveId": "f1173c43-91bd-4aaa-973c-54e79e15235b",
"requestedServiceObjectiveId": "dd6d99bb-f193-4ec1-86f2-43d3bccbc49c",
"requestedServiceObjectiveName": "Basic", "sampleName": null,
"defaultSecondaryLocation": "Japan West",
"earliestRestoreDate": "2017-02-10T01:52:52.923Z",
"elasticPoolName": null,
"containmentState": 2,
"readScale": "Disabled",
"failoverGroupId": null
}
}
},
"202": {}
}
}
在上面的示例中,我从标准 S0 缩减到基本层。
希望对您有所帮助。
Can anyone guide me with the Parameter for setting DTU at Database Level ??
正确的DTU参数应该是requestedServiceObjectiveName。它的类型是枚举。您可以为此 属性.
设置以下值
Basic,
S0, S1, S2, S3
P1, P2, P4, P6, P11, P15
System, System2
ElasticPool
请检查对应的DTU值如下。
Basic(5DTU),
S0(10DTU), S1(20DTU), S2(50DTU), S3(100DTU)
P1(125DTU), P2(250DTU), P4(500DTU), P6(1000DTU), P11(1750DTU), P15(4000DTU)
System, System2
ElasticPool
这是我所做的,它可以增加实例和 DTU。我不得不尝试让它工作,但找不到专门调用 DTU 的可靠文档:
我得到我的不记名令牌,然后调用此方法来增加或减少 dtus。请注意调用 GetUpdateSettingsJson() returns 以下两个 Json 字符串,具体取决于我是增加还是减少 DTU / 实例大小:
增加 JSON: "{"sku": {"name": "Premium","tier":"Premium", "capacity": 250} }"
减少 JSON: "{"sku": {"name": "Standard","tier":"Standard", "capacity": 50} }"
private static async Task<string> UpdateDatabaseSettings()
{
using (HttpClient client = new HttpClient())
{
var updateDtuEndpoint = String.Format(@"https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Sql/servers/{2}/databases/{3}?api-version=2017-10-01-preview",
subscriptionId,
databaseResourceGroup,
databaseServer,
databaseName
);
var accept = "application/json; charset=utf-8";
client.DefaultRequestHeaders.Add("Accept", accept) ;
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue ("Bearer", bearerToken);
//string queryParameters = String.Format(@"resource=https%3A%2F%2Fgraph.microsoft.com%2F&client_id={0}&grant_type=client_credentials&client_secret={1}", clientId, clientSecret);
string jsonUpdateSetting = GetUpdateSettingsJson();
using (var response = await client.PatchAsync(updateDtuEndpoint, new StringContent(jsonUpdateSetting, Encoding.UTF8, "application/json")))
{
if (response.StatusCode == System.Net.HttpStatusCode.Accepted)
{
WriteLine("Capacity Update is Processing");
return "Capacity Update is Processing";
}
else
{
Environment.ExitCode = 1;
WriteLine("Capacity Update failed.");
return "Capacity Update failed. ";
}
}
}
}
EXTRA: 上面的这个方法只是把request放进去,下面的方法会被调用直到它可以确认改变是EFFECTIVE。上面的方法只是请求更改。该方法仅检查 RequestServiceObject = CurrentServiceObject(它不确认 DTU)。
private static async Task<bool> CheckDatabaseSettng()
{
using (HttpClient client = new HttpClient())
{
var checkDatabaseSettings = String.Format(@"https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Sql/servers/{2}/databases/{3}?api-version=2017-10-01-preview",
subscriptionId,
databaseResourceGroup,
databaseServer,
databaseName
);
var accept = "application/json; charset=utf-8";
client.DefaultRequestHeaders.Add("Accept", accept);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", bearerToken);
using (var response = await client.GetAsync(checkDatabaseSettings))
{
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var jsonresult = JObject.Parse(await response.Content.ReadAsStringAsync());
var jsonProperties = jsonresult["properties"];
if (jsonProperties == null)
{
throw new Exception("Could not find properties that are supposed to be returned in this call.");
}
var currentServiceObject = (string)jsonProperties["currentServiceObjectiveName"];
var requestedServiceObject = (string)jsonProperties["requestedServiceObjectiveName"];
string msg = string.Format("currentServiceObjectiveName = {0}; requestedServiceObjectiveName = {1}", currentServiceObject, requestedServiceObject);
WriteLine(msg);
if (currentServiceObject == requestedServiceObject)
{
return true;
}
}
}
}
return false;
}
希望这可以帮助别人节省比我花在上面的时间更多。
第一步:您需要在Azure门户上创建clientId、Token和clientSecret。
使用本教程(有很多只是令人困惑)这个有效。 how to get a token,clientID and client secret
Step2 : 接下来需要选择是使用剩余的API还是SDK。我更喜欢 SDK,但这取决于您(您不需要弄清楚所有 json 值都在使用 SDK)。第 3 步之后假设您选择了 SDK。
要为 SDK 安装 pre-requisites,您需要以下 nuget 包:
Install-Package Microsoft.Azure.Management.Fluent
Install-Package Microsoft.Azure.Management.ResourceManager.Fluent
并且 SQL 需要以下内容:
Microsoft.Azure.Management.Sql.Fluent
您需要第 1 步中的令牌进行身份验证。
Step3:接下来看这个例子如何使用SDK-很好看如何应用。
Azure SQL SDK
最后: 当您具备以上所有条件后,这里是一个代码片段 - 非常出色地完成了工作。
string tenantId = "9596ecae-xxxxxxx";
string clientAppId= "51c28b54-xxxxxxxxx";
string secret = "@w6.Quv--your secret";
credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(clientAppId,
secret,
tenantId,
AzureEnvironment.AzureGlobalCloud);
var azure = Microsoft.Azure.Management.Fluent.Azure
.Configure()
.Authenticate(credentials)
.WithDefaultSubscription();
var database = azure.SqlServers
.GetById("/subscriptions/<your specific>/resourceGroups/<your specific>/providers/Microsoft.Sql/servers/<your specific>")
.Databases.GetById("/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Sql/servers/xxx/databases/xxx");
var result = database.Update()
.WithEdition(DatabaseEditions.Standard)
.WithServiceObjective(ServiceObjectiveName.S0) <-- CHANGE LEVEL OF DB.
.Apply();
SQL 和数据库字符串名称的提示和帮助: 获取正确的字符串来替换上面的代码是很棘手的。一个快捷方式是先调用 List 然后你可以调试并查看将什么放入 GetById 函数中你可以先调用它并查看结果:
azure.SqlServers.List() <-- Debug this and you will see the full names of the Database Servers string names
Then do azure.SqlServer.Databases.List() <-- an array of objects where you can get the string for the database name.
希望对您有所帮助 - 文档太糟糕了。
我可以通过提及定价层来创建 Azure Sql 数据库。
我正在尝试为数据库设置内存和 DTU。
我找不到正确的 Api ,这是我试过的
PUT : https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<Resource-group-Name>/providers/Microsoft.Sql/servers/<Server-name>/databases/<Database-name>/?api-version=2014-04-01
请求正文:
{
"location": "East Asia",
"properties": {
"edition": "Premium",
"collation":"SQL_Latin1_General_CP1_CI_AS",
"sampleName": "blank database",
"serviceTierAdvisors":[
{
"maxSizeInGB":"150",
"maxDtu":"500"
}
]
}
}
我也没有收到正确的错误消息,谁能指导我在数据库级别设置 DTU 的参数??
您需要使用 API 更新数据库,如 this 文章中所述。
例如:
{
"parameters": {
"subscriptionId": "00000000-1111-2222-3333-444444444444",
"resourceGroupName": "sqlcrudtest-4799",
"serverName": "sqlcrudtest-5961",
"databaseName": "testdb",
"api-version": "2014-04-01",
"parameters": {
"properties": {
"edition": "Standard",
"status": "Online",
"createMode": "Default",
"serviceLevelObjective": "S0",
"collation": "SQL_Latin1_General_CP1_CI_AS",
"maxSizeBytes": "268435456000",
"currentServiceObjectiveId": "f1173c43-91bd-4aaa-973c-54e79e15235b",
"requestedServiceObjectiveId": "dd6d99bb-f193-4ec1-86f2-43d3bccbc49c",
"requestedServiceObjectiveName": "Basic",
"defaultSecondaryLocation": "Japan West",
"earliestRestoreDate": "2017-02-10T01:52:52.923Z",
"containmentState": 2,
"readScale": "Disabled"
}
}
},
"responses": {
"200": {
"body": {
"id": "/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/sqlcrudtest-4799/providers/Microsoft.Sql/servers/sqlcrudtest-5961/databases/testdb",
"name": "testdb",
"type": "Microsoft.Sql/servers/databases",
"location": "Japan East",
"kind": "v12.0,user",
"properties": {
"edition": "Standard",
"status": "Online",
"serviceLevelObjective": "S0",
"collation": "SQL_Latin1_General_CP1_CI_AS",
"creationDate": "2017-02-24T22:39:46.547Z",
"maxSizeBytes": "268435456000",
"currentServiceObjectiveId": "f1173c43-91bd-4aaa-973c-54e79e15235b",
"requestedServiceObjectiveId": "dd6d99bb-f193-4ec1-86f2-43d3bccbc49c",
"requestedServiceObjectiveName": "Basic", "sampleName": null,
"defaultSecondaryLocation": "Japan West",
"earliestRestoreDate": "2017-02-10T01:52:52.923Z",
"elasticPoolName": null,
"containmentState": 2,
"readScale": "Disabled",
"failoverGroupId": null
}
}
},
"202": {}
}
}
在上面的示例中,我从标准 S0 缩减到基本层。
希望对您有所帮助。
Can anyone guide me with the Parameter for setting DTU at Database Level ??
正确的DTU参数应该是requestedServiceObjectiveName。它的类型是枚举。您可以为此 属性.
设置以下值Basic,
S0, S1, S2, S3
P1, P2, P4, P6, P11, P15
System, System2
ElasticPool
请检查对应的DTU值如下。
Basic(5DTU),
S0(10DTU), S1(20DTU), S2(50DTU), S3(100DTU)
P1(125DTU), P2(250DTU), P4(500DTU), P6(1000DTU), P11(1750DTU), P15(4000DTU)
System, System2
ElasticPool
这是我所做的,它可以增加实例和 DTU。我不得不尝试让它工作,但找不到专门调用 DTU 的可靠文档:
我得到我的不记名令牌,然后调用此方法来增加或减少 dtus。请注意调用 GetUpdateSettingsJson() returns 以下两个 Json 字符串,具体取决于我是增加还是减少 DTU / 实例大小: 增加 JSON: "{"sku": {"name": "Premium","tier":"Premium", "capacity": 250} }" 减少 JSON: "{"sku": {"name": "Standard","tier":"Standard", "capacity": 50} }"
private static async Task<string> UpdateDatabaseSettings()
{
using (HttpClient client = new HttpClient())
{
var updateDtuEndpoint = String.Format(@"https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Sql/servers/{2}/databases/{3}?api-version=2017-10-01-preview",
subscriptionId,
databaseResourceGroup,
databaseServer,
databaseName
);
var accept = "application/json; charset=utf-8";
client.DefaultRequestHeaders.Add("Accept", accept) ;
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue ("Bearer", bearerToken);
//string queryParameters = String.Format(@"resource=https%3A%2F%2Fgraph.microsoft.com%2F&client_id={0}&grant_type=client_credentials&client_secret={1}", clientId, clientSecret);
string jsonUpdateSetting = GetUpdateSettingsJson();
using (var response = await client.PatchAsync(updateDtuEndpoint, new StringContent(jsonUpdateSetting, Encoding.UTF8, "application/json")))
{
if (response.StatusCode == System.Net.HttpStatusCode.Accepted)
{
WriteLine("Capacity Update is Processing");
return "Capacity Update is Processing";
}
else
{
Environment.ExitCode = 1;
WriteLine("Capacity Update failed.");
return "Capacity Update failed. ";
}
}
}
}
EXTRA: 上面的这个方法只是把request放进去,下面的方法会被调用直到它可以确认改变是EFFECTIVE。上面的方法只是请求更改。该方法仅检查 RequestServiceObject = CurrentServiceObject(它不确认 DTU)。
private static async Task<bool> CheckDatabaseSettng()
{
using (HttpClient client = new HttpClient())
{
var checkDatabaseSettings = String.Format(@"https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Sql/servers/{2}/databases/{3}?api-version=2017-10-01-preview",
subscriptionId,
databaseResourceGroup,
databaseServer,
databaseName
);
var accept = "application/json; charset=utf-8";
client.DefaultRequestHeaders.Add("Accept", accept);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", bearerToken);
using (var response = await client.GetAsync(checkDatabaseSettings))
{
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var jsonresult = JObject.Parse(await response.Content.ReadAsStringAsync());
var jsonProperties = jsonresult["properties"];
if (jsonProperties == null)
{
throw new Exception("Could not find properties that are supposed to be returned in this call.");
}
var currentServiceObject = (string)jsonProperties["currentServiceObjectiveName"];
var requestedServiceObject = (string)jsonProperties["requestedServiceObjectiveName"];
string msg = string.Format("currentServiceObjectiveName = {0}; requestedServiceObjectiveName = {1}", currentServiceObject, requestedServiceObject);
WriteLine(msg);
if (currentServiceObject == requestedServiceObject)
{
return true;
}
}
}
}
return false;
}
希望这可以帮助别人节省比我花在上面的时间更多。
第一步:您需要在Azure门户上创建clientId、Token和clientSecret。 使用本教程(有很多只是令人困惑)这个有效。 how to get a token,clientID and client secret
Step2 : 接下来需要选择是使用剩余的API还是SDK。我更喜欢 SDK,但这取决于您(您不需要弄清楚所有 json 值都在使用 SDK)。第 3 步之后假设您选择了 SDK。
要为 SDK 安装 pre-requisites,您需要以下 nuget 包:
Install-Package Microsoft.Azure.Management.Fluent
Install-Package Microsoft.Azure.Management.ResourceManager.Fluent
并且 SQL 需要以下内容:
Microsoft.Azure.Management.Sql.Fluent
您需要第 1 步中的令牌进行身份验证。
Step3:接下来看这个例子如何使用SDK-很好看如何应用。 Azure SQL SDK
最后: 当您具备以上所有条件后,这里是一个代码片段 - 非常出色地完成了工作。
string tenantId = "9596ecae-xxxxxxx";
string clientAppId= "51c28b54-xxxxxxxxx";
string secret = "@w6.Quv--your secret";
credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(clientAppId,
secret,
tenantId,
AzureEnvironment.AzureGlobalCloud);
var azure = Microsoft.Azure.Management.Fluent.Azure
.Configure()
.Authenticate(credentials)
.WithDefaultSubscription();
var database = azure.SqlServers
.GetById("/subscriptions/<your specific>/resourceGroups/<your specific>/providers/Microsoft.Sql/servers/<your specific>")
.Databases.GetById("/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Sql/servers/xxx/databases/xxx");
var result = database.Update()
.WithEdition(DatabaseEditions.Standard)
.WithServiceObjective(ServiceObjectiveName.S0) <-- CHANGE LEVEL OF DB.
.Apply();
SQL 和数据库字符串名称的提示和帮助: 获取正确的字符串来替换上面的代码是很棘手的。一个快捷方式是先调用 List 然后你可以调试并查看将什么放入 GetById 函数中你可以先调用它并查看结果:
azure.SqlServers.List() <-- Debug this and you will see the full names of the Database Servers string names
Then do azure.SqlServer.Databases.List() <-- an array of objects where you can get the string for the database name.
希望对您有所帮助 - 文档太糟糕了。