-1

I do not believe the question to be a duplicate as I believe the issue is related to Shared Hosting environments getting outbound to other cloud mysql servers.

Error message

PDO Exception: SQLSTATE[HY000] [2002] Connection timed out.

I am simply just trying to connect to my instance of AWS RDS and run a query on one of my tables.

SOLUTION: GODADDY BLOCKS ALL PORTS EXCEPT 21 AND 22 (FTP) ON A STANDARD ACCOUNT. THIS IS WHAT I WAS TOLD BY GODADDY TECHNICAL SUPPORT. HENCE, IT WILL BLOCK PORT 1234 AND EVEN 3306 FOR A DATABASE CONNECTION.

UPDATE:

@Drew has validated that this php code works for the connection:

try {
        $db = new PDO('mysql:host=uwmparkingbackend.cb7gmuw49vhs.us-east-1.rds.amazonaws.com;port=1234;dbname=uwm_parking;charset=utf8', 'fbgrecojr', 'test1234');
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

        echo "Connected and Happy<br>";
        $sql="select id,thing from thingsCoveted where id in (1,3)";
        foreach ($db->query($sql) as $row) {
            print $row['id'] . "\t";
            print $row['thing'] . "<br>";
        }

    } catch (PDOException $e) {
        echo 'PDO Exception: ' . $e->getMessage();
        exit();
    }

I am getting this error however on my end:

PDO Exception: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'uwmparkingbackend.cb7gmuw49vhs.us-east-1.rds.amazonaws.com' (110)

Hence, there is either a config issue on the Godaddy side (where the .php is hosted) or the AWS RDS side (where the database is hosted);

Things I have tried: 1) checked to make sure all PDO options are selected in GoDaddy's php settings 2) added an inbound rule for my AWS RDS security group to allow all connections using my port (1234).

frankgreco
  • 1,426
  • 1
  • 18
  • 31
  • 1
    Well the first problem is that you are connecting using MSQLI but running queries using MYSQL. You can not mix and match like that. – David Soussan Nov 06 '15 at 01:27
  • I used MySQLi because of the documentation I found here: http://www.w3schools.com/php/php_mysql_connect.asp – frankgreco Nov 06 '15 at 01:32
  • so what do you call `mysql_query` and `mysql_num_rows` and `mysql_fetch_assoc` and `mysql_error`? @DavidSoussan told you already. What are we not grasping here? – Funk Forty Niner Nov 06 '15 at 02:06
  • as per your edit, you're using PDO to connect with, then using `mysql_` functions to query with. Again... you can NOT mix different MySQL APIs. Use the same API from connection to query, nothing else. Your question was closed accordingly. – Funk Forty Niner Nov 06 '15 at 02:08
  • My question is not a duplicate as I believe the issue to be related to AWS. – frankgreco Nov 06 '15 at 02:31
  • and you are ok if we point to it and try to connect, is that right ? – Drew Nov 06 '15 at 03:38
  • are you saying that you would like to try to connect to it? – frankgreco Nov 06 '15 at 03:39
  • if it would help you and you want me to. You could create a different db – Drew Nov 06 '15 at 03:41
  • 1
    $dbhost = 'uwmparkingbackend.cb7gmuw49vhs.us-east-1.rds.amazonaws.com'; $dbport = '1234'; $dbname = 'uwm_parking'; $dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname}"; $username = 'fbgrecojr'; $password = 'test1234'; – frankgreco Nov 06 '15 at 03:44
  • alright I created a table. Let me see if I can hit it with pdo – Drew Nov 06 '15 at 03:49
  • got in no problem. Where do you want the PHP file. Pastie.org or an email to a spam email address – Drew Nov 06 '15 at 03:56
  • no-reply@frankgrecojr.com - its the feedback email for my website - let's see if I can get it your code to work. – frankgreco Nov 06 '15 at 03:58
  • actually let's use pastie.org – frankgreco Nov 06 '15 at 04:04
  • ok you have it it two places then, maybe. http://pastie.org/10533333 – Drew Nov 06 '15 at 04:05
  • I get this: PDO Exception: SQLSTATE[HY000] [2002] Connection timed out. So we've ruled out an error with the php. Could there be an issue with GoDaddy? Would I need a custom php.ini file? – frankgreco Nov 06 '15 at 04:08
  • I will look that up. In the meantime run this `` on GoDaddy – Drew Nov 06 '15 at 04:10
  • I did hear a few days ago about GoDaddy and pdo issues unless I am imagining it – Drew Nov 06 '15 at 04:11
  • http://stackoverflow.com/a/21844583 – Drew Nov 06 '15 at 04:12
  • Let me check my settings in go daddy – frankgreco Nov 06 '15 at 04:15
  • all of the pdf options were already checked – frankgreco Nov 06 '15 at 04:22
  • btw you know the **latency** on this would be dreadful even if it worked (still looking into it). You know you can get a cheap (free tier first year) ec2 acct. Point your dns entries up to the static IP address given to you in ec2 Elastic IP – Drew Nov 06 '15 at 04:46
  • Here is my angle on it. GoDaddy accepts connection on port 80. PHP in turn says lets go out on 1234 or 3306 to somewhere. Wait, we don't want their bandwidth like that (so let's block it). Frustrate them in the shared world of shared timeouts after 60 seconds, and get them to pay $50 and up a month for VPS. Because the only thing standing in the way of how I am doing it, and how you are doing it, is their firewall – Drew Nov 06 '15 at 04:54
  • I would agree with you....I just created an EC2 and am trying to get it set up. – frankgreco Nov 06 '15 at 04:55
  • And just because cPanel says here check this box, well that is all well and fine, so long as the target is **localhost**. I could be wrong. It happens – Drew Nov 06 '15 at 04:55
  • I believe I have successfully set up my ec2 instance as I was able to connect it in workshop using ssh - so now, and i only ask this because of my naiveness with pdo, what do I need to change in my php code to connect it to the ec2? – frankgreco Nov 06 '15 at 05:17
  • this will go on for a while. you have skype ? Usually when I help people with ec2 and key pairs and security groups and ftp servers it goes on and on. :) – Drew Nov 06 '15 at 05:19
  • on second thought I think you need to start over with a fresh question. Abandon this puppy, but maybe reference it (I wouldn't). It just muddies up everything and we get scolded for essentially revamping it. If I was asked to reopen it, I wouldn't ! – Drew Nov 06 '15 at 05:36

1 Answers1

0

The problem is you've just copied and pasted your connection info into the example given by Amazon inside the $_SERVER array.

// Example
$link = mysqli_connect($_SERVER['RDS_HOSTNAME'], $_SERVER['RDS_USERNAME'], $_SERVER['RDS_PASSWORD'], $_SERVER['RDS_DB_NAME'], $_SERVER['RDS_PORT']);                        

// What yours should be
mysqli_connect(
    '***********.******.us-east-1.rds.amazonaws.com', 
    '*******',
    '********',
    'dbname', 
    '1234'
);

You also need to use mysqli functions since that's how you're connecting, e.g. http://php.net/manual/en/mysqli.query.php

EDIT: Now you're using PDO you will need to take a different approach to getting your data.

$dbhost = '***********.******.us-east-1.rds.amazonaws.com';
$dbport = 3306;
$dbname = '*****';

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

try {

    $dbh = new PDO($dsn, $username, $password);
    $query = $dbh->query("SELECT num_spaces FROM uwm_parking.lots");
    while($row = $query->fetch(PDO::FETCH_OBJ)) {
        var_dump( $row );
    }
} catch(PDOException  $e ){
    echo "Error: ".$e;
}
Tim Sheehan
  • 3,994
  • 1
  • 15
  • 18
  • Great point! I have updated my connection string and have updated my query so that it mimics an example shown on php.net. However, when I run it now, when the page loads it seems to get stuck as the progress bar only moves about 10% of the way – frankgreco Nov 06 '15 at 01:45
  • Also their PDO example would be the best way to go rather than mysqli, if you have it enabled on your server. – Tim Sheehan Nov 06 '15 at 01:46
  • See my edit to my code above – frankgreco Nov 06 '15 at 01:55
  • You're still using mysql functions with PDO, when you change the way you connect - I'll edit my answer. – Tim Sheehan Nov 06 '15 at 01:59
  • @Frank I've updated my answer, you were also trying to connect via port 1234, this should be 3306 by default. – Tim Sheehan Nov 06 '15 at 02:06
  • I have mimicked my code to mirror yours but the connection is still timing out. The connection is working fine in workbench. Also, I do not think this question is a duplicate as I believe the problem to be related to AWS. – frankgreco Nov 06 '15 at 02:29
  • Have you checked your AWS security group settings? You need to enable TCP traffic to port 3306 from the IP you're trying to connect to it from. – Tim Sheehan Nov 06 '15 at 02:33
  • Are you using ssh to tunnel your connection in Workbench? – Tim Sheehan Nov 06 '15 at 02:34
  • According to AWS, "DB Security Groups are only needed when the EC2-Classic Platform is supported." I am fairly new to AWS so I could be missing something. Also, I am not using an SSH tunnel in workbench. – frankgreco Nov 06 '15 at 02:37
  • So if you aren't tunneling your mysql connection through SSH and you're able to connect via Workbench there must be some discrepancy between the connection settings you're using in Workbench and the settings you're using in PHP. – Tim Sheehan Nov 06 '15 at 03:05
  • My php script is hosted on GoDaddy. Would that have anything to do with my settings? This may be a stupid question.. – frankgreco Nov 06 '15 at 03:09
  • @Frank you're going to have to allow godaddy to connect to your RDS server, you need to add a VPC security group inbound rule for it. Go into your RDS panel, look for VPC ID next to your db and mark it down, go to security groups and then click the "Go to the EC2 Console" link, find your VPC ID security group and add a TCP rule for your hostgator servers IP for port 3306. – Tim Sheehan Nov 06 '15 at 04:15
  • what I just did was added a "custom TCP rule" inbound rule and set the port to 1234 (I am using this port instead of 3306). then I set "from anywhere". The error I get from that is: PDO Exception: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'uwmparkingbackend.cb7gmuw49vhs.us-east-1.rds.amazonaws.com' (110) – frankgreco Nov 06 '15 at 04:34
  • @Frank check this out - https://forums.aws.amazon.com/message.jspa?messageID=38614 someone at the bottom of this post seems to have sorted it out and was also using GoDaddy hosting. – Tim Sheehan Nov 06 '15 at 05:34
  • where exactly in that article about caching is that about PHP going into a server on GoDaddy and godaddy in turn allowing outbound mysql connections from GoDaddy servers to external servers. I don't even see the words Go or Daddy in there. I see way to go, and undergoing. – Drew Nov 06 '15 at 07:52