在 Google Cloud SQL nodejs 上定义 Sequelize



嗨,我从GAE连接到Google Cloud SQL时遇到问题。

我的应用程序在 docker 容器内运行,这里是 docker 文件

FROM node:8.10.0-alpine
ARG NODE_ENV=production
ENV NODE_ENV=$NODE_ENV
# env like sql user db instance connection name
# Set a working directory
WORKDIR /usr/src/app
COPY ./build/package.json .
COPY ./build/yarn.lock .
# Install Node.js dependencies
RUN yarn install --production --no-progress
# Copy application files
COPY ./build .
COPY ./src/db/seeders ./seeds
COPY ./src/db/migrations ./migrations
COPY ./scripts ./scripts
RUN yarn run db:seed # -> failed to run this line
RUN yarn run db:migrate
# Run the container under "node" user by default
USER node
CMD [ "node", "server.js" ]

连接到我正在使用的数据库 续集这是我的连接配置

module.exports = {
production: {
dialect: 'postgres',
seederStorage: 'sequelize',
seederStorageTableName: 'sequelize_seeder',
username: process.env.SQL_USER,
password: process.env.SQL_PASSWORD,
database: process.env.SQL_DATABASE,
host: `/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}`,
logging: true,
dialectOptions: {
socketPath: `/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}`,
supportBigNumbers: true,
bigNumberStrings: true,
ssl: false,
},
pool: {
max: 5,
idle: 30000,
acquire: 60000,
},
operatorsAliases: false,
define: {
freezeTableName: true,
},
},
};

我尝试了从设置主机到本地主机/127.0.0.1的几乎所有内容 这样做的时候,我得到了SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:5432

如果我正在设置主机:host:/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}''

我收到不同的错误SequelizeConnectionError: connect ENOENT {MY_INSTANCE_CONNECTION_NAME}.s.PGSQL.5432

我的应用程序.yaml 文件

env: flex
runtime: custom
env_variables:
#env db user etc..
beta_settings:
cloud_sql_instances: MY_INSTANCE_CONNECTION_NAME

我尝试使用 knex 登录并设法连接,所以我假设我的配置有问题

今天花了一整天的时间尝试在通过Bitbucket管道部署时从Google App Engine应用程序连接到Google Cloud SQL(PostreSQL(。

以下是对我有用的配置(也许它们可以挽救某人几个小时的生命(:

const sequelize = new Sequelize(DB_NAME, USERNAME, PASSWORD, {
dialect: 'postgres',
// e.g. host: '/cloudsql/my-awesome-project:us-central1:my-cloud-sql-instance'
host: '/cloudsql/${INSTANCE_CONNECTION_NAME}',
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
dialectOptions: {
// e.g. socketPath: '/cloudsql/my-awesome-project:us-central1:my-cloud-sql-instance'
// same as host string above
socketPath: '/cloudsql/${INSTANCE_CONNECTION_NAME}'
},
logging: false,
operatorsAliases: false
});

app.yaml文件:

runtime: nodejs
env: flex
# make sure to include code below
beta_settings:
cloud_sql_instances: my-awesome-project:us-central1:my-cloud-sql-instance

就我而言,当我没有提供host它在连接到 Google Cloud SQL 时以及所有其他带有连接字符串的变体都失败了。

干杯!

看起来传递给续集的参数有问题。 尝试使用简单的连接字符串 var conString = "postgres://UserName:Password@Host:5432/YourDatabase";

相关内容

  • 没有找到相关文章

最新更新