我想使用gcloud cli创建可在默认网络上访问的sql实例。所以我尝试了这个:
gcloud beta sql instances create instance1 \
--network projects/peak-freedom-xxxxx/global/networks/default
我得到错误
ERROR: (gcloud.beta.sql.instances.create) [INTERNAL_ERROR] Failed to create subnetwork.
Please create Service Networking connection with service 'servicenetworking.googleapis.com'
from consumer project '56xxxxxxxxx' network 'default' again.
当您进入控制台创建它时,您可以检查私有IP,您可以看到以下内容:
并且有一个“分配并连接”按钮。所以我想这就是我需要做的。但是我不知道如何使用gcloud cli来做到这一点。
任何人都可以帮忙吗?
编辑1:
我尝试将--network
设置为https://www.googleapis.com/compute/alpha/projects/testing-project-xxx/global/networks/default
导致的原因
ERROR: (gcloud.beta.sql.instances.create) [INTERNAL_ERROR] Failed to
create subnetwork. Set Service Networking service account as
servicenetworking.serviceAgent role on consumer project
然后,我尝试重新创建一个全新的项目并启用Service Networking API,如下所示:
gcloud --project testing-project-xxx \
services enable \
servicenetworking.googleapis.com
然后创建数据库导致相同的错误。因此,我尝试手动添加servicenetworking.serviceAgent
角色并运行:
gcloud projects add-iam-policy-binding testing-project-xxx \
--member=serviceAccount:[email protected] \
--role=roles/servicenetworking.serviceAgent
此成功
Updated IAM policy for project [testing-project-xxx].
bindings:
- members:
- user:[email protected]
role: roles/owner
- members:
- serviceAccount:[email protected]
role: roles/servicenetworking.serviceAgent
etag: XxXxXX37XX0=
version: 1
但是创建数据库失败,并出现相同的错误。作为参考,这是我用来创建数据库的命令行:
gcloud --project testing-project-xxx \
beta sql instances create instanceName \
--network=https://www.googleapis.com/compute/alpha/projects/testing-project-xxx/global/networks/default \
--database-version POSTGRES_11 \
--zone europe-north1-a \
--tier db-g1-small
格式为“ projects / peak-freedom-xxxxx / global / networks / default”的网络名称用于在共享VPC网络下创建SQL实例。如果要在普通的VPC网络中创建实例,则应使用:
gcloud --project=[PROJECT_ID] beta sql instances create [INSTANCE_ID]
--network=[VPC_NETWORK_NAME]
--no-assign-ip
其中[VPC_NETWORK_NAME]
的格式为https://www.googleapis.com/compute/alpha/projects/[PROJECT_ID]/global/networks/[VPC_NETWORK_NAME]
有关更多信息,请检查here。