3

I'm receiving the error

Failed to connect to MySQL: Permission denied

When connecting from PHP with MySQL, MySQLi and PDO to a remote MySQL host.

However, if I connect to the same host, with the same credentials through MySQL from the same host on the command line it works perfectly.

So I'm making the assumption its a PHP config problem?

Connecting FROM

PHP 5.6.40 (Client API version => mysqlnd 5.0.11-dev)

MySQL 5.5.59

Connecting TO

PHP 5.6.23 (Client API version => mysqlnd 5.0.11-dev)

MySQL 5.5.52

Anyone got any ideas why I can connect from the command line to MySQL but not from PHP to the same host with the exact same credentials?

Dharman
  • 26,923
  • 21
  • 73
  • 125
Mr J
  • 2,345
  • 4
  • 32
  • 53

1 Answers1

9

I guess that you have SELinux enabled on your server. By default it doesn't allow apache process to initialize outgoing network connections.

Copy of an answer from https://serverfault.com/a/456875/442205

To check SELinux

sestatus

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
Naktibalda
  • 13,064
  • 5
  • 33
  • 47