1

我无法让我在 Google App Engine Flexible 上的 Symfony 3 项目连接到 Google Cloud 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 项目。创建后(只有欢迎屏幕和“您的应用程序现已准备就绪。您可以在以下位置开始处理它:/app/”)我将它部署到我的 GAEflex 并且它工作正常。

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

但是,无论我尝试什么,我的 composer.json 中的“symfony-scripts”“post-install-cmd”命令中的一个(或另一个,取决于我的实验状态)总是得到“拒绝连接” .

“$ 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
...

参数.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]

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

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

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

我被困住了,因为我找不到任何关于如何让这个相当明显的组合运行的最新文档。任何帮助是极大的赞赏!

干杯/卡斯滕

4

1 回答 1

3

我找到了。我从 composer.json 阅读了这篇文章 Flexible env: post-deploy-cmd 它没有被执行,这产生了两个有趣的信息。

  1. post-deploy-cmdApp Engine Flex for PHP ,不再运行
  2. 除非您需要数据库连接,否则仅用post-install-cmd作替代品。

因此,在部署期间,根本没有可用的数据库连接/套接字!这就是为什么谷歌引入了(非标准)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 社区结束

大家加油!

于 2017-11-13T10:57:26.440 回答