Google App Engine Flexible 上的 Symfony3 无法连接到 Google Cloud SQL MySQL

Symfony3 on Google App Engine Flexible can't connect to Google Cloud SQL MySQL

我无法在 Google App Engine 上获取我的 Symfony 3 项目灵活地连接到 Google 云 SQL(MySQL,第 2 代)。

从这些例子开始

https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/appengine/flexible/symfony

https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/appengine/flexible/cloudsql-mysql

我在 europe-west3 创建了一个 Google App Engine 和一个 Google Cloud SQL(MySQL,第 2 代)。 我在我的 GAE 上测试了这些示例,两者都 运行 很好。

然后我继续从头开始创建一个新的、非常简单的 Symfony 3.3 项目。 创建后(只有欢迎屏幕和 "Your application is now ready. You can start working on it at: /app/")我将其部署到我的 GAEflex 并且工作正常。

之后我添加了一个简单的实体和一个额外的控制器。 当启动 Googles cloud_sql_proxy 和 Symfony 内置网络服务器 "php bin/console server:run" 时,这工作正常,它应该如此。 它从云端读取和写入云端 SQL,一切都很简单。

但是,无论我尝试什么,我总是在 "symfony-scripts" "post-install-cmd" 中的一个命令(或另一个,取决于我的实验状态)上得到 "Connection Refused"我的 composer.json.

“$ gcloud beta app deploy”输出(摘录)

...
Step #1: Install PHP extensions...
Step #1: Running composer...
Step #1: Loading composer repositories with package information
...
Step #1:     [Doctrine\DBAL\Exception\ConnectionException]
Step #1:     An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused
Step #1:
Step #1:
Step #1:     [Doctrine\DBAL\Driver\PDOException]
Step #1:     SQLSTATE[HY000] [2002] Connection refused
Step #1:
Step #1:
Step #1:     [PDOException]
Step #1:     SQLSTATE[HY000] [2002] Connection refused
...

parameters.yml(摘录)

parameters:
    database_host: 127.0.0.1
    database_port: 3306
    database_name: gae-test-project
    database_user: root
    database_password: secretpassword

config.yml(摘录)

doctrine:
    dbal:
        driver: pdo_mysql
        host: '%database_host%'
        port: '%database_port%'
        unix_socket: '/cloudsql/restof:europe-west3:connectionname'
        dbname: '%database_name%'
        user: '%database_user%'
        password: '%database_password%'
        charset: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci

composer.json(摘录)

"require": {
    "php": "^7.1",
    "doctrine/doctrine-bundle": "^1.6",
    "doctrine/orm": "^2.5",
    "incenteev/composer-parameter-handler": "^2.0",
    "sensio/distribution-bundle": "^5.0.19",
    "sensio/framework-extra-bundle": "^3.0.2",
    "sensio/generator-bundle": "^3.0",
    "symfony/monolog-bundle": "^3.1.0",
    "symfony/polyfill-apcu": "^1.0",
    "symfony/swiftmailer-bundle": "^2.3.10",
    "symfony/symfony": "3.3.*",
    "twig/twig": "^1.0||^2.0"
},
"require-dev": {
    "symfony/phpunit-bridge": "^3.0"
},
"scripts": {
    "symfony-scripts": [
        "Incenteev\ParameterHandler\ScriptHandler::buildParameters",
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap",
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache",
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets",
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile",
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget"
    ],
    "post-install-cmd": [
        "chmod -R ug+w $APP_DIR/var",
        "@symfony-scripts"
    ],
    "post-update-cmd": [
        "@symfony-scripts"
    ]
},

app.yml(完成)

service: default
runtime: php
env: flex

runtime_config:
  document_root: web

env_variables:
  SYMFONY_ENV: prod
  MYSQL_USER: root
  MYSQL_PASSWORD: secretpassword
  MYSQL_DSN: mysql:unix_socket=/cloudsql/restof:europe-west3:connectionname;dbname=gae-test-project
  WHITELIST_FUNCTIONS: libxml_disable_entity_loader

#[START cloudsql_settings]
# Use the connection name obtained when configuring your Cloud SQL instance.
beta_settings:
    cloud_sql_instances: "restof:europe-west3:connectionname"
#[END cloudsql_settings]

请记住,我只添加或更改了示例和文档建议我添加或更改的行。

我做错了什么? 这些示例都工作正常,通过 运行ning cloud_sql_proxy 连接到数据库的 Symfony 项目工作正常,当我尝试从 GAEflex I 中获取连接到 CloudSQL 的学说时没有连接。

谁能发现我可能犯的菜鸟错误? 有没有人遇到过这个问题,也许我忘记了 GAE 或 CloudSQL 的一个或另一个设置? 谁能告诉我一个使用 GAEFlex 和 CloudSQL 的工作 Symfony3/Doctrine 项目的存储库?

我被困住了,因为我找不到任何关于如何获得这个相当明显的 运行 组合的最新文档。 非常感谢任何帮助!

干杯/卡斯滕

我找到了。 我读了这个 post Flexible env: post-deploy-cmd from composer.json it not being executed,它产生了两个有趣的信息。

  1. PHP 的 App Engine Flex 运行 post-deploy-cmd不再
  2. 只需使用 post-install-cmd 作为替代品,除非您需要数据库连接。

因此,在部署期间,根本没有可用的数据库连接/套接字! 这就是 Google 引入(非标准)post-deploy-cmd 的原因,不幸的是,它似乎引起了其他一些麻烦,因此他们再次将其删除。无赖。

实际上,截至目前,无法使用 任何利用 post-install-cmd.

中的数据库的东西

在上面的例子中,我只是改变了

composer.json(节选)

"scripts": {
    "symfony-scripts": [
        "Incenteev\ParameterHandler\ScriptHandler::buildParameters",
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap",
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache",
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets",
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile",
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget"
    ],
    "pre-install-cmd": [
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets",
    "post-install-cmd": [
        "chmod -R ug+w $APP_DIR/var",
        "Incenteev\ParameterHandler\ScriptHandler::buildParameters",
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap",
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache",
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile",
        "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget"
    ],
    "post-update-cmd": [
        "@symfony-scripts"
    ]
},

一切都运行很好...

我不确定 installAssets 的内部结构,当我将它移动到预安装时是否仍能达到最初预期的效果,但是,它适用于一个非常简单的示例应用程序.. . 在接下来的几天里,我将努力将它应用到我更复杂的应用程序中,看看我是否可以让它工作。

进一步的讨论很可能会在 Google Cloud Platform Community

结束

大家干杯!