【问题标题】:GCloud - Connecting App Engine Flex to Cloud SQL in other projectGCloud - 将 App Engine Flex 连接到其他项目中的 Cloud SQL
【发布时间】:2021-01-28 12:32:53
【问题描述】:

我正在尝试将我的应用引擎 flex 服务连接到共享项目中的云 sql 数据库。

我已将应用引擎默认用户和应用引擎 flex 用户作为 SQL 管理员添加到共享项目中。

现在我正在尝试使用以下连接字符串将应用引擎容器连接到云 sql 实例:

"Server=cloudsql;Database=cliche;User Id=user;Password=password;"

但是,连接总是以给定的连接字符串失败。

[13:57:37 ERR] An error occurred using the connection to database 'cliche' on server 'cloudsql'.
[13:57:37 ERR] An exception occurred while iterating over the results of a query for context type 'IKL.Data.ApplicationDbContext'.
 Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)

根据要求,我在此处提供 app.yaml:

runtime: aspnetcore
env: flex
service: platform
manual_scaling:
  instances: 1
resources:
  cpu: 2
  memory_gb: 1.5
  disk_size_gb: 10
beta_settings:
  cloud_sql_instances: shared-resources-291914:europe-west3:edutec-mssql=tcp:1433  

【问题讨论】:

  • 您如何使用 App Engine Flex 配置 Cloud SQL 连接?在 app.yaml 中?你能分享你的 app.yaml 吗?
  • 我正在使用 app yaml 进行配置,对于缺乏细节深表歉意。
  • 数据库有公网IP吗?还是只部署在私有 IP 上?
  • 部署在公网ip
  • 你能分享一下你配置和连接数据库的代码吗?

标签: asp.net google-app-engine google-cloud-platform google-cloud-sql


【解决方案1】:

我发现了你的问题!您的连接字符串"Server=cloudsql;Database=cliche;User Id=user;Password=password;" 不正确。

Cloud SQL 代理打开一个隧道。隧道的一侧是 Cloud SQL 实例,另一侧是您的本地实例。因此,您必须在本地主机上连接

"Server=127.0.0.1;Database=cliche;User Id=user;Password=password;"

【讨论】:

    【解决方案2】:

    如果您在 App Engine Flex 项目和 Cloud SQL 项目之间使用 VPC 对等互连,那么我必须通知您,目前还不能这样做,当您在 Cloud SQL 上使用内部 IP 时,它将自动使用一个 VPC 对等互连(与您可能使用的不同),并且 3 个 VPC 对等互连之间的流量是不可能的。

    您可以在 Cloud SQL 项目中使用虚拟机作为 NAT 服务器将流量路由到那里的 Cloud SQL 以解决此问题,或者您可以在 App Engine 和 Cloud SQL 项目之间使用 Cloud VPN,如果请确保 NAT 虚拟机与您的 Cloud SQL 服务器位于同一区域,或者您的 SQL 项目上的 VPN 与您的 SQL 服务器位于同一区域。

    另外,请记住,您需要对所有涉及的 GCP 项目具有适当的 GCP 防火墙规则。

    【讨论】:

      猜你喜欢
      • 2019-03-16
      • 2021-08-04
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      • 2021-02-22
      相关资源
      最近更新 更多