Search code examples
phpmysqldatabaseamazon-ec2rds

Unable to select Database Foo using Ec2 and RDS


I have set up an RDS Database Instance with a security group where I use my EC2 Elastic IP as my CIDR/IP. I have also associated the security group with my EC2.

enter image description here

My security group on the EC2 Instance looks like this. I associated one of the 3306 ports with my Elastic IP.

enter image description here

I have created a database and a table in phpMyAdmin and am trying to test it out by printing out all the values by using the code below:

<?php


    // set database server access variables:
    $host = "XXXXXXX.XXXXXXXXXX.eu-west-1.rds.amazonaws.com";
    $user = "XXXXXXX";
    $pass = "XXXXXXXX";
    $db = "XXXXXXX";    
    $con = mysql_connect($host, $user, $pass, $db);

// Check connection
if (mysql_connect_error())
  {
  echo "Failed to connect to MySQL: " . mysql_connect_error();
  }else { echo "You have connected successfully!";
}


 $result = mysql_query($con, "SELECT * FROM `XXXXX` LIMIT 0, 30 ");

echo "<p>starting again..</p>";

while($row = mysql_fetch_assoc($result)){
    //iterate over all the fields
    foreach($row as $key => $val){
        //generate output
        echo $key . ": " . $val . "<BR />";
    }
}
    mysql_close($connection);

?>

The error that I am getting is Unknown database 'XXXX'. Any ideas?

EDIT 1

I have just changed all the mysqli statements to mysql. But the connection is still not successful i.e. the database cannot be found.

EDIT 2

Here is a screenshot of my mysql privileges.

enter image description here


Solution

  • Unknown database 'XXXX' means the database could not be accessed. This could happen because :

    1. Server is connecting successfully, but database does not exist. Try connecting without giving the database name as a parameter. Then run query show databases;. It will show all the databases there.

    2. You are connecting to wrong server (you added database somewhere else database exists on other server). Checking the databases present can help you identify which server is it.

    It seems that you are able to connect to the Amazon RDS server. But database is not there. Similar error would show if we try to connect to fresh installation of mysql. It has zero databases (other than information_schema and mysql).