Can't connect to MySQL using 'localhost' but using '127.0.0.1' [SOLVED] | Web Hosting | Domain Registration | Wordpress Hosting | SSL Certificates

Can't connect to MySQL using 'localhost' but using '127.0.0.1' [SOLVED]

Sometimes it happens that MYSQL doesn't connect while using host = localhost while works fine while we connect it using 127.0.0.1. I fell into this issue and found the following solutions for this issue:

Opinion 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
Opinion 2
You need a mysql.sock in /tmp
ln -s /var/lib/mysql/mysql.sock mysql.sock
should work fine after that.
Opinion 3
For me, changing the permissions to be publically readable on the parent directory of the mysql.sock fixed the issue:
chmod 755 /var/lib/mysql
Solution:
Just add a line in /etc/my.cnf and restart mysql to let it work.
bind-address = localhost


and it started to work!!!!!!

SHARE