本頁面說明如何避免誤刪 Cloud SQL 執行個體。
總覽
執行個體防刪除功能可防止意外移除現有和新執行個體。您可以使用執行個體刪除防護功能,保護對應用程式和服務而言至關重要的執行個體。
您可以在建立執行個體時設定防刪除選項。此外,您也可以在現有執行個體上設定這個選項。無論上述哪種情況,系統都會在執行個體層級禁止刪除作業。根據預設,除非您使用 Google Cloud 控制台或 Terraform 建立執行個體,否則刪除保護選項會停用。
複製執行個體時,新執行個體會從來源執行個體接收防刪除功能選項 (已啟用或已停用)。
規劃防刪除功能
如果您打算建立執行個體,請決定是否要保護新執行個體,避免意外刪除。舉例來說,您應該以這種方式保護重要執行個體。
同時考量是否有現有執行個體需要避免意外刪除。如果您有多個要保護的執行個體,請為每個執行個體設定選項。
考慮為唯讀備用資源啟用防刪除功能
建立唯讀備用資源時,該備用資源不會從主要執行個體取得刪除保護設定,這項設定會保持獨立。不過,您可以為新的或現有的唯讀備援機制啟用刪除保護功能。
對於 Cloud SQL 唯讀備用資源,刪除保護選項的運作方式與 Cloud SQL 執行個體相同。
確認必要權限或角色
如要設定刪除保護選項,Cloud SQL 執行個體的服務帳戶必須具備特定權限或 IAM 角色。
建立執行個體的權限或角色
如同相關作業,防刪除選項需要 cloudsql.instances.create
權限或 cloudsql.admin
角色。因此,建立執行個體時,不需要額外權限就能啟用刪除保護功能。
編輯執行個體的權限或角色
如同相關作業一樣,刪除保護機制變更需要 cloudsql.instances.update
權限或 cloudsql.editor
角色。因此,當您編輯防刪除的執行個體時,不需要額外權限。
規劃執行個體刪除作業
如要刪除執行個體,請先執行下列操作:
- 確認刪除執行個體是否安全
- 確認防刪除功能已停用;如有需要,請編輯執行個體以停用防刪除功能
防刪除功能的限制
執行個體的防刪除功能無法防止下列情況:
- 停止執行個體
- 重新啟動執行個體
- 編輯執行個體
- 刪除備份
- 因帳單問題而暫停執行個體
- 因帳單問題而刪除已停權的執行個體
- 因專案刪除而刪除執行個體
為新的執行個體設定防刪除功能
以下是設定防刪除選項的方式。根據預設,系統會停用防刪除功能,除非您使用 Google Cloud 控制台或 Terraform 建立執行個體。
如果已啟用防刪除功能,嘗試刪除執行個體的動作就會失敗。
控制台
gcloud
如要在建立執行個體時設定防刪除功能,請使用類似下列的指令:
gcloud sql instances create [INSTANCE_NAME] \ --deletion-protection
REST v1
如要在建立執行個體時設定防刪除功能,請在 deletionProtection
欄位中指定 true
。
使用任何要求資料之前,請先替換以下項目:
- project-id:您的專案 ID
- instance-id:所需的執行個體 ID
- database-version:資料庫版本
- region:所需區域
- machine-type:所需機器類型
HTTP 方法和網址:
POST https://sqladmin.googleapis.com/v1/projects/project-id/instances
JSON 要求主體:
{ "name": "instance-id", "region": "region", "databaseVersion": "database-version", "settings": { "tier": "machine-type", "deletionProtectionEnabled": true } }
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id", "status": "PENDING", "user": "[email protected]", "insertTime": "2022-05-24T15:34:10.929Z", "operationType": "CREATE", "name": "operation-id", "targetId": "instance-id", "selfLink": "https://sqladmin.googleapis.com/v1/projects/project-id/operations/operation-id", "targetProject": "project-id" }
REST v1beta4
如要在建立執行個體時設定防刪除功能,請在 deletionProtection
欄位中指定 true
。
使用任何要求資料之前,請先替換以下項目:
- project-id:您的專案 ID
- instance-id:所需的執行個體 ID
- database-version:資料庫版本
- region:所需區域
- machine-type:所需機器類型
HTTP 方法和網址:
POST https://sqladmin.googleapis.com/v1beta4/projects/project-id/instances
JSON 要求主體:
{ "name": "instance-id", "region": "region", "databaseVersion": "database-version", "settings": { "tier": "machine-type", "deletionProtectionEnabled": true } }
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id", "status": "PENDING", "user": "[email protected]", "insertTime": "2022-05-26T15:37:10.929Z", "operationType": "CREATE", "name": "operation-id", "targetId": "instance-id", "selfLink": "https://sqladmin.googleapis.com/v1/projects/project-id/operations/operation-id", "targetProject": "project-id" }
為現有執行個體設定或移除防刪除功能
以下是編輯防刪除功能的實例方法。如果啟用這個選項,後續嘗試刪除執行個體的動作就會失敗。
控制台
gcloud
如要在更新執行個體時啟用防刪除功能,請使用類似下列的指令:
gcloud sql instances patch [INSTANCE_NAME] \ --deletion-protection
如要在更新執行個體時停用防刪除功能,請使用類似下列的指令:
gcloud sql instances patch [INSTANCE_NAME] \ --no-deletion-protection
REST v1
如要在更新執行個體時設定防刪除功能,請在 deletionProtection
欄位中指定 true
。如要移除防刪除功能,請在該欄位中指定 false
。
使用任何要求資料之前,請先替換以下項目:
- project-id:您的專案 ID
- instance-id:所需的執行個體 ID
HTTP 方法和網址:
PATCH https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id
JSON 要求主體:
{ "settings": { "deletionProtectionEnabled": true } }
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id", "status": "PENDING", "user": "[email protected]", "insertTime": "2022-06-1T03:42:12.281Z", "operationType": "CREATE", "name": "operation-id", "targetId": "instance-id", "selfLink": "https://sqladmin.googleapis.com/v1/projects/project-id/operations/operation-id", "targetProject": "project-id" }
REST v1beta4
如要在更新執行個體時設定防刪除功能,請在 deletionProtection
欄位中指定 true
。如要移除防刪除功能,請在該欄位中指定 false
。
使用任何要求資料之前,請先替換以下項目:
- project-id:您的專案 ID
- instance-id:所需的執行個體 ID
HTTP 方法和網址:
PATCH https://sqladmin.googleapis.com/v1beta4/projects/project-id/instances/instance-id
JSON 要求主體:
{ "settings": { "deletionProtectionEnabled": true } }
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id", "status": "PENDING", "user": "[email protected]", "insertTime": "2022-06-1T03:32:12.281Z", "operationType": "UPDATE", "name": "operation-id", "targetId": "instance-id", "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/operations/operation-id", "targetProject": "project-id" }