Connect MySQL through localhost not working but 127.0.0.1 is working

淺唱寂寞╮ 提交于 2019-12-01 15:52:40

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

Watch out, depending on which driver you use, you may have to specify the pdo_mysql.default_socket=!

In order to check your current directory run the following command in mysql:

select @@datadir;

In many cases localhost is a hint to use the UNIX socket to connect to the MySQL server process, where 127.0.0.1 forces a TCP connection. Sometimes the UNIX socket is in an unexpected place, or simply isn't accessible, which results in the "file not found" error.

The socket is typically something like /tmp/mysql.sock or could be in some other place depending on your distribution and how much you've customized it.

Keep in mind that access via UNIX socket, which is local to the server by definition, and TCP are controlled by two different rules. localhost in a GRANT refers to UNIX socket. % or some specific host name refers to remote via TCP.

The answer to this problem was that in the my.cnf located within

/etc/mysql/my.cnf

the line was either

bind-address = 127.0.0.1 

or

bind-address = localhost 

it should have been

bind-address = 0.0.0.0

which will allow all connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!