我试过
gcloud sql instances patch YOUR_INSTANCE_NAME --authorized-networks my-ip
但是此命令会从访问控制列表中删除所有IP地址,并仅添加我的IP地址。如何添加我的IP地址以保留以前的IP地址?
答案 0 :(得分:1)
全流程(使用 Nicholas 回答):
# get zone from global meta data
zone_full=`curl http://metadata/computeMetadata/v1/instance/zone -H "Metadata-Flavor: Google"`
zone=${zone_full##*/}
# get sql-instance from metadata
sql_instance=`curl http://metadata/computeMetadata/v1/instance/attributes/sql-instance -H "Metadata-Flavor: Google"`
# get current instance external natIP
instance_nat_ip=`gcloud --format="value(networkInterfaces[0].accessConfigs[0].natIP)" compute instances describe $HOSTNAME --zone=$zone`
# get already authorized ips
authorized_sql_ips=`gcloud sql instances describe $sql_instance | grep -E "\-\s[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" | cut -d '-' -f 2 | cut -d ' ' -f 2 | tr '\n' ',' | head -c -1`
# add instance's natIP to authorized SQL networks
gcloud sql instances patch $sql_instance --authorized-networks $authorized_sql_ips,$instance_nat_ip
答案 1 :(得分:0)
You can add ip addresses via the console web interface
Go to Storage -> Cloud SQL-> Choose your instance -> Access Control -> Click "Add Item" where it shows Allowed Networks