1

Symptoms:

  • I am running CentOS 6.
  • I can connect to the remote MySQL from the command line: mysql -h a.b.c.d -u user -p -D db.
  • Laravel artisan commands run on the local command line can also connect: artisan migrate.
  • But the same Laravel code when running through Apache cannot connect.
  • The error is: SQLSTATE[HY000] [2003] Can’t connect to MySQL server on ‘a.b.c.d’ (13)
  • The MySQL help on reasons for "access denied" didn't help.

What am I doing wrong? (I am self answering for posterity.)

2 Answers 2

1

To see what flags are set on httpd processes

getsebool -a | grep httpd

To allow Apache to connect to remote database through SELinux

setsebool httpd_can_network_connect_db 1

Use -P option makes the change permanent. Without this option, the boolean would be reset to 0 at reboot.

setsebool -P httpd_can_network_connect_db 1
2
  • What's the point of duplicating the answer for such an old question? Answer from bishop seems to be higher quality as it contains more explanation and not just list of commands.
    – rvs
    Aug 10, 2021 at 16:46
  • This answer adds new information, specifically the allow apache to talk to a database information which Bishop's answer lacks.
    – sysadmin1138
    Aug 10, 2021 at 23:05
0

The key to this problem is the (13), which means "Permission Denied" (via perror). Because I can connect from the command line, the permission must be in the web app. Since I know that the credentials in the web app match the ones on the command line, the only thing left is the web server.

Via an strace, I discovered SELinux was blocking it. To fix:

sudo setsebool -P httpd_can_network_connect=1

This permanently sets SELinux to allow network connections out of httpd.

1
  • You can solve the related problem of Apache not sending emails, while the command line can, with: setsebool -P httpd_can_sendmail=1
    – bishop
    Nov 18, 2014 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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