13

My /etc/hosts file looks like this:

127.0.0.1 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 127.0.0.1 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

16

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 127.0.0.1 -u root -p

-- Once you have successfully logged in --

mysql> select user,host from mysql.user;
+------+--------------------------------+
| user | host                           |
+------+--------------------------------+
| root | 127.0.0.1                      | 
| 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.

3
  • 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 127.0.0.1 (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. 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
    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). Mar 31, 2019 at 15:21
9

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 127.0.0.1 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.

8
  • Thanks EEAA. The problem is that things like the Joomla installer fail. If the you specify 127.0.0.1 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? Oct 7, 2013 at 1:37
  • 3
    @PeterSnow Set up a Unix domain socket in your MySQL configuration. 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. Oct 7, 2013 at 2:51
  • @PeterSnow Did you restart mysqld after making that change?
    – EEAA
    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 127.0.0.1 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. Oct 7, 2013 at 3:19
1

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

1

You can check permission like this :

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

If this is listening on 0 0.0.0.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 = 127.0.0.1   
bind-address = 0.0.0.0
1
  • Thanks a_learner, for the information, although I have already accepted an answer. Oct 8, 2013 at 0:45
0

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
0

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.

-1

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 .