connect ENOENT error when connecting to Google Cloud SQL from App Engine / localhost

huangapple go评论63阅读模式
英文:

connect ENOENT error when connecting to Google Cloud SQL from App Engine / localhost

问题

I have a next.js-strapi v4 app hosted on google cloud app engine.
The database is postgreSql and hosted on google cloud sql.
The files are also hosted on gcp.
Since the deployment to Google App Engine is missing from the strapi v4 documentation, I used that guide on medium.

when I run the app using:

gcloud app deploy app.yaml

I get this error:

debug: ⛔️ Server wasn't able to start properly.
error: connect ENOENT /cloudsql/INSTANCE_CONNECTION_NAME/.s.PGSQL.5432
at PipeConnectWrap.afterConnect

this is the app.yaml:

runtime: nodejs16

instance_class: F2

env_variables:
NODE_ENV: 'production'

beta_settings:
cloud_sql_instances: <INSTANCE_CONNECTION_NAME>

this is database.js:

module.exports = ({ env }) => ({
connection: {
client: "postgres",
connection: {
host: /cloudsql/${env('INSTANCE_CONNECTION_NAME')},
port: env.int("DATABASE_PORT", 5432),
database: env("DATABASE_NAME", "strapi"),
user: env("DATABASE_USERNAME", ""),
password: env("DATABASE_PASSWORD", ""),
},
},
});

This is what I have already did:

  1. enable the Cloud SQL Admin API
  2. add Cloud SQL Client role to my App Engine default service account
  3. expose the virtual machine ip and my local ip to the Authorized networks of the database
  4. run "gcloud auth application-default login" on the google cloud sdk
  5. made a successful connectivity test with my ip using the CONNECTIVITY TESTS tab

I also tried to connect to the gcp database from my local app but got the same error.

thanks for any help!

英文:

I have a next.js-strapi v4 app hosted on google cloud app engine.
The database is postgreSql and hosted on google cloud sql.
The files are also hosted on gcp.
Since the deployment to Google App Engine is missing from the strapi v4 documentation, I used that guide on medium.

when I run the app using:

gcloud app deploy app.yaml

I get this error:

debug: ⛔️ Server wasn&#39;t able to start properly.
error: connect ENOENT /cloudsql/INSTANCE_CONNECTION_NAME/.s.PGSQL.5432
 at PipeConnectWrap.afterConnect

this is the app.yaml:

runtime: nodejs16

instance_class: F2

env_variables:
  NODE_ENV: &#39;production&#39;

beta_settings:
  cloud_sql_instances: &lt;INSTANCE_CONNECTION_NAME&gt;

this is database.js:

module.exports = ({ env }) =&gt; ({
  connection: {
    client: &quot;postgres&quot;,
    connection: {
      host: `/cloudsql/${env(&#39;INSTANCE_CONNECTION_NAME&#39;)}`,
      port: env.int(&quot;DATABASE_PORT&quot;, 5432),
      database: env(&quot;DATABASE_NAME&quot;, &quot;strapi&quot;),
      user: env(&quot;DATABASE_USERNAME&quot;, &quot;&quot;),
      password: env(&quot;DATABASE_PASSWORD&quot;, &quot;&quot;),
    },
  },
});

This is what I have already did:

  1. enable the Cloud SQL Admin API
  2. add Cloud SQL Client role to my App Engine default service account
  3. expose the virtual machine ip and my local ip to the Authorized networks of the database
  4. run "gcloud auth application-default login" on the google cloud sdk
  5. made a successful connectivity test with my ip using the CONNECTIVITY TESTS tab

I also tried to connect to the gcp database from my local app but got the same error.

thanks for any help!

答案1

得分: 1

solution 1

你必须通过云SQL身份验证代理授予访问权限。
配置云SQL与App Engine标准环境的文档

solution 2

  1. 首先,您必须在GCP上创建虚拟专用云(VPC)。
  2. 然后将您的云SQL实例IP添加到VPC网络对等连接。
  3. 接着,在您的VPC中创建一个具有28 CIDR范围子网的无服务器VPC访问连接器。
  4. 最后,在app.yaml中提及您的适配器名称。

配置VPC对等连接
配置无服务器VPC访问连接器

如果您正在使用App Engine弹性环境进行托管,无需创建无服务器VPC访问连接器。

英文:

solution 1

you have to grants access through cloud sql auth proxy
documentation for config cloud sql with app engine standard environment

solution 2

  1. first you have to create virtual private cloud (VPC) from gcp.
  2. then add you cloud sql instance ip to VPC network peering
  3. then create Serverless VPC access connector with 28 CIDR range
    subnet in your VPC
  4. finally mention your adapter name in app.yaml

configure vpc peering

configure serverless vpc access connector

if you are host using app engine flexible environment no need to creatre serverless VPC access connector

huangapple
  • 本文由 发表于 2023年3月20日 23:16:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定