I had the same issue after getting a new CentOS 7 box, running SELinux. I could connect to my remote MySQL DB server from the command line, but Drupal (and test PHP scripts) could not.
The issue turned out to be the SELinux security policies.
By default, the policy httpd_can_network_connect_db is disabled (meaning that your web server cannot contact a remote DB.)
Check this via:
getsebool -a | grep httpd
If httpd_can_network_connect_db is Off, enable it via:
setsebool -P httpd_can_network_connect_db 1
(The -P flag makes the change permanent, so the setting survives a reboot.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…