Why do I get a SQLSTATE[HY000] [2002] Connection refused error when running an artisan command in my EC2 instance?

Viewed 314

I have a Laravel web app set up on Elastic Beanstalk and it can connect to the RDS database just fine. I have the environment variables set up through the configuration setting in EB. When I ssh into the EC2 instance and run a custom artisan command that queries the database like "sudo php artisan check-companies", I get the error SQLSTATE[HY000] [2002] Connection refused (SQL: the query)". What could be causing this error? Here are a few things to note and what I've tried.

  1. I have workers that run the same artisan command every day and it works.
  2. I'm able to connect to the Aurora Cluster from MYSQL command line utility and run queries when I ssh into the instance.
  3. I've tried exposing the environment variables to the instance as shown here: https://aws.amazon.com/premiumsupport/knowledge-center/elastic-beanstalk-env-variables-shell/
  4. I made sure port 3306 is open in my security group
1 Answers

Did you check if your sudo user has access to the env vars? That was the issue for me, I found this link:

https://askubuntu.com/questions/161924/how-do-i-set-persistent-environment-variables-for-root

Which suggests that sudo users should take the env vars from the base user essentially.

I was able to test this, by doing a printenv on my server and checking the env vars as the sudo and as the base user I had created. I could see these were different and this meant I was not pulling through my env vars.

Using sudo -E su allowed me to access for my testing purposes as I did not want to persist, but if you do, the answer is in the link above.

Related