【问题标题】:How to run Google Cloud SQL only when I need it?如何仅在需要时运行 Google Cloud SQL?
【发布时间】:2018-09-07 21:03:09
【问题描述】:

Google Cloud SQL 宣称,对于最小的机器类型,每小时只需 0.0150 美元,而且我按小时收费,而不仅仅是我连接的小时数。这是因为我使用的是游泳池吗?如何设置我的后端,以便它仅在需要时查询云数据库,这样我就不会在一天中的每个小时都被收费?

const mysql      = require('mysql');
const pool = mysql.createPool({
    host : process.env.SQL_IP,
    user     : 'root',
    password : process.env.SQL_PASS,
    database : 'mydb',
    ssl      : {
          [redacted]
    }
});

function query(queryStatement, cB){
  pool.getConnection(function(err, connection) {
    // Use the connection
    connection.query(queryStatement, function (error, results, fields) {
      // And done with the connection.
      connection.destroy();
      // Callback
      cB(error,results,fields);

    });
  });
}

【问题讨论】:

    标签: javascript mysql node.js google-cloud-platform google-cloud-sql


    【解决方案1】:

    这与其说是关于池,不如说是关于 Cloud SQL 的性质。与 App Engine 不同,Cloud SQL 实例始终处于运行状态。一个星期六早上,当我离开这个项目一周时,我很难学会这一点。 :)

    没有办法在不使用它们时将它们关闭,除非您明确停止服务。

    没有办法安排服务停止,至少在 GCP SDK 中是这样。您总是可以编写一个 cron 作业,或类似的东西,在例如当地时间下午 6 点(M-F)运行一个小的gcloud sql instances patch [INSTANCE_NAME] --activation-policy NEVER 命令。我太懒了,所以我只是为自己设置了一个日历提醒,让我在工作日结束时关闭我的实例。

    这是当前 SDK 文档的 MySQL 实例启动/停止/重启页面: https://cloud.google.com/sql/docs/mysql/start-stop-restart-instance

    另外,根据流量,GCP 平台中有一个持续的“Feature Request”来启动/停止 Cloud SQL(第 2 代)。您也可以访问link,并在那里提供您宝贵的建议/cmets。

    【讨论】:

    • 对不起,你会在哪里写这样一个可以访问 gcloud 的 cron 作业?
    • @AlexanderShubert 您可以在 GCP 上的 linux VM 上运行 cron。可以使用 GCP 中的云功能、发布订阅和调度程序轻松安排虚拟机的启动和停止
    【解决方案2】:

    我从@ingernet 那里得到了这个想法,并创建了一个云函数,它可以在需要时启动/停止 CloudSQL 实例。它可以通过计划的作业触发,因此您可以定义实例何时上升或下降。

    详细信息在此Github Gist 中(灵感来自here)。 免责声明:我不是 python 开发人员,所以代码中可能存在问题,但最终它可以工作。


    基本上你需要遵循以下步骤:

    1. 创建将用于触发云功能的发布/订阅主题。
    2. Create the cloud function 并复制下面的代码。
      1. 确保在第 8 行设置正确的项目 ID。
      2. 将触发器设置为 Pub/Sub 并选择在第 1 步中创建的主题。
    3. Create a cloud scheduler job定时触发云功能。
      1. 选择您希望触发云功能的频率。
      2. 将目标设置为 Pub/Sub 并定义在步骤 1 中创建的主题。
      3. 负载应设置为start [CloudSQL instance name]stop [CloudSQL instance name] 以启动或停止指定的实例(例如start my_cloudsql_instance 将启动名为my_cloudsql_instance 的CloudSQL 实例)

    Main.py:

    from googleapiclient import discovery
    from oauth2client.client import GoogleCredentials
    import base64
    from pprint import pprint
    
    credentials = GoogleCredentials.get_application_default()
    service = discovery.build('sqladmin', 'v1beta4', credentials=credentials, cache_discovery=False)
    project = 'INSERT PROJECT_ID HERE'
    
    def start_stop(event, context):
      print(event)
      pubsub_message = base64.b64decode(event['data']).decode('utf-8')
      print(pubsub_message)
      command, instance_name = pubsub_message.split(' ', 1)
    
      if command == 'start':
        start(instance_name)
      elif command == 'stop':
        stop(instance_name)
      else:
        print("unknown command " + command)
    
    def start(instance_name):
      print("starting " + instance_name)
      patch(instance_name, "ALWAYS")
    
    def stop(instance_name):
      print("stopping " + instance_name)
      patch(instance_name, "NEVER")
    
    def patch(instance, activation_policy):
      request = service.instances().get(project=project, instance=instance)
      response = request.execute()
    
      dbinstancebody = {
        "settings": {
          "settingsVersion": response["settings"]["settingsVersion"],
          "activationPolicy": activation_policy
        }
      }
    
      request = service.instances().patch(
        project=project,
        instance=instance,
        body=dbinstancebody)
      response = request.execute()
      pprint(response)
    

    Requirements.txt

    google-api-python-client==1.10.0
    google-auth-httplib2==0.0.4
    google-auth==1.19.2
    oauth2client==4.1.3
    

    【讨论】:

    • 干得好!我有兴趣在我的沙盒中尝试一下。
    猜你喜欢
    • 2017-11-21
    • 1970-01-01
    • 2018-04-26
    • 2016-05-29
    • 2020-04-08
    • 2020-10-25
    • 2019-09-11
    • 1970-01-01
    • 2019-02-20
    相关资源
    最近更新 更多