0

Here is the gist of my problem: I have a web application that won't connect when the database has the host is set as localhost, however it will if I use the address 127.0.0.1. The error that PHP gives me when connecting is: "SQLSTATE[HY000][2002] Permission denied". I know that the socket is there because when I stop the service I get "SQ:STATE[HY000][2002] No such file or directory."

After I type in "ls -als /tmp" in the terminal, I see that mysql.sock has the permissions of 777.

Any ideas why I cannot connect to the socket?

2 Answers 2

1

Does your mysql.user table have an entry for your database user for localhost host? If not, that is the reason for Permission denied error from MySQL.

4
  • Yes, it does have an entry for the user for localhost. Commented Apr 17, 2014 at 19:10
  • Have you checked PHP's configuration that it tries to use the socket in /tmp/mysql.sock? Can you connect to MariaDB using mysql -u user@localhost -p and entering the password? Commented Apr 17, 2014 at 19:13
  • I've manually edited the php.ini file to point to /tmp/mysql.sock. No, I cannot connect using mysql -u user@localhost -p. Commented Apr 17, 2014 at 19:22
  • I am thinking it may have something to do with SELinux on CentOS Commented Apr 17, 2014 at 23:19
1

On CentOS, the culprit is SELinux You can confirm by running the command

getsebool -a | grep httpd 

Check if httpd_can_network_connect_db is Off. If it is off you can enable it using the command

setsebool -P httpd_can_network_connect_db 1 

(The -P flag makes the change permanent, so the setting survives a reboot.)

Adapted from https://stackoverflow.com/a/41475776/6000657

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.