"连接被拒绝"在创建Elastic Beanstalk环境时

时间:2017-07-09 18:26:14

标签: mysql laravel amazon-web-services laravel-5 elastic-beanstalk

我按照本教程在Elastic Beanstalk环境中设置Laravel:

https://deliciousbrains.com/scaling-laravel-using-aws-elastic-beanstalk-part-3-setting-elastic-beanstalk/

我已经完成了两次全新安装的Laravel,只是为了看看它是否有效,并且两次都有效。

现在,我已经完成了它,但这次是在我的主要Laravel项目上。我已经仔细检查了教程中的所有内容,并且我确信我没有错过任何内容。

但是,当我使用教程中的这个命令创建环境时(当然填充了值):

eb create --vpc.id {VPCID} --vpc.elbpublic --vpc.elbsubnets {VPCELBSUBNETS} --vpc.ec2subnets {VPCEC2SUBNETS} --vpc.securitygroups {VPCSG}

我收到以下输出错误:

Printing Status:
INFO: createEnvironment is starting.
INFO: Using elasticbeanstalk-us-east-1-487650495335 as Amazon S3 storage bucket for environment data.
INFO: Created security group named: sg-018fe470
INFO: Created load balancer named: awseb-e-7-AWSEBLoa-1M3V7HA824OQ0
INFO: Created security group named: sg-7489ec08
INFO: Environment health has transitioned to Pending. Initialization in progress (running for 19 seconds). There are no instances.
INFO: Created Auto Scaling launch configuration named: awseb-e-7xdtjzn4bn-stack-AWSEBAutoScalingLaunchConfiguration-RZPSBCGFS6HY
INFO: Added instance [i-09cc6faf451ef3670] to your environment.
INFO: Created Auto Scaling group named: awseb-e-7xdtjzn4bn-stack-AWSEBAutoScalingGroup-BQI6UG2OLL7E
INFO: Waiting for EC2 instances to launch. This may take a few minutes.
INFO: Created Auto Scaling group policy named: arn:aws:autoscaling:us-east-1:457680865345:scalingPolicy:a3629314-6d24-4871-a0a1-59d74a1087c2:autoScalingGroupName/awseb-e-7xdtjzn4bn-stack-AWSEBAutoScalingGroup-BQI6UG2OLL7E:policyName/awseb-e-7xdtjzn4bn-stack-AWSEBAutoScalingScaleDownPolicy-1SM372VEND7T6
INFO: Created Auto Scaling group policy named: arn:aws:autoscaling:us-east-1:457680865345:scalingPolicy:b03a08fb-e39f-4dc5-8e00-f81f8059fc56:autoScalingGroupName/awseb-e-7xdtjzn4bn-stack-AWSEBAutoScalingGroup-BQI6UG2OLL7E:policyName/awseb-e-7xdtjzn4bn-stack-AWSEBAutoScalingScaleUpPolicy-AS65NNA5M4PP
INFO: Created CloudWatch alarm named: awseb-e-7xdtjzn4bn-stack-AWSEBCloudwatchAlarmLow-1D9SO13U3HBR0
INFO: Created CloudWatch alarm named: awseb-e-7xdtjzn4bn-stack-AWSEBCloudwatchAlarmHigh-1FUCKP1GWED3A
ERROR: [Instance: i-09cc6faf451ef3670] Command failed on instance. Return code: 1 Output: (TRUNCATED)...
  [PDOException]
  SQLSTATE[HY000] [2002] Connection refused


Script php artisan optimize handling the post-install-cmd event returned with error code 1.
Hook /opt/elasticbeanstalk/hooks/appdeploy/pre/10_composer_install.sh failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.
INFO: Command execution completed on all instances. Summary: [Successful: 0, Failed: 1].
ERROR: Create environment operation is complete, but with errors. For more information, see troubleshooting documentation.

正如您所看到的,在post-install-cmd中运行SQLSTATE[HY000] [2002] Connection refused时会显示php artisan optimize

如果我尝试在命令行上连接到我的MySQL RDS数据库,它会成功连接,所以看起来连接不是问题。

我花了一段时间才找到post-install-cmd的含义,但我在composer.json文件中找到了它:

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.6.4",
        "laravel/framework": "5.3.*",
        "doctrine/dbal": "^2.5",
        "embed/embed": "^2.7",
        "pda/pheanstalk": "~3.0",
        "guzzlehttp/guzzle": "^6.2",
        "intervention/image": "^2.3",
        "approached/laravel-image-optimizer": "2.3.0",
        "php-ffmpeg/php-ffmpeg": "0.9.3",
        "laravelcollective/html": "^5.3",
        "tymon/jwt-auth": "0.5.*",
        "brozot/laravel-fcm": "^1.2",
        "league/flysystem": "^1.0",
        "cybercog/laravel-ban": "^2.1",
        "pragmarx/firewall": "^1.0",
        "predis/predis": "^1.1",
        "aws/aws-sdk-php": "^3.31",
        "dusterio/laravel-aws-worker": "^0.1.9",
        "fideloper/proxy": "^3.3",
        "aws/aws-sdk-php-laravel": "^3.1",
        "league/flysystem-aws-s3-v3": "^1.0"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~5.0",
        "symfony/css-selector": "3.1.*",
        "symfony/dom-crawler": "3.1.*"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}

对我来说,看起来任何事情都不合适。

问题是什么?

更新

我只是尝试将我项目composer.json的内容复制并粘贴到Laravel的全新安装中,并成功创建了一个Elastic Beanstalk环境,所以看起来我的composer.json isn& #39;问题。

它可能是什么?

更新#2

eb logs,我发现了这个:

[Illuminate\Database\QueryException]                                                                                                                                                              
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `categories` where exists (select * from `topics` where `topics`.`category_id` = `categories`.`id`) order by `name` asc)

[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000] [2002] Connection refused

调用此查询的唯一位置是我的AppServiceProvider.php类。所以我删除了它,以及我添加到课程中的所有其他内容,但我仍然得到上面完全相同的错误,即使我删除了该查询调用。

为什么?

1 个答案:

答案 0 :(得分:0)

要回答我自己的问题,我的AppServiceProvider.php文件中的代码(在boot()方法中)导致其失败。

我所做的是用boot()方法包装所有代码:

public function boot()
{
    if (!$this->app->runningInConsole())
    {
        // Code here
    }
}