在本地使用 cloud_sql_proxy 获取 notAuthorized 错误

Getting notAuthorized error with cloud_sql_proxy locally

我正在尝试为第二代云 sql 实例在本地设置连接。

调用代理是

./cloud_sql_proxy -dir=/cloudsql -instances=status-1268:us-central1:status-dev=tcp:3306 & mysql -u status_stg --host 127.0.0.1

我不太清楚如何正确设置 API 部分的服务帐户,所以我尝试了两种默认设置,App 引擎默认服务帐户和计算引擎默认服务帐户。我还创建了一个新的服务帐户。对于他们中的每一个,我都从管理权限部分将服务帐户 ID 分配为编辑者和所有者。也许还有另一种方法可以为这些帐户启用 Cloud SQL Admin API?

当我的服务试图与 DB 对话时代理的输出:

./cloud_sql_proxy -dir=/cloudsql -instances=status-1268:us-central1:status-   dev=tcp:3306 & mysql -u status_stg --host 127.0.0.1
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (61)
2016/04/04 11:26:58 Open socket for "status-1268:us-central1:status-dev" at     "127.0.0.1:3306"
2016/04/04 11:26:58 Socket prefix: /cloudsql
2016/04/04 11:27:10 Got a connection for "status-1268:us-central1:status-dev"
2016/04/04 11:27:11 couldn't connect to "status-1268:us-central1:status-dev": googleapi: Error 403: The client is not authorized to make this request., notAuthorized
2016/04/04 11:29:08 Got a connection for "status-1268:us-central1:status-dev"
2016/04/04 11:29:08 couldn't connect to "status-1268:us-central1:status-dev": googleapi: Error 403: The client is not authorized to make this request., notAuthorized

关于寻找什么的想法?我想我在这里配置服务帐户时遗漏了一些基本的东西。

(总的来说,对于我的项目,我是 运行 一个 node.js 快速应用程序,并希望将它连接到第二代云 sql 数据库实例,而不必使用 0.0.0.0.0 公开它。 0.0 因为我目前正在工作)

谢谢

请仔细检查连接字符串。听起来您的凭据是有序的,所以您的实例名称很可能有错误。

我建议从实例的 Cloud Console 页面复制并粘贴 "Instance connection name":

在我的例子中,问题是服务帐户没有正确的角色。您至少应该将其设置为编辑器。

出现这种错误的原因有很多。

解决方案如下:

1) 从 sql 实例 'overview' 选项卡复制实例名称。

2) 您可能无法访问 cloud sql client.

here更详细。

TL;DR:如果您的错误日志包含 Error during createEphemeral..,请继续阅读。

对我有用的解决方案来自 Github Cloud-SQL-Proxy Issue Tracker

It seems that you may have encountered a bug in backend that is triggered when there is an old service account with the same name. Follow the steps below to solve your issue:

  1. Go to the Cloud Project IAM page (Left Menu > IAM & Admin > IAM)
  2. Look for the row with the robot account that is having the issue.
  3. It should say "Cloud SQL Client" on the role column.
  4. Click on it to open the dropdown
  5. Click on the 'Cloud SQL Client' role that is checked to uncheck it.
  6. Click the blue save button above.
  7. Verify that the service account is removed from the IAM page.
  8. Click the Add button above in the same page.
  9. Enter the service account email address and select the Cloud SQL Client role
  10. Click Add
  11. The service account should appear again in the the list.

感谢 Chees,但分数是我的 :P。希望它能解决您的问题。

就我而言,我犯了两个错误:

  1. 确保为云 sql 实例使用正确的项目。

    $gcloud config set project my-project

  2. 使用正确connectionName。如果您像我一样关注 google 文档,您最终可能会在 connectionName 中放置方括号。删除它们。

Myname-MBP:django setoelkahfi$ ./cloud_sql_proxy -instances="[blitzkrig-9f158:us-central1:polls-instance]"=tcp:3306 2018/06/01 04:55:48 Listening on 127.0.0.1:3306 for [blitzkrig-9f158-9f158:us-central1:polls-instance] 2018/06/01 04:55:48 Ready for new connections 2018/06/01 04:56:48 New connection for "[blitzkrig-9f158-9f158:us-central1:polls-instance]" 2018/06/01 04:56:50 couldn't connect to "[blitzkrig-9f158-9f158:us-central1:olls-instance]": ensure that the account has access to "[blitzkrig-9f158-9f158:us-central1:polls-instance]" (and make sure there's no typo in that name). Error during createEphemeral for [blitzkrig-9f158-9f158:us-central1:polls-instance]: googleapi: Error 403: The client is not authorized to make this request., notAuthorized

另一种说法是,如果您使用 -dir 标志,则意味着您将通过 unix-socket 进行连接,MySQL 是一个额外的连接参数,而 =tcp:3306 变得多余,当通过 127.0.0.1 连接时,这将不起作用,因为它是 TCP 并且它正在等待套接字上的连接。