Rita Carolina
Rita Carolina

Reputation: 121

How to Connect AWS DB with PHP

I am trying to connect my AWS MYSQL DataBase, but I get this error.

Connection failed: SQLSTATE[HY000] [2002] Connection timed out

I've searched and notice it could a matter of security, adding the IP to the Security Group rules, but it still didn't work.

<?php

$servername = "****-1.cdvmnfrkbalc.us-east-2.rds.amazonaws.com";
$port = 3306;
$username = "admin";
$password = "******";
//$dbname = "phpmyadmin";

try {
    $conn = new PDO("mysql:host=$servername:3306", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully."; 
}
catch(PDOException $e)
{
    echo "Connection failed: " . $e->getMessage();
}
?>

How can I connect my DB to AWS?

Upvotes: 0

Views: 336

Answers (1)

smac2020
smac2020

Reputation: 10724

To successfully connect to a MySQL RDS instance, you have to do 2 things:

  1. set the inbound rules so you can connect from your development environment.
  2. set the RDS instance to allow Public access.

Once you do these 2 tasks, you can connect. See this AWS tutorial on how we setup the RDS instance.

Creating the Amazon Relational Database Service item tracker

Upvotes: 2

Related Questions