如何仅在需要时运行 Google Cloud SQL?

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

Google Cloud SQL advertises that it's only $0.0150 per hour for the smallest machine type, and I'm being charged for every hour, not just hours that I'm connected. Is this because I'm using a pool? How do I setup my backend so that it queries the cloud db only when needed so I don't get charged for every hour of the day?

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);

    });
  });
}

推荐答案

这与其说是关于池,不如说是关于 Cloud SQL 的性质.与 App Engine 不同,Cloud SQL 实例始终启动.在我离开项目一周后的一个星期六早上,我艰难地了解到了这一点.:)

This is not so much about the pool as it is about the nature of Cloud SQL. Unlike App Engine, Cloud SQL instances are always up. I learned this the hard way one Saturday morning when I'd been away from the project for a week. :)

除非您明确停止服务,否则无法在不使用它们时停止它们.

There's no way to spin them down when they're not being used, unless you explicitly go stop the service.

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

There's no way to schedule a service stop, at least within the GCP SDK. You could alway write a cron job, or something like that, that runs a little gcloud sql instances patch [INSTANCE_NAME] --activation-policy NEVER command at, for example, 6pm local time, M-F. I was too lazy to do that, so I just set a calendar reminder for myself to shut down my instance at the end of my workday.

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

Here's the MySQL Instance start/stop/restart page for the current SDK's docs: https://cloud.google.com/sql/docs/mysql/start-stop-restart-instance

另外请注意,GCP 平台中有一个正在进行的功能请求"以启动/也根据流量停止 Cloud SQL(第 2 代).您还可以访问链接并在那里提供您宝贵的建议/意见.

On an additional note, there is an ongoing 'Feature Request' in the GCP Platform to start/stop the Cloud SQL (2nd Gen), according to the traffic as well. You can also visit the link and provide your valuable suggestions/comments there as well.

相关文章