Unable to establish MySQL database connection in local PHP testing environment

StackOverflow https://stackoverflow.com/questions/8565895

  •  21-03-2021
  •  | 
  •  

سؤال

When I run the following page on my local development environment I'm getting a timeout error on the TCP connection. I've inherited this site recently from another developer. I've pulled all the files on the server and dropped them into my /Users/mycomputer/Sites/testsite.com folder on my MacBook Pro running Lion.

The code I'm running (obviously I've sanitized the code for posting here):

<?php
mysql_connect("mysqldb", "usernamehere", "passwordhere") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("test") or die(mysql_error());
echo "Connected to Database";
?>

And the error:

Warning: mysql_connect() [function.mysql-connect]: [2002] Operation timed out (trying to connect via tcp://mysqldb:3306) in /Users/mycomputer/Sites/testsite.com/dbtest.php on line 2
Warning: mysql_connect() [function.mysql-connect]: Operation timed out in /Users/mycomputer/Sites/testsite.com/dbtest.php on line 2
Operation timed out

Any ideas on where I've gone wrong? I'm pretty unskilled in the PHP world so, forgive my ignorance. If this were ColdFusion I would suspect a datasource problem.

هل كانت مفيدة؟

المحلول

Firstly, you need to check whether your website allows external IPs to connect to the DB. If they do, you might have to add your current IP to the list of allowed IPs.

Then, you need to connect to the DB using their external db address. If you do all that correctly, this should work. You error shows that it can't connect to the DB.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top