node.js - 将 Ghost 与 Google Cloud SQL 结合使用时出现问题

标签 node.js google-cloud-platform google-cloud-sql knex.js ghost

我正在按照说明操作 here使用 Ghost 作为 NPM 模块,并尝试设置 Ghost 进行生产。

我正在本地运行 Google 云 sql 代理。当我运行 NODE_ENV=生产 knex-migrator init --mgpath node_modules/ghost 时,我收到此错误消息:

NAME: RollbackError
CODE: ER_ACCESS_DENIED_ERROR
MESSAGE: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'cloudsqlproxy~[SOME_IP_ADDRESS]' (using password: NO)

运行 knex-migrator init --mgpath node_modules/ghost 工作得很好,我可以在本地启动应用程序,没有任何问题。只有当我尝试将应用程序设置为生产时才会遇到问题。

编辑:我可以通过 MySQL Workbench 连接到数据库,使用与下面配置中使用的相同的凭据

这是我的 config.Production.json (已删除私有(private)数据):

{
    "production": {
        "url": "https://MY_PROJECT_ID.appspot.com",
        "fileStorage": false,
        "mail": {},
        "database": {
            "client": "mysql",
            "connection": {
                "socketPath": "/cloudsql/MY_INSTANCE_CONNECTION_NAME",
                "user": "USER",
                "password": "PASSWORD",
                "database": "DATABASE_NAME",
                "charset": "utf8"
            },
            "debug": false
        },
        "server": {
            "host": "0.0.0.0",
            "port": "2368"
        },
        "paths": {
            "contentPath": "content/"
        }
    }
}

app.yaml:

runtime: nodejs
env: flex
manual_scaling:
  instances: 1
env_variables:
  MYSQL_USER: ******
  MYSQL_PASSWORD: ******
  MYSQL_DATABASE: ******
  # e.g. my-awesome-project:us-central1:my-cloud-sql-instance-name
  INSTANCE_CONNECTION_NAME: ******
beta_settings:
  # The connection name of your instance on its Overview page in the Google
  # Cloud Platform Console, or use `YOUR_PROJECT_ID:YOUR_REGION:YOUR_INSTANCE_NAME`
  cloud_sql_instances: ******

# Setting to keep gcloud from uploading not required files for deployment
skip_files:
  - ^(.*/)?#.*#$
  - ^(.*/)?.*~$
  - ^(.*/)?.*\.py[co]$
  - ^(.*/)?.*/RCS/.*$
  - ^(.*/)?\..*$
  - ^(.*/)?.*\.ts$
  - ^(.*/)?config\.development\.json$

最佳答案

文件ghost.prod.config.js不是 Ghost 识别的东西 - 我不确定该文件名来自哪里,但 Ghost < 1.0 使用 config.js所有环境都在一个文件中,并且 Ghost >= 1.0 使用 config.<env>.json每个环境都在其自己的文件中。

您的config.production.json文件不包含您的 MySQL 连接信息,因此 knex-migrator 工具无法连接到您的数据库。

如果合并 ghost.prod.config.js 的内容进入config.producton.json这应该可以正常工作。

您的 config.production.json 应如下所示:

 {
     "url": "https://something.appspot.com",
     "database": {
         "client": "mysql",
         "connection": {
             "socketPath": "path",
             "user": "user",
             "password": "password",
             "database": "dbname",
             "charset": "utf8"
        }        
    }
}

这里需要注意的是,新的 JSON 格式不能包含代码或逻辑,只能包含显式值,例如process.env.PORT || "2368"不再允许。

相反,您需要使用参数或环境变量来提供动态配置。有关如何使用环境变量的文档位于:https://docs.ghost.org/docs/config#section-running-ghost-with-config-env-variables

例如NODE_ENV=production port=[your port] database__connection__user=[your user] ...etc... knex-migrator init --mgpath node_modules/ghost

您需要为配置中的每个动态变量添加一个环境变量。

关于node.js - 将 Ghost 与 Google Cloud SQL 结合使用时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48756880/

相关文章:

javascript - 如何在 Promise 循环后运行回调?

javascript - 发布数据未被识别 Node js

node.js - Nodejs 异步等待循环

kubernetes - Google Kubernetes引擎上的Traefik错误

python - Google App Engine 上的 Django 在开发环境中使用 Cloud SQL

java - 使用简单的 Java 程序 (JDBC) 连接到 Google SQL

node.js - 如何在nodejs中处理多个端点

javascript - 类型错误 : Cannot read property 'redirect_uris' of undefined

kubernetes - Kubectl无法描述HPA

google-cloud-sql - 谷歌云 SQL : unix socket URI format? 上的 PostgREST