My /etc/hosts file looks like this: localhost

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

I found while building the server, that although i could ping localhost successfully, in certain configuration files for services like Postfix and Apache and Dovecot, I needed to specify instead of localhost in order to make them work.

I also needed to change the configuration files of certain database driven websites, which used localhost to connect to MySQL.

I've tried various modifications to the hosts file, but nothing I've tried so far has helped.

The server is running Ubuntu 12.04.02 LTS. It does not have selinux installed and the above situation remains even after setting IPtables default policies to accept and flushing them.

7 Answers 7


One thing you might check is (which requires you to login to the MySQL console) - check to make sure that you have permissions to login to root via localhost.

mysql -h -u root -p

-- Once you have successfully logged in --

mysql> select user,host from mysql.user;
| user | host                           |
| root |                      | 
| root | ::1                            |
| root | localhost                      | <-- Make sure you have a localhost entry for root
3 rows in set (0.00 sec)

Just throwing it out there, just in case this is what the issue is.

  • I've a feeling you're on the right track. I've done that and I can see that although root and couple of other users were created for localhost, those created by the hosting control panel for users are all for (except for one which is %). I think this probably reveals enough for me to fix it, so I will probably accept your answer in short while. Commented Oct 7, 2013 at 3:54
  • thanks for the help! i'd like to add that a mysql server restart is required after updating the mysql.user table for the change to take affect.
    – lackovic10
    Commented Jan 11, 2019 at 9:27
  • Although accepted, this is not usually (ever?) the issue, for mysql. See EEAA's answer instead (mysql interprets localhost as socket instead of TCP). Commented Mar 31, 2019 at 15:21

Most MySQL clients are odd in the fact that if you specify the host as localhost, they alias that to a socket connection instead of a TCP connection. Your options are to either stick with or, if the client supports it (like the mysql CLI binary does with the --protocol flag), force it to use TCP instead of a unix socket.

  • Thanks EEAA. The problem is that things like the Joomla installer fail. If the you specify instead of localhost in the hostname field during installation, it causes installation paths to get messed up giving me other errors, but specifying localhost fails to connect the database! What's caused this situation? Why do I have it? Commented Oct 7, 2013 at 1:37
  • 3
    @PeterSnow Set up a Unix domain socket in your MySQL configuration. Commented Oct 7, 2013 at 1:38
  • Thanks @MichaelHampton I've searched for the last half hour and can't find how to set up this. Can you point me at an article or give me some idea please? I do have socket = /var/run/mysqld/mysqld.sock in my.cnf and the file is existing, owned by mysql with permissions 777. Thanks. Commented Oct 7, 2013 at 2:51
  • @PeterSnow Did you restart mysqld after making that change?
    – EEAA
    Commented Oct 7, 2013 at 3:13
  • Currently I'm trying to use it via the Joomla install script. The http page asks for the hostname. If I choose it works (but the script uses the entry later and fails because it expects resolvable name like localhost), but if I use localhost, connecting, to create the database, fails. Commented Oct 7, 2013 at 3:19

One thing to note if the grants are correct is if your using MariaDB there was a bug which caused this exact behaviour in version 10.0.1 and 5.5.30 https://mariadb.atlassian.net/browse/MDEV-4356


You can check permission like this :

[learner@localhost ~]$ sudo netstat -npa | grep mysqld  
tcp        0      0      *                   LISTEN      6284/mysqld         
unix  2      [ ACC ]     STREAM     LISTENING     233040 6284/mysqld         /var/lib/mysql/mysql.sock

If this is listening on 0* it means that every one can access that mysql.

you can edit binding for this in my.conf file. In linux default path for this is :

[learner@localhost ~]$ sudo vi /etc/my.cnf

Add these lines in my.conf file for allowing ip that you want.

bind-address =   
bind-address =
  • Thanks a_learner, for the information, although I have already accepted an answer. Commented Oct 8, 2013 at 0:45

PHP is still trying to use the default socket location. This problem can appear if you have moved the MariaDB/MySQL folder from /var/lib/mysql to another location. In order to solve the problem you have to define the new socket's location in the /etc/php.ini file.

mysqli.default_socket =/newDBLocation/mysql/mysql.sock

I solved this problem by creating symlink, sudo ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock. But first I have to create folder /var/run/mysqld/ because the folder didn't exist initially.


You need a mysql.sock in /tmp

ln -s /var/lib/mysql/mysql.sock mysql.sock

should work fine after that.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .