概览
在本页中,您将学习如何使用 Database Migration Service API 管理 PostgreSQL 源数据库和 PostgreSQL 目标数据库的连接配置文件。
您可以通过两种方式使用 Database Migration Service API。您可以进行 REST API 调用,也可以使用 Google Cloud CLI (CLI)。
如需查看有关使用 gcloud 管理 Database Migration Service 连接配置文件的概要信息,请点击此处。
为 PostgreSQL 源数据库创建连接配置文件
以下示例展示了为 PostgreSQL 源数据库创建连接配置文件的请求。
REST
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- region:项目区域
- connection-profile-id:连接配置文件 ID
- connection-profile-display-name:连接配置文件显示名称
- host-ip-address:来源 IP 地址
- username:数据库用户名
- password:数据库用户密码
- client-key:与客户端证书关联的未加密 PKCS#1 或 PKCS#8 PEM 编码的私钥。如果使用此字段,则
clientCertificate字段为必填字段。 - client-certificate:副本将用来对源数据库服务器进行身份验证的 x509 PEM 编码证书。如果使用此字段,则必须使用
clientKey字段。 - ca-certificate:必填。签署源数据库服务器证书的 CA 的 x509 PEM 编码证书。副本将使用此证书验证其连接到了正确的主机。
HTTP 方法和网址:
POST https://datamigration.googleapis.com/v1/projects/project-id/locations/region/connectionProfiles?connectionProfileId=connection-profile-id
请求 JSON 正文:
{
"displayName": "connection-profile-display-name",
"postgres": {
"host": "host-ip-address",
"port": 5432,
"username": "username",
"password": "password",
"ssl": {
"clientKey": "client-key",
"clientCertificate": "client-certificate",
"caCertificate": "ca-certificate"
}
}
}
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/project-id/locations/region/operations/operation-1591973161667-5a7e422cb0ba4-3004980d-2ae97165",
"metadata": {
"@type": "type.googleapis.com/google.cloud.clouddms.v1.OperationMetadata",
"createTime": "2020-06-12T14:46:01.744267779Z",
"target": "projects/project-id/locations/region/connectionProfiles/connection-profile-id",
"verb": "create",
"requestedCancellation": false,
"apiVersion": "v1"
},
"done": false
}
gcloud
如需查看有关使用 gcloud 创建 Database Migration Service 连接配置文件的概要信息,请点击此处。
创建后,您可以调用 connectionProfiles/get 方法来查看连接配置文件的相关信息。
REST
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- region:项目区域
- connection-profile-id:连接配置文件 ID
HTTP 方法和网址:
GET https://datamigration.googleapis.com/v1/projects/project-id/locations/region/connectionProfiles/connection-profile-id
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/project-id/locations/region/connectionProfiles/connection-profile-id",
"createTime": "2019-12-22T16:17:37.159786963Z",
"updateTime": "2019-12-24T13:13:39.455857411Z",
"state": "READY",
"displayName": "connection-profile-display-name",
"postgres": {
"host": "host-ip-address",
"port": 5432,
"username": "username"
}
}
gcloud
如需详细了解如何使用 gcloud 检索连接配置文件的相关信息,请点击此处。
为 Cloud SQL for PostgreSQL 源数据库创建连接配置文件
以下示例展示了为 Cloud SQL for PostgreSQL 源数据库创建连接配置文件的请求。此示例使用 PostgreSQL 连接配置文件,因为它连接到 PostgreSQL 数据库引擎,而不是 Cloud SQL 管理层。
若要使用 Cloud SQL 在源数据库和副本之间创建配对,您必须提供 Cloud SQL 数据库的实例 ID。您可以使用 Cloud SQL Admin API 的 databases/list 方法找到实例 ID 值。
REST
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- region:项目区域
- connection-profile-id:连接配置文件 ID
- connection-profile-display-name:连接配置文件显示名称
- host-ip-address:来源 IP 地址
- username:数据库用户名
- password:数据库用户密码
- cloud-sql-instance-id:Cloud SQL 实例 ID
HTTP 方法和网址:
POST https://datamigration.googleapis.com/v1/projects/project-id/locations/region/connectionProfiles?connectionProfileId=connection-profile-id
请求 JSON 正文:
{
"displayName": "connection-profile-display-name",
"postgres": {
"host": "host-ip-address",
"port": 5432,
"username": "username",
"password": "password",
"cloud_sql_id": "cloud-sql-instance-id"
}
}
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/project-id/locations/region/operations/operation-1591973161667-5a7e422cb0ba4-3004980d-2ae97165",
"metadata": {
"@type": "type.googleapis.com/google.cloud.clouddms.v1.OperationMetadata",
"createTime": "2020-06-12T14:46:01.744267779Z",
"target": "projects/project-id/locations/region/connectionProfiles/connection-profile-id",
"verb": "create",
"requestedCancellation": false,
"apiVersion": "v1"
},
"done": false
}
gcloud
如需查看有关使用 gcloud 创建 Database Migration Service 连接配置文件的概要信息,请点击此处。
如需详细了解如何使用 gcloud 为 Cloud SQL for PostgreSQL 源数据库创建连接配置文件,请点击此处。
为 Cloud SQL for PostgreSQL 目标数据库创建连接配置文件
以下示例显示了为 Cloud SQL for PostgreSQL 目标位置创建连接配置文件的请求。Database Migration Service 会使用此请求中的信息创建新的 Cloud SQL for PostgreSQL 实例。
REST
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- region:项目区域
- connection-profile-id:连接配置文件 ID
- connection-profile-display-name:连接配置文件显示名称
- database-version:数据库版本。例如,POSTGRES_12。
- tier:机器类型。例如,db-custom-1-4096。
- data-disk-type:数据磁盘类型。例如,PD_SSD。
- data-disk-size-gb:数据磁盘大小(以 GB 为单位)。例如,20。
- zone:项目区域中的可用区
- cmek_key_name:可选:客户管理的加密密钥 (CMEK) 的完整路径和名称。例如,“projects/project-id/locations/location/keyRings/ring/cryptoKeys/customer-managed-encryption-key”。
Google Cloud 中存储的所有静态数据,是使用我们用于自己的加密数据的同一强化密钥管理系统进行加密的。这些密钥管理系统提供严格的密钥访问控制和审核机制,并使用 AES-256 加密标准加密用户的静态数据。无需进行任何设置、配置或管理。对于对加密材料的合规性或当地性没有特定要求的用户, Google Cloud的默认静态加密是最佳选择。
如果您需要更好地控制用于对 Google Cloud 项目中的静态数据进行加密的密钥,则 Database Migration Service 允许您使用在 Cloud Key Management Service (KMS) 中管理的加密密钥来保护您的数据。 这些加密密钥称为客户管理的加密密钥 (CMEK)。当您使用 CMEK 保护 Database Migration Service 中的数据时,CMEK 由您控制。
cmek_key_name 参数与拥有一个 CMEK 相关联,Database Migration Service 可以使用该 CMEK 来加密从源迁移到目标的数据。CMEK 由 customer-managed-encryption-key 占位符表示。
ring 占位符表示 CMEK 的密钥环。密钥环在特定 Google Cloud 位置组织密钥,并允许您管理对密钥组的访问权限控制。密钥环的名称在 Google Cloud 项目中不必是唯一的,但在给定位置必须是唯一的。如需详细了解密钥环,请参阅 Cloud KMS 资源。
在创建连接配置文件时,Database Migration Service 将验证 CMEK 是否存在,以及 Database Migration Service 是否有权使用密钥。
如果未满足其中任何一个条件,则系统会返回以下错误消息:
CMEK_DOES_NOT_EXIST_OR_MISSING_PERMISSIONS
如需解决此问题,请验证您提供的密钥是否存在,以及 Database Migration Service 服务账号是否拥有该密钥的 cloudkms.cryptoKeys.get 权限。
如果您希望使用 Google Cloud的内部密钥管理系统(而非 CMEK)来加密数据,请不要在 API 请求中添加 cmek_key_name 参数和值。
HTTP 方法和网址:
POST https://datamigration.googleapis.com/v1/projects/project-id/locations/region/connectionProfiles?connectionProfileId=connection-profile-id
请求 JSON 正文:
{
"displayName": "connection-profile-display-name",
"cloudsql": {
"settings": {
"databaseVersion": "database-version",
"tier": "machine-type",
"storageAutoResizeLimit": 0,
"activationPolicy": "ALWAYS",
"ipConfig":
{
"authorizedNetworks": [],
"enableIpv4": true,
"privateNetwork": null
},
"autoStorageIncrease": false,
"dataDiskType": "data-disk-type",
"dataDiskSizeGb": "data-disk-size",
"zone": "zone",
"sourceId": "projects/project-id/locations/region/connectionProfiles/connection-profile-id",
"cmek_key_name": "projects/project-id/locations/location/keyRings/ring/cryptoKeys/customer-managed-encryption-key"
}
}
}
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/project-id/locations/region/operations/operation-1591975557292-5a7e4b195623c-e350e3da-713dee7d",
"metadata": {
"@type": "type.googleapis.com/google.cloud.clouddms.v1.OperationMetadata",
"createTime": "2020-06-12T15:25:57.430715421Z",
"target": "projects/project-id/locations/region/connectionProfiles/connection-profile-id",
"verb": "create",
"requestedCancellation": false,
"apiVersion": "v1"
},
"done": false
}
gcloud
如需查看有关使用 gcloud 创建 Database Migration Service 连接配置文件的概要信息,请点击此处。
获取连接配置文件的相关信息
REST
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- region:项目区域
- connection-profile-id:连接配置文件 ID
HTTP 方法和网址:
GET https://datamigration.googleapis.com/v1/projects/project-id/locations/region/connectionProfiles/connection-profile-id
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/project-id/locations/region/connectionProfiles/connection-profile-id",
"createTime": "2019-12-22T16:17:37.159786963Z",
"updateTime": "2019-12-24T13:13:39.455857411Z",
"state": "READY",
"displayName": "connection-profile-display-name",
"postgres": {
"host": "host-ip-address",
"port": 5432,
"username": "username"
}
}
gcloud
如需详细了解如何使用 gcloud 检索连接配置文件的相关信息,请点击此处。
列出连接配置文件
以下代码展示了一个检索所有连接配置文件的相关信息的请求。
REST
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- region:项目区域
orderBy:使用此过滤条件可按字母顺序检索特定区域的所有连接配置文件列表。例如,orderBy=name过滤条件会按名称的字母顺序返回所有连接个人资料。-
pageSize:使用此过滤条件可指定 Database Migration Service 在页面上检索和显示的连接配置文件的数量上限。例如,通过设置pageSize=10,Database Migration Service 最多会针对一个网页返回 10 个连接配置文件。
如果连接配置文件超过 10 个,则会显示在其他页面上。每个页面末尾都会显示 nextPageToken 参数和唯一标识符。使用该标识符检索以下页面的连接配置文件列表。
HTTP 方法和网址:
GET https://datamigration.googleapis.com/v1/projects/project-id/locations/region/connectionProfiles
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{
"connectionProfiles": [
{
"name": "projects/project-id/locations/region/connectionProfiles/name-of-first-connection-profile",
"createTime": "2019-12-22T16:17:37.159786963Z",
"updateTime": "2019-12-24T13:13:39.455857411Z",
"state": "READY",
"displayName": "display-name-of-first-connection-profile",
"postgres": {
"host": "host-ip-address-of-first-connection-profile",
"port": port-number-of-first-connection-profile,
"username": "username-of-first-connection-profile",
"password_set": "true"
}
}
{
"name": "projects/project-id/locations/region/connectionProfiles/name-of-second-connection-profile",
"createTime": "2020-11-21T19:22:25.153824963Z",
"updateTime": "2020-11-11T11:15:14.451046111Z",
"state": "READY",
"displayName": "display-name-of-second-connection-profile",
"postgres": {
"host": "host-ip-address-of-second-connection-profile",
"port": port-number-of-second-connection-profile,
"username": "username-of-second-connection-profile",
"password_set": "true"
}
}
]
}
gcloud
如需详细了解如何使用 gcloud 检索所有连接配置文件的相关信息,请点击此处。
更新连接配置文件
以下示例展示了用于更新现有连接配置文件的用户名和密码字段的请求。通过在请求中使用 updateMask 参数,使只有这些字段需要包含在请求正文中。
REST
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- region:项目区域
- connection-profile-id:连接配置文件 ID
- username:数据库用户名
- password:数据库用户密码
HTTP 方法和网址:
PATCH https://datamigration.googleapis.com/v1/projects/project-id/locations/region/connectionProfiles/connection-profile-id?updateMask=postgres.username,postgres.password
请求 JSON 正文:
{
"postgres" {
"username": "username",
"password": "password"
}
}
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/project-id/locations/region/operations/operation-1591973161667-5a7e422cb0ba4-3004980d-2ae97165",
"metadata": {
"@type": "type.googleapis.com/google.cloud.clouddms.v1.OperationMetadata",
"createTime": "2020-06-12T14:46:01.744267779Z",
"target": "projects/project-id/locations/region/connectionProfiles/connection-profile-id",
"verb": "update",
"requestedCancellation": false,
"apiVersion": "v1"
},
"done": false
}
gcloud
如需详细了解如何使用 gcloud 更新连接配置文件,请点击此处。
删除连接配置文件
REST
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- region:项目区域
- connection-profile-id:连接配置文件 ID
HTTP 方法和网址:
DELETE https://datamigration.googleapis.com/v1/projects/project-id/locations/region/connectionProfiles/connection-profile-id
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/project-id/locations/region/operations/operation-1591973161667-5a7e422cb0ba4-3004980d-2ae97165",
"metadata": {
"@type": "type.googleapis.com/google.cloud.clouddms.v1.OperationMetadata",
"createTime": "2020-06-12T14:46:01.744267779Z",
"target": "projects/project-id/locations/region/connectionProfiles/connection-profile-id",
"verb": "delete",
"requestedCancellation": false,
"apiVersion": "v1"
},
"done": false
}
gcloud
如需详细了解如何使用 gcloud 删除连接配置文件,请点击此处。
删除连接配置文件和关联的 Cloud SQL 实例
以下示例展示了删除目标连接配置文件以及级联删除关联的 Cloud SQL 实例的请求。
REST
在使用任何请求数据之前,请先进行以下替换:
- project-id:项目 ID
- region:项目区域
- connection-profile-id:连接配置文件 ID
HTTP 方法和网址:
DELETE https://datamigration.googleapis.com/v1/projects/project-id/locations/region/connectionProfiles/connection-profile-id?force=true
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/project-id/locations/region/operations/operation-1591973161667-5a7e422cb0ba4-3004980d-2ae97165",
"metadata": {
"@type": "type.googleapis.com/google.cloud.clouddms.v1.OperationMetadata",
"createTime": "2020-06-12T14:46:01.744267779Z",
"target": "projects/project-id/locations/region/connectionProfiles/connection-profile-id",
"verb": "create",
"requestedCancellation": false,
"apiVersion": "v1"
},
"done": false
}
gcloud
如需详细了解如何使用 gcloud 同时删除连接配置文件和关联的 Cloud SQL 实例,请点击此处。