Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL working with 127.0.0.1 but not working with localhost?

I have a strange problem in php MySQL:

php connects with 127.0.0.1 but not with localhost.

How to solve this issue?

like image 927
air Avatar asked Feb 17 '10 19:02

air


People also ask

Why MySQL localhost is not working?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.

How do I run MySQL on localhost?

Testing your Local SQL ServerClick on the “New Connection” icon and leave everything default, except the “Connection Name,” here enter localhost . Double click on the new connection and enter the password you created during installation. Voila! Let's run a command to make sure everything is working.

How do I find my localhost MySQL IP address?

What is the IP address of MySQL server? Hostname: The host name or IP address of the MySQL server. The host name "localhost" might resolve to "127.0. 0.1" or "::1" on your host, so note this when checking permissions.

What is the default localhost of MySQL database?

If the PHP directive mysql. default_host is undefined (default), then the default value is 'localhost:3306'. In SQL safe mode, this parameter is ignored and value 'localhost:3306' is always used.


2 Answers

Try ping localhost in a command line, it may be resolving to ::1: the IP6 equivalent to 127.0.0.1

To fix it, add (or uncomment) the line:

   127.0.0.1       localhost

in C:\WINDOWS\system32\drivers\etc\hosts

like image 165
Carlos Gutiérrez Avatar answered Nov 03 '22 00:11

Carlos Gutiérrez


localhost must be defined in your hosts file, which it is by default on Windows and most OSs.

Check the following text file and see what's up:

C:\WINDOWS\system32\drivers\etc\hosts

Please note "hosts" is the file, no extension, it's not a folder. This file should be pretty much empty, if you see entries either your AV/spam blocker added a ton of entries that point to 127.0.0.1 or nasty malware has written garbage in there, despite, newer entries are below older ones. Localhost should be the first entry in that file.

like image 5
TravisO Avatar answered Nov 03 '22 01:11

TravisO