我正在尝试将谷歌云sql实例与用python编写的云函数连接起来。
global pg_pool, pg_pool_db_name
pg_config = {
'user': CONNECTION_DATA[dbname]['DB_USER'],
'password': CONNECTION_DATA[dbname]['DB_PASSWORD'],
'host': host,
'dbname': dbname
}
pg_pool = ThreadedConnectionPool(1, 1, **pg_config)
此功能测试调用失败,并输出错误消息:
Error: function terminated. Recommended action: inspect logs for termination reason. Details:could not connect to server: Connection refused
Is the server running locally and accepting
connections on Unix domain socket "/cloudsql/my_instance_name/.s.PGSQL.5432"?
任何经历过这种情况的人。
我写了一篇关于从谷歌云函数连接到云SQL 的教程
9.Create a service account for your cloud function. Ensure that the service account for your service has the following IAM roles: Cloud SQL Client, and for connecting from Cloud Functions to Cloud Sql on internal ip we need also the role Compute Network User.
gcloud iam service-accounts create cloud-function-to-sql
gcloud projects add-iam-policy-binding gcf-to-sql --member serviceAccount:cloud-function-to-sql@gcf-to-sql.iam.gserviceaccount.com --role roles/cloudsql.client
gcloud projects add-iam-policy-binding gcf-to-sql --member serviceAccount:cloud-function-to-sql@gcf-to-sql.iam.gserviceaccount.com --role roles/compute.networkUser
然后使用您刚刚创建的服务帐户部署该功能:
2.Deploy the cloud function:
gcloud beta functions deploy gcf_to_sql --runtime python37 --region europe-west2 --service-account cloud-function-to-sql --trigger-http
编辑
请阅读以下文档:
1.创建服务帐户
2.授予服务帐户角色
3.从云控制台部署云功能
单击"更多"可显示高级选项,例如设置区域,指定超时或添加环境变量
在高级选项中,选择您刚刚创建的服务帐户。