I have the following PHP code to connect to my db:
Maybe your MySQL is not using TCP for localhost. Please try
$host='/tmp/mysql.sock';
or whatever socket it might be using.
I also have noticed this issue linux version of xampp with MySSQL 5.6.12-log running over network. Also, a question asked in MySQL forum (http://forums.mysql.com/read.php?52,294772,294772) resembles this, but not satisfactorily answered.
What I have noticed is, it is probably due to Networking being unstable, or too many mysql connections from other developers in the network connecting to the database server at the same time - and keeping the port number busy for a fraction of time. It is not often related to the configured connection time, nor maximum execution time, nor wrong username/password etc.
Amazingly, the same script, same password, and same network allows the same server run properly on the second attempt - without having to do anything as a fix.
Hence, it should be your momentarily unstable network fluctuation (or busy mysql connections), that goes on and off, and your script attempted to connect to the server when network was down for a very short time.
That is a guessed answer from personal experience related to this error, may not be exact.
But if the error message is persistent, you should really need to do something.
Check the following:
localhost
to 127.0.0.1
Basically, the errors you're getting mean that it cannot connect to the server. It sends request to localhost:3306
, and only waits so long for a reply. it's not getting it, which means the request is either blocked (firewall) or ignored (MySQL is not running and/or is listening on a different port)
If phpMyAdmin came with the MySQL install, then it could be that it was configured to use the appropriately different port
ha i also had that issue now i fix it by changing port number e.g "find.db.13344.hostedfind.com:3306". before it my connection was trying to access the "find.db.13344.hostedfind.com:3307" so it gave me this error Warning: mysql_connect() [function.mysql-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) .