4

I typically connect using the following:

$db_host = "localhost"; $db_username = "bill"; $db_pass = "mypassword"; $db_name = "theDB"; mysql_connect("$db_host","$db_username","$db_pass", TRUE) or die(mysql_error()); mysql_select_db("$db_name") or die("no database by that name"); 

I tried changing the host to the RDS endpoint below with no luck:

$db_host = "mysql7.1234567890123.us-west-2.rds.amazonaws.com"; 

-The security group has inbound rules that allow my IP.

-I've tried adding "10.0.0.0/8" to the security group from Connecting From PHP Server to MySQL Server on Amazon AWS

Other info: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_PHP_legacy.rds.html

1
  • 2
    I want to point that ancient mysql functions. They are deprecated, you must switch PDO or mysqli Commented Mar 13, 2015 at 18:45

2 Answers 2

5

There are couple of things and use PDO or mysqli to connect. If you've made sure that you can connect to RDS by setting the right security group, then you've got the main issues squared away.

Connect to Amazon RDS with this in PHP:

$dbhost = $_SERVER['RDS_HOSTNAME']; $dbport = $_SERVER['RDS_PORT']; $dbname = $_SERVER['RDS_DB_NAME']; $dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname}"; $username = $_SERVER['RDS_USERNAME']; $password = $_SERVER['RDS_PASSWORD']; $dbh = new PDO($dsn, $username, $password); $link = mysqli_connect($_SERVER['RDS_HOSTNAME'], $_SERVER['RDS_USERNAME'], $_SERVER['RDS_PASSWORD'], $_SERVER['RDS_DB_NAME'], $_SERVER['RDS_PORT']); 

Example 1. Example using PDO to connect to an RDS database

$dsn = 'mysql:host=mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com;port=3306;dbname=mydb'; $username = 'sa'; $password = 'mypassword'; $dbh = new PDO($dsn, $username, $password); 

Example 2. Example using mysqli_connect() to connect to an RDS database

$link = mysqli_connect('mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com', 'sa', 'mypassword', 'mydb', 3306); 

if nothing works, launch a mysql client like navicat or mysql workbench (free) and punch in all the necessary fields and try to connect and see if it loads the db.

Sign up to request clarification or add additional context in comments.

2 Comments

I can connect through MySQL Workbench no problem. But, don't I have to connect through a PHP script each time I run a query?
yes you do and i suggested that for a test to make sure your RDS is active and you can connect and your security groups are setup correctly. Now, you should connect using the php script but use either PDO or mysqli to perform the query and when making the connection.
0

This ended up working for me. Thanks for you help!

$db_host = 'instance2.123456789.us-west-2.rds.amazonaws.com:3306'; //RDS Endpoint... $db_username = 'myusername'; $db_pass = 'mypassword'; $db_name = 'myDB'; mysql_connect("$db_host","$db_username","$db_pass", TRUE) or die(mysql_error()); mysql_select_db("$db_name") or die("no database by that name"); 

1 Comment

tha'ts great that you figured it out and you do need to specify this ":3306" in your host.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.