【问题标题】:Symfony3 on Google App Engine Flexible can't connect to Google Cloud SQL MySQLGoogle App Engine Flexible 上的 Symfony3 无法连接到 Google Cloud SQL MySQL
【发布时间】:2017-11-08 15:37:19
【问题描述】:

我无法让我在 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 读取和写入数据,一切都很简单。

但是,无论我尝试什么,我总是在我的“symfony-scripts”“post-install-cmd”命令的一个(或另一个,取决于我的实验状态)上得到一个“拒绝连接”作曲家.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]

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

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

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

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

干杯/卡斯滕

【问题讨论】:

  • 我不熟悉 Symfony,但是如果你从 config.yml 中删除主机/端口行会发生什么?错误消息(“连接被拒绝”)表明它正在尝试通过 TCP 连接,而不是使用 UNIX 套接字。
  • 您对 TCP 的“拒绝连接”提示是正确的,但是,不,然后它说“SQLSTATE [HY000] [2002] 没有这样的文件或目录”。我有没有提到我这几天一直在思考这个问题?在我找不到一个例子之后,我宁愿尝试我能想到的一切,除了在键盘上滚动我的头。这就是为什么我问是否有人可以明确指出我的错误或可行的项目。
  • 您能否再次检查您是否已启用 Cloud SQL API?请参阅此处“开始之前”下的第 2 点:cloud.google.com/appengine/docs/flexible/php/using-cloud-sql
  • 瓦迪姆 ^_^ 请。

标签: google-app-engine google-cloud-sql symfony-3.3 google-app-engine-php symfony3.x


【解决方案1】:

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

  1. App Engine Flex for PHP,不再运行 post-deploy-cmdany
  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 Community结束

大家干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-18
    • 2019-11-27
    • 2017-08-28
    • 1970-01-01
    • 2016-03-08
    • 2020-06-07
    • 1970-01-01
    • 2019-02-25
    相关资源
    最近更新 更多