Question

My App component can't connect to MySQL DB Cluster, but the same app connects from local.

Hi,

I am not a very technical person, just created this API and a frontEnd to learn a bit more on the topic and I may be missing something very basic, sorry in advance if that’s the case.

I have a PHP API that works great in local (it connects to my MySQL cluster in DigitalOcean from Local, tested in Postman and in MySQL WorkBench) but when I deploy it throws an error on DO server:

SQLSTATE[HY000] [2002] Connection timed out

SAME connection string.

I’m figure out a permissions error happens, but I added my App Component to the Trusted Sources, as my local IP and I am not sure if I should add something else. I downloaded the CA Certificate and added it to the same path as the connection php file:

class Connection {
  _//-----> DB Info_
  _static_ _public_ function infoDatabase() {
    $infoDB **=** array(
      "host" **=>** $_ENV['DB_HOST'],
      "database" **=>** $_ENV['DB_NAME'],
      "user" **=>** $_ENV['DB_USER'],
      "pass" **=>** $_ENV['DB_PASS']
    );

    **return** $infoDB;
  }

  _//-----> DB Connect_
  _static_ _public_ function connect() {
    **try** {

      $options **=** array();
      **if** ($_ENV['APP_ENV'] **!=** 'local') {
        $options **=** array(
          _PDO_**::**MYSQL_ATTR_SSL_VERIFY_SERVER_CERT **=>** true,
          _PDO_**::**MYSQL_ATTR_SSL_CA **=>** "./ca-certificate.crt"
        );
      }
      $link **=** **new** _PDO_(
          sprintf(
              'mysql:host=%s;port=%s;dbname=%s;charset=utf8mb4',
              Connection**::**infoDatabase()["host"],
              25060,
              Connection**::**infoDatabase()["database"]
          ),
          Connection**::**infoDatabase()["user"],
          Connection**::**infoDatabase()["pass"],
          $options
      );

      $link**->**exec("set names utf8");

    }**catch**(PDOException $e) {
      $response **=** array(
        "error" **=>** $e**->**getMessage(),
        "environment" **=>** $_ENV['APP_ENV'],
         "host" **=>** $_ENV['DB_HOST'],
      "database" **=>** $_ENV['DB_NAME'],
      "user" **=>** $_ENV['DB_USER'],
      "pass" **=>** $_ENV['DB_PASS'],
        "isNonLocalEnvironment" **=>** $_ENV['APP_ENV'] **!=** 'local'
      );
      echo json_encode($response);
      **die**();
    }

    **return** $link;
  }

Those Env Variables have the correct values (The same than in the Env Variables section of my App), and here is what $response returns:

{
	"error": "SQLSTATE[HY000] [2002] Connection timed out",
	"environment": "dev",
	"host": "private-db-beapilot-sp-ppl-do-user-14307371-0.b.db.ondigitalocean.com",
	"database": "beapilot",
	"user": "thecorrectuser",
	"pass": "thecorrectpass",
	"isNonLocalEnvironment": true
}

I am not sure what else to check or fix. =(

Thanks in advance for your help!

Daniel


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
July 19, 2023

Hi there,

The Connection timed out error would usually be caused by either your app not being added to the Trusted Sources of the managed database cluster or if you are using an incorrect port.

Can you verify that you are using port 25060 in your connection string?

And also, I would recommend again double checking that the specific app is added to the trusted sources of your database.

Another thing to keep in mind is that during the build stage there will be no network connectivity to the managed database clusters. So if you are trying to run any database migrations during the build stage, it is best to move them to the run stage instead.

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel