在 Google Cloud SQL 中使用 Ghost 时出现问题

Issue using Ghost with Google Cloud SQL

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

我在本地 运行ning Google 云 sql 代理。当我 运行 NODE_ENV=production 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(删除了私人数据):

{
    "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 无法识别文件 ghost.prod.config.js - 我不确定该文件名的来源,但 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

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

我找到问题了

我的配置文件不应该有 "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": "8080"
        },
        "paths": {
            "contentPath": "content/"
        }
}

它现在覆盖了默认配置。

唯一的问题是您不能将 knex-migrator 与 "socketPath" 属性 集一起使用,但这对于 运行 云中的应用程序是必需的。