Google App Engine Rails Postgres 无法连接到 Cloud SQL 数据库。没有这样的文件或目录

问题描述 投票:0回答:1

我有一个Rails应用,应该通过Google Cloud App Engine进行托管。它有一个Postgres数据库设置。我遵循了这个教程。https:/cloud.google.comrubyrailsusing-cloudsql-postgres。

我在app.yaml.和database.yml中添加了所有的配置。

entrypoint: bundle exec rackup --port $PORT
env: flex
runtime: ruby

env_variables:
  SECRET_KEY_BASE: [SECRET_KEY]

beta_settings:
  cloud_sql_instances: [INSTANCE]

和数据库.yml中添加了所有配置

production:
  adapter: postgresql
  encoding: unicode
  pool: 5
  timeout: 5000
  username: postgres
  password: [DB_PASSWORD]
  database: [DB_DATABASE]
  host: /cloudsql/[INSTANCE]

Cloud SQL实例设置正确,因为我可以使用Cloud SQL代理连接到它。https:/cloud.google.comsqldocspostgresconnect-admin-proxy。

在部署到App Engine并尝试迁移后,它给了我如下的错误。

PG::ConnectionBad: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/cloudsql/[INSTANCE]/.s.PGSQL.5432"?

这种情况也发生在本地运行的代理服务器上,并尝试

RAILS_ENV=production rake db:create rake db:migrate

当我SSH进入App Engine的虚拟机时,我看到cloudsql文件夹不见了。我仔细检查了一下,访问权限应该是有的,而且Cloud SQL API和Cloud SQL Admin API是启用的。我还注意到,当SSH进入App Engine的虚拟机(显然有两个,我试了两个)并运行ls时,目录完全是空的。部署成功了,因为我可以访问应用程序,但不能访问需要DB连接的控制器。

ruby-on-rails postgresql google-app-engine google-cloud-sql
1个回答
0
投票

我按照这个老的但更好的解释 GCP教程(本教程旨在使用云端shell),我意识到必须在本地环境中运行云端SQL代理(在其他终端或后台)

云端sql代理将创建unix套接字文件。

Listening on /cloudsql/tetsingfakeproject:us-central1:testdatabase ce/.s.PGSQL.5432 for tetsingfakeproject:us-central1:testdatabase

这样才能进行迁移,你可以在本地环境上通过这一步。

关于使用这个的注意事项 教程

我遵循了旧教程的所有步骤,并采用了一些替代步骤

在步骤3中,我运行了最新的命令来启用SQL admin api。

gcloud service enable sqladmin.googleapis.com

在第10步,我用这些命令安装了pg和appengine包,而不是修改文件 "Gemfile"

bundle add pg
bundle add appengine

如果这个ruby应用的部署时间超过10分钟,请运行这个命令。

gcloud config set app/cloud_build_timeout 3600s

并运行 gcloud app deploy 再来一次。

按照这个教程,我可以使用ruby+postgresql得到一个功能性的App Engine服务,我在使用其他教程时遇到了很多问题。

© www.soinside.com 2019 - 2024. All rights reserved.