mysql_connect doesn't work with localhost on my Windows machine

15,551

Solution 1

Filed a bug with PHP. The problem is with MySQL, and has to do with it not correctly connecting to all possible IP addresses for a domain.

Solution? Don't use host names to connect to databases, if it isn't absolutely necessary. This might also have a positive impact on performance.

Solution 2

Are you just having trouble connecting through PHP or can you not establish a connection to MySQL from the command line as well? If you cannot connect at all, verify the service is running and listening on port 3306.

You can also try to change localhost to: 127.0.0.1:3306

Or

$cfg['Servers'][$i]['host'] = 'localhost';

to

$cfg['Servers'][$i]['host'] = '127.0.0.1';

Solution 3

Try adding 127.0.0.1 localhost to C:\Windows\System32\drivers\etc\hosts?

Share:
15,551
Znarkus
Author by

Znarkus

Updated on June 04, 2022

Comments

  • Znarkus
    Znarkus almost 2 years

    This does not work with my PHP 5.3.3 installation on WAMP / Windows 7:

    mysql_connect('localhost', 'root', '');
    

    After Maximum execution time, this error is presented:

    Warning: mysql_connect() [function.mysql-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306)

    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.

    This works:

    mysql_connect('127.0.0.1', 'root', '');
    

    I'm wondering if it has something to do with this, from the PHP docs:

    Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as in your PHP configuration and leave the server field blank.

    This is from my php.ini:

    ; Default socket name for local MySQL connects.  If empty, uses the built-in
    ; MySQL defaults.
    ; http://php.net/mysql.default-socket
    mysql.default_socket =
    

    Is this a bug, or what is going on?


    Update: I'm using MySQL 5.1.36 something, and I can connect to it otherwise. Forgot to mention, but this works with PHP 5.3.0 and 5.2.11. Ofcourse I can just change localhost to 127.0.0.1, but I want to know why I can't use localhost.


    Update 2: Adding (uncommenting) 127.0.0.1 localhost to the hosts file made it work. But why is this necessary?

  • Wouter Dorgelo
    Wouter Dorgelo over 13 years
    Also an option: uncomment the # 127.0.0.1 localhost in: C:/Windows/System32/drivers/etc ;-)
  • Znarkus
    Znarkus over 13 years
    Amazing, that actually worked. But I shouldn't need to add it, why must I do you think?
  • Theodore R. Smith
    Theodore R. Smith over 13 years
    Mark his question as answered and I'll tell you.
  • Wouter Dorgelo
    Wouter Dorgelo over 13 years
    In Windows 7 ip: 127.0.0.1 doesn't exist by default. By uncommenting # 127.0.0.1, you make it exist. Just a small mistake by Microsoft.
  • Znarkus
    Znarkus over 13 years
    I'm not connecting to ., but localhost.
  • Ardee Aram
    Ardee Aram over 13 years
    Just a heads up, the bug that you filed has been closed as "not a bug". I think it's more of a MySQL problem than anything else.
  • Ardee Aram
    Ardee Aram over 13 years
    Nope, not a mistake by Microsoft, but by MySQL: blogs.iis.net/donraman/archive/2010/06/11/…