使用授权网络进行授权
本页面介绍了如何使用已获授权的网络设置连接到使用 IP 地址的 Cloud SQL 实例。
配置已获授权的网络
在以下情况下,您的客户端应用的 IP 地址或地址范围必须配置为 authorized networks
:
- 您的客户端应用通过其公共 IP 地址直接连接到 Cloud SQL 实例。
- 您的客户端应用通过其专用 IP 地址直接连接到 Cloud SQL 实例,并且您的客户端的 IP 地址是一个非 RFC 1918 地址
此 IP 地址可以是单一端点,也可以包含 CIDR 表示法的范围。
控制台
-
在 Google Cloud Console 中,转到 Cloud SQL 实例页面。
转到“Cloud SQL 实例”
-
如需打开实例的概览页面,请点击实例名称。
- 从 SQL 导航菜单中选择连接。
- 选择公共 IP 复选框。
- 点击添加网络。
- 在名称字段中,为新网络输入名称。
- 在网络*字段中,输入要允许连接的 IP 地址或地址范围。
使用 CIDR 表示法。
- 点击完成。
- 点击保存以更新实例。
gcloud
配置授权网络会替换现有的授权网络列表。
gcloud sql instances patch INSTANCE_ID \
--authorized-networks=NETWORK_RANGE_1,NETWORK_RANGE_2...
REST v1
配置授权网络会替换现有的授权网络列表。
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- instance-id:实例 ID
- network_range_1:已获授权的 IP 地址或范围
- network_range_2:另一个已获授权的 IP 地址或范围
HTTP 方法和网址:
PATCH https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id
请求 JSON 正文:
{
"settings":
{
"ipConfiguration":
{
"authorizedNetworks":
[{"value": "network_range_1"}, {"value": network_range_2"}]
}
}
}
如需发送您的请求,请展开以下选项之一:
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"
}
REST v1beta4
配置授权网络会替换现有的授权网络列表。
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- instance-id:实例 ID
- network_range_1:已获授权的 IP 地址或范围
- network_range_2:另一个已获授权的 IP 地址或范围
HTTP 方法和网址:
PATCH https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id
请求 JSON 正文:
{
"settings":
{
"ipConfiguration":
{
"authorizedNetworks":
[{"value": "network_range_1"}, {"value": network_range_2"}]
}
}
}
如需发送您的请求,请展开以下选项之一:
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"
}
限制
某些 IP 地址范围不能添加为已获授权的网络。
地址范围 |
备注 |
127.0.0.0/8 |
环回地址范围 |
10.0.0.0/8 |
RFC 1918 地址范围。这些地址由 Cloud SQL 自动并隐式包含在已获授权的网络中 |
172.16.0.0/12 |
RFC 1918 地址范围。这些地址由 Cloud SQL 自动并隐式包含在已获授权的网络中 |
172.17.0.0/16 |
为 Docker 桥接网络预留 |
192.168.0.0/16 |
RFC 1918 地址范围。这些地址由 Cloud SQL 自动并隐式包含在已获授权的网络中 |
0.0.0.0/8 |
RFC 3330 null 网络 |
169.254.0.0/16 |
RFC 3927 和 RFC 2373,链接本地网络 |
192.0.2.0/24 |
RFC 3330 和 RFC 3849,文档网络 |
224.0.0.0/4 |
RFC 3330,多播网络 |
240.0.0.0/4 |
此地址块(以前称为 E 类地址空间)留待将来使用;请参阅 RFC 1112 的第 4 部分。 |
后续步骤
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-05-22 UTC.
[{
"type": "thumb-down",
"id": "hardToUnderstand",
"label":"很难理解"
},{
"type": "thumb-down",
"id": "incorrectInformationOrSampleCode",
"label":"信息或示例代码不正确"
},{
"type": "thumb-down",
"id": "missingTheInformationSamplesINeed",
"label":"没有我需要的信息/示例"
},{
"type": "thumb-down",
"id": "translationIssue",
"label":"翻译问题"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"其他"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"易于理解"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"解决了我的问题"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"其他"
}]