本页面介绍如何为 Cloud SQL 配置数据库标志,并列出了可以为实例设置的标志。您可以将数据库标志用于许多操作,包括调整 SQL Server 参数、调整选项以及配置和优化实例。
当您设置、移除或修改数据库实例的标志时,该数据库可能会重启。除非您主动移除,否则实例的此标志值会一直保留。如果实例是副本的来源,并且实例已经重启,那么系统还会重启副本以与实例的当前配置保持一致。
配置数据库标志
设置数据库标志
- 在 Google Cloud 控制台中,选择包含要为其设置数据库标志的 Cloud SQL 实例的项目。
- 打开实例,然后点击修改。
- 向下滚动至标志部分。
- 要在实例上设置先前未设置的标志,请点击添加一项,从下拉菜单中选择该标志,然后设置其值。
- 点击保存以保存更改。
- 在“概览”页的标志下确认更改。
修改实例:
gcloud sql instances patchINSTANCE_NAME --database-flags=FLAG1 =VALUE1 ,FLAG2 =VALUE2
此命令将覆盖之前设置的所有数据库标志。如需保留这些标志并添加新标志,请为您要在实例上设置的所有标志添加值;任何未明确添加的标志都会设置为默认值。对于不带值的标志,请指定标志名称并后接一个等号(“=”)。
例如,如需设置 1204
、remote access
和 remote query timeout (s)
标志,您可以使用以下命令:
gcloud sql instances patchINSTANCE_NAME \ --database-flags="1204"=on,"remote access"=on,"remote query timeout (s)"=300
如需添加数据库标志,请使用 Terraform 资源。
应用更改
如需在 Google Cloud 项目中应用 Terraform 配置,请完成以下部分中的步骤。
准备 Cloud Shell
- 启动 Cloud Shell。
-
设置要在其中应用 Terraform 配置的默认 Google Cloud 项目。
您只需为每个项目运行一次以下命令,即可在任何目录中运行它。
export GOOGLE_CLOUD_PROJECT=
PROJECT_ID 如果您在 Terraform 配置文件中设置显式值,则环境变量会被替换。
准备目录
每个 Terraform 配置文件都必须有自己的目录(也称为“根模块”)。
-
在 Cloud Shell 中,创建一个目录,并在该目录中创建一个新文件。文件名必须具有
.tf
扩展名,例如main.tf
。在本教程中,该文件称为main.tf
。mkdir
DIRECTORY && cdDIRECTORY && touch main.tf -
如果您按照教程进行操作,可以在每个部分或步骤中复制示例代码。
将示例代码复制到新创建的
main.tf
中。(可选)从 GitHub 中复制代码。如果端到端解决方案包含 Terraform 代码段,则建议这样做。
- 查看和修改要应用到您的环境的示例参数。
- 保存更改。
-
初始化 Terraform。您只需为每个目录执行一次此操作。
terraform init
(可选)如需使用最新的 Google 提供程序版本,请添加
-upgrade
选项:terraform init -upgrade
应用更改
-
查看配置并验证 Terraform 将创建或更新的资源是否符合您的预期:
terraform plan
根据需要更正配置。
-
通过运行以下命令并在提示符处输入
yes
来应用 Terraform 配置:terraform apply
等待 Terraform 显示“应用完成!”消息。
- 打开您的 Google Cloud 项目以查看结果。在 Google Cloud 控制台的界面中找到资源,以确保 Terraform 已创建或更新它们。
删除更改
如需删除更改,请执行以下操作:
- 如需停用删除防护,请在 Terraform 配置文件中将
deletion_protection
参数设置为false
。deletion_protection = "false"
- 运行以下命令并在提示符处输入
yes
,以应用更新后的 Terraform 配置:terraform apply
-
运行以下命令并在提示符处输入
yes
,以移除之前使用 Terraform 配置应用的资源:terraform destroy
设置现有数据库的标志:
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- instance-id:实例 ID
HTTP 方法和网址:
PATCH https://sqladmin.googleapis.com/v1/projects/project-id /instances/instance-id
请求 JSON 正文:
{ "settings": { "databaseFlags": [ { "name": "flag_name ", "value": "flag_value " } ] } }
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://sqladmin.googleapis.com/v1/projects/project-id /instances/instance-id "
PowerShell (Windows)
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method PATCH `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/v1/projects/project-id /instances/instance-id " | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/v1/projects/project-id /instances/instance-id ", "status": "PENDING", "user": "user@example.com", "insertTime": "2020-01-21T22:43:37.981Z", "operationType": "UPDATE", "name": "operation-id ", "targetId": "instance-id ", "selfLink": "https://sqladmin.googleapis.com/v1/projects/project-id /operations/operation-id ", "targetProject": "project-id " }
如果数据库存在已配置的现有标志,请修改先前的命令,将其包含在内。PATCH
命令会使用请求中指定的标志覆盖现有标志。
设置现有数据库的标志:
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- instance-id:实例 ID
HTTP 方法和网址:
PATCH https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id /instances/instance-id
请求 JSON 正文:
{ "settings": { "databaseFlags": [ { "name": "flag_name ", "value": "flag_value " } ] } }
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id /instances/instance-id "
PowerShell (Windows)
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method PATCH `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id /instances/instance-id " | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id /instances/instance-id ", "status": "PENDING", "user": "user@example.com", "insertTime": "2020-01-21T22:43:37.981Z", "operationType": "UPDATE", "name": "operation-id ", "targetId": "instance-id ", "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id /operations/operation-id ", "targetProject": "project-id " }
如果数据库存在已配置的现有标志,请修改先前的命令,将其包含在内。PATCH
命令会使用请求中指定的标志覆盖现有标志。
将所有标志恢复为其默认值
- 在 Google Cloud 控制台中,选择包含要清除所有标志的 Cloud SQL 实例的项目。
- 打开实例,然后点击修改。
- 打开数据库标志部分。
- 点击显示的所有标志旁的 X。
- 点击保存以保存更改。
将实例上的所有标志恢复为其默认值:
gcloud sql instances patchINSTANCE_NAME \ --clear-database-flags
系统会提示您确认实例将重启。
清除现有实例的所有标志:
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- instance-id:实例 ID
HTTP 方法和网址:
PATCH https://sqladmin.googleapis.com/v1/projects/project-id /instances/instance-id
请求 JSON 正文:
{ "settings": { "databaseFlags": [] } }
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://sqladmin.googleapis.com/v1/projects/project-id /instances/instance-id "
PowerShell (Windows)
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method PATCH `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/v1/projects/project-id /instances/instance-id " | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/v1/projects/project-id /instances/instance-id ", "status": "PENDING", "user": "user@example.com", "insertTime": "2020-01-21T22:43:37.981Z", "operationType": "UPDATE", "name": "operation-id ", "targetId": "instance-id ", "selfLink": "https://sqladmin.googleapis.com/v1/projects/project-id /operations/operation-id ", "targetProject": "project-id " }
清除现有实例的所有标志:
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- instance-id:实例 ID
HTTP 方法和网址:
PATCH https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id /instances/instance-id
请求 JSON 正文:
{ "settings": { "databaseFlags": [] } }
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id /instances/instance-id "
PowerShell (Windows)
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method PATCH `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id /instances/instance-id " | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id /instances/instance-id ", "status": "PENDING", "user": "user@example.com", "insertTime": "2020-01-21T22:43:37.981Z", "operationType": "UPDATE", "name": "operation-id ", "targetId": "instance-id ", "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id /operations/operation-id ", "targetProject": "project-id " }
确定已为实例设置的数据库标志
如需查看已为 Cloud SQL 实例设置的标志:
- 在 Google Cloud 控制台中,选择包含要查看其已设置数据库标志的 Cloud SQL 实例的项目。
- 选择实例,打开其实例概览页面。
数据库标志部分列出了已设置的数据库标志。
获取实例状态:
gcloud sql instances describeINSTANCE_NAME
在输出中,数据库标志作为 databaseFlags
集合列在 settings
下。要详细了解输出中标志的表示法,请参阅实例资源表示法。
列出实例的已配置标志:
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- instance-id:实例 ID
HTTP 方法和网址:
GET https://sqladmin.googleapis.com/v1/projects/project-id /instances/instance-id
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://sqladmin.googleapis.com/v1/projects/project-id /instances/instance-id "
PowerShell (Windows)
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/v1/projects/project-id /instances/instance-id " | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
{ "settings": { "authorizedGaeApplications": [], "tier": "machine-type ", "kind": "sql#settings", "availabilityType": "REGIONAL", "pricingPlan": "PER_USE", "replicationType": "SYNCHRONOUS", "activationPolicy": "ALWAYS", "ipConfiguration": { "privateNetwork": "projects/project-id /global/networks/default", "authorizedNetworks": [], "ipv4Enabled": false }, "locationPreference": { "zone": "zone ", "kind": "sql#locationPreference" }, "databaseFlags": [ { "name": "general_log", "value": "on" } ], "dataDiskType": "PD_SSD", "maintenanceWindow": { "kind": "sql#maintenanceWindow", "hour": 0, "day": 0 }, "backupConfiguration": { "startTime": "03:00", "kind": "sql#backupConfiguration", "enabled": true, "binaryLogEnabled": true }, "settingsVersion": "54", "storageAutoResizeLimit": "0", "storageAutoResize": true, "dataDiskSizeGb": "10" } }
在输出中,查找 databaseFlags
字段。
列出实例的已配置标志:
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- instance-id:实例 ID
HTTP 方法和网址:
GET https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id /instances/instance-id
如需发送您的请求,请展开以下选项之一:
curl(Linux、macOS 或 Cloud Shell)
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id /instances/instance-id "
PowerShell (Windows)
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id /instances/instance-id " | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
响应
{ "settings": { "authorizedGaeApplications": [], "tier": "machine-type ", "kind": "sql#settings", "availabilityType": "REGIONAL", "pricingPlan": "PER_USE", "replicationType": "SYNCHRONOUS", "activationPolicy": "ALWAYS", "ipConfiguration": { "privateNetwork": "projects/project-id /global/networks/default", "authorizedNetworks": [], "ipv4Enabled": false }, "locationPreference": { "zone": "zone ", "kind": "sql#locationPreference" }, "databaseFlags": [ { "name": "general_log", "value": "on" } ], "dataDiskType": "PD_SSD", "maintenanceWindow": { "kind": "sql#maintenanceWindow", "hour": 0, "day": 0 }, "backupConfiguration": { "startTime": "03:00", "kind": "sql#backupConfiguration", "enabled": true, "binaryLogEnabled": true }, "settingsVersion": "54", "storageAutoResizeLimit": "0", "storageAutoResize": true, "dataDiskSizeGb": "10" } }
在输出中,查找 databaseFlags
字段。
支持的标志
Cloud SQL 仅支持本部分列出的标志。
Cloud SQL 标志 | 类型 可接受值及备注 |
是否需要 重启? |
---|---|---|
1204(跟踪记录标志) | boolean on | off |
否 |
1222(跟踪记录标志) | boolean on | off |
否 |
1224(跟踪记录标志) | boolean on | off |
否 |
2528(跟踪记录标志) | boolean on | off |
否 |
3205(跟踪记录标志) | boolean on | off |
否 |
3226(跟踪记录标志) | boolean on | off |
否 |
3625(跟踪记录标志) | boolean on | off |
是 |
4199(跟踪记录标志) | boolean on | off |
否 |
4616(跟踪记录标志) | boolean on | off |
否 |
7806(跟踪记录标志) | boolean on | off |
是 |
access check cache bucket count | integer 0 至 65536 |
否 |
access check cache quota | integer 0 至 2147483647 |
否 |
affinity mask | integer 2147483648 至 2147483647 |
否 |
agent xps | boolean on | off |
否 |
automatic soft-numa disabled | boolean on | off |
是 |
cloud sql xe 存储桶名称 | string 存储桶名称必须以 gs:// 前缀开头。 |
否 |
cloud sql xe 输出总磁盘大小 (mb) | integer 10 至 512 |
否 |
cloud sql xe 文件保留时间(分钟) | integer 0 至 10080 |
否 |
cloud sql xe 上传间隔时间(分钟) | integer 1 至 60 |
否 |
cloudsql enable linked servers | boolean on | off |
否 |
cost threshold for parallelism | integer 0 至 32767 |
否 |
contained database authentication | boolean on | off
|
否 |
cross db ownership chaining | boolean on | off
|
否 |
cursor threshold | integer -1 至 2147483647 |
否 |
default full-text language | integer 0 至 2147483647 |
否 |
默认语言 | integer 0 至 32 |
否 |
default trace enabled | boolean on | off |
否 |
disallow results from triggers | boolean on | off |
否 |
external scripts enabled | boolean on | off |
是 |
ft crawl bandwidth (max) | integer 0 至 32767 |
否 |
ft crawl bandwidth (min) | integer 0 至 32767 |
否 |
ft notify bandwidth (max) | integer 0 至 32767 |
否 |
ft notify bandwidth (min) | integer 0 至 32767 |
否 |
fill factor (%) | integer 0 至 100 |
否 |
index create memory (kb) | integer 704 至 2147483647 |
否 |
locks | integer 5000 至 2147483647 |
是 |
max server memory (mb) | integer 1000 ... 2147483647
Cloud SQL 可能会根据 Microsoft 的建议值,在实例上为此标志设置值。如需了解详情,请参阅特殊标志。 |
否 |
max text repl size (b) | integer -1 至 2147483647 |
否 |
max worker threads | integer 128 至 65535 |
否 |
nested triggers | boolean on | off |
否 |
optimize for ad hoc workloads | boolean on | off |
否 |
ph timeout (s) | integer 1 至 3600 |
否 |
query governor cost limit | integer 0 至 2147483647 |
否 |
query wait (s) | integer -1 至 2147483647 |
否 |
recovery interval (min) | integer 0 至 32767 |
否 |
remote access | boolean on | off |
是 |
remote login timeout (s) | integer 0 至 2147483647 |
否 |
remote query timeout (s) | integer 0 至 2147483647 |
否 |
transform noise words | boolean on | off |
否 |
two digit year cutoff | integer 1753 至 9999 |
否 |
user connections | integer 0 、10 至 32767 |
是 |
user options | integer 0 至 32767 |
否 |
特殊标志
本部分包含有关 Cloud SQL for SQL Server 标志的更多信息。
max server memory (mb)
此标志会限制 Cloud SQL 可为其内部池分配的内存量。一般建议将此标志的值设置为大约 80%,以防止 SQL Server 耗尽 Cloud SQL 实例的所有可用内存。如果您将该值设置为高于 80%,则可能会因内存不足问题而导致不稳定、性能下降和数据库崩溃。
如果您未为此标志设置值,Cloud SQL 会根据实例的 RAM 大小自动管理此值。此外,如果您调整实例大小,Cloud SQL 会自动调整此标志的值,以满足我们对新实例大小的建议。这样可以确保数据库高效利用资源,防止实例过度分配、崩溃和性能下降。
问题排查
问题 | 问题排查 |
---|---|
您想要修改 Cloud SQL 实例的时区。 |
如需了解如何更新实例的时区,请参阅实例设置。 在 Cloud SQL for SQL Server 中,您可以使用 |
后续步骤
- 详细了解 SQL Server 配置选项。