Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

up vote 6 down vote accepted

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.

share|improve this answer
    
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. –  Peter Snow Oct 7 '13 at 3:54

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.

share|improve this answer
    
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? –  Peter Snow Oct 7 '13 at 1:37
2  
@PeterSnow Set up a Unix domain socket in your MySQL configuration. –  Michael Hampton Oct 7 '13 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. –  Peter Snow Oct 7 '13 at 2:51
    
@PeterSnow Did you restart mysqld after making that change? –  EEAA Oct 7 '13 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. –  Peter Snow Oct 7 '13 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

share|improve this answer

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

share|improve this answer
    
Thanks a_learner, for the information, although I have already accepted an answer. –  Peter Snow Oct 8 '13 at 0:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.