如何将 Strapi (GAE) 应用程序连接到 MySQL (GSC) 数据库?

How do I connect a Strapi (GAE) app to a MySQL (GSC) database?

已编辑: 下面的配置现在运行正确!

Package.json:依赖项(部分):

    ...
    "knex": "latest",
    "lodash": "^4.17.5",
    "mysql": "^2.17.1",
    "sqlite3": "^4.1.0",
    "strapi": "3.0.0-beta.15",
    "strapi-admin": "3.0.0-beta.15",
    "strapi-hook-bookshelf": "3.0.0-beta.15",
    "strapi-hook-knex": "3.0.0-beta.15",
    ....

database.json(分期)

{
  "defaultConnection": "default",
  "connections": {
    "default": {
      "connector": "strapi-hook-bookshelf",
      "settings": {
        "client": "mysql",
        "host": "${process.env.MYSQL_HOST || '127.0.0.1'}",
        "port": "${process.env.MYSQL_PORT || '3306'}",
        "database": "${process.env.MYSQL_DATABASE}",
        "username": "${process.env.MYSQL_USERNAME}",
        "password": "${process.env.MYSQL_PASSWORD}",
        "socketPath": "/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}"
      },
      "options": {
      }
    }
  }
}

app.staging.yaml

runtime: nodejs10
service: strapi

env_variables:
  NODE_ENV: staging
  MYSQL_USERNAME: <hidden>
  MYSQL_PASSWORD: <hidden>
  MYSQL_DATABASE: strapi
  # e.g. my-awesome-project:us-central1:my-cloud-sql-instance
  INSTANCE_CONNECTION_NAME: <hidden>:europe-west1:mysql"
beta_settings:
  # The connection name of your instance, available by using
  # 'gcloud beta sql instances describe [INSTANCE_NAME]' or from
  # the Instance details page in the Google Cloud Platform Console.
  cloud_sql_instances: "<hidden>:europe-west1:mysql"

然后我可以通过运行以下命令成功部署上述设置:

gcloud app deploy app.staging.yaml --project <hidden> --quiet

在错误的第一行,您会看到:

Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'undefined'@'cloudsqlproxy~\ (using password: YES)

所以看起来 process.env.MYSQL_USERNAME 是未定义的。确保您在 app.yaml 中正确设置了环境变量。