I am trying to connect to the database using the following script (cxn-test.php)
When I try it on the terminal
php cxn-test.php //Success
But when I try it on localhost, I get the following error:
curl -s http://localhost/cxn-test.php
Error: Unable to connect to MySQL. Debug error number: 2002 Debug error message: Permission denied
This is a weird issue, it doesn't work on localhost but works fine on the command line.
I ran into the same problem after getting a new CentOS 7 box running SELinux. I can connect to the remote MySQL database server via the command line, but Drupal (and the test PHP script) cannot connect.
The problem is ultimately caused by the SELinux security policy.
By default, the policyhttpd_can_network_connect_dbis disabled (meaning your web servercannotcontact the remote database).
Check with the following command:
getsebool -a | grep httpd
If httpd_can_network_connect_db is Off, please enable it through the following command:
(The -P flag makes the changes permanent, so the settings remain in effect across reboots.)