KPM KPM - 3 months ago 24
MySQL Question

Cannot Connect from PHP script to AWS database

I'm trying to connect to an aws rds that I created. I can connect no problem using MySQL Workbench but not from the PHP script which I have uploaded to a server. I have a feeling it is something to do with the permissions but I am not sure. Could someone point me in the right direction. Below is the PHP code I am using

<?php
//Database configuration

$dbhost = $_SERVER['https://address.to.aws.rds/rds/home?region=us-west-2'];
$dbport = $_SERVER['3306'];
$dbname = $_SERVER['test_db_name'];

$dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname}";
$username = $_SERVER['username'];
$password = $_SERVER['password'];

$dbh = new PDO($dsn, $username, $password);
}


The error I get when I upload and run this script is

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: NO)' in /home2/betamath/public_html/graspe/db.php:12 Stack trace: #0 /home2/betamath/public_html/graspe/db.php(12): PDO->__construct('mysql:host=;por...', NULL, NULL) #1 {main} thrown in /home2/betamath/public_html/graspe/db.php on line 12


Line 12 is
$dbh = new PDO($dsn, $username, $password);


The permissions I have set up for the aws rds are

HTTP TCP 80 0.0.0.0/0

SSH TCP 22 0.0.0.0/0

MySQL/AURORA TCP 3306 0.0.0.0/0

HTTPS TCP 443 0.0.0.0/0


Again, any help is much appreciated. Thanks

Answer

Try using this:

$dbname = "database_name";
$dbuser = "database_user"; 
$dbpass = "database_password"; 
$dbhost01 = "user.regioninfo.rds.amazonaws.com"; // did not use http or https; info provided from AWS
$dbport = 3306;

$dbh = new PDO("mysql:host=$dbhost01;dbname=$dbname", $dbuser, $dbpass);
Comments