0

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

5
  • 1
    Are you sure when you created the RDS the User name used was root? RDS never gives a root access. Commented Aug 9, 2016 at 1:19
  • Where is the PHP server located? Commented Aug 9, 2016 at 1:28
  • the username is not root but that is what it is showing me. The server is located in Texas in the USA. Commented Aug 9, 2016 at 1:33
  • I mean if it is in shared hosting it may be locked down for getting back out in some hosting environments. Not where on a map Commented Aug 9, 2016 at 2:49
  • :-) Sorry, It is a shared server. Commented Aug 9, 2016 at 20:26

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for getting back so quickly. I now get the error message Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'us-west-2.console.aws.amazon.com/rds/home?region=us-west-2' (0)' in /home2/betamath/public_html/graspe/db.php:10 Stack trace: #0 /home2/betamath/public_html/graspe/db.php(10): PDO->__construct('mysql:host=us-w...', 'uname', 'pword') #1 {main} thrown in /home2/betamath/public_html/graspe/db.php on line 10 I've tried adding the https:// to the start of the URL but I get the same error
That doesn't look like the correct address for an RDS server. At least that's not what my address looks like.
When looking at the RDS dashboard, get the instance's endpoint. I use everything except the colon and the port number (:3306).
You were right. I had the wrong endpoint. Couldn't see the wood for the trees. Thanks so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.