Mysql – PHP cannot connect to MySQL

apache-2.2MySQLphp5windows 7

I recently installed Apache 2 + PHP 5.3.1 + MySQL 5.1.44 on my Windows 7 64bit machine following this guide:
http://sleeplessgeek.blogspot.com/2010/01/setting-up-apache-php-mysql-phpmyadmin.html

It all went fine, php is working great (even with XDebug) but I cannot connect to mysql server.

A simple script I wrote to test connection (yes, root has no pass):

    $username = "root";
    $password = "";
    $database = "test";
    $hostname = "localhost";

$conn = mysql_connect($hostname, $username, $password)
    or die("Unable to connect to MySQL Database!!");

It prints this error after 60sec of timeout:

Warning: mysql_connect() [function.mysql-connect]: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

I can connect to mysql using cmd>mysql -h localhost -u root

Services are working properly.


There also seems to be a problem with PhpMyAdmin (using 3.2.5). As soon as I type user and pass the page loads and turns blank (content-lenght in headers is 0 but status code is 302 Found). Looks like something wrong with cookies (my auth method).

I hope someone has a clue, it has to be something dumb simple I missed.
Thanks in advance.

Best Answer

I have this on my machine since upgrading. In my case, specifying 127.0.0.1 as the host instead of localhost does the trick.

One day it will bother me enough to really fix it :)

Edit regarding phpMyAdmin
Ah, phpMyAdmin... yes! I feel your pain. I HATE with a passion every time I am forced to re-install this app. It's a brilliant app when it works, but getting it into a working state has caused me a lot of grief over time.

In my personal experience, make sure all the required PHP modules are loaded and make sure your PHP sessions directory (as specified in php.ini) actually exists on the filesystem. If you change any of those, restart Apache.

Also in my experience, restarting Apache alone is not good enough - a browser restart is needed to properly clear the session.

Related Topic