DB connection works from the CLI, but fails from within Drupal I have set up a MySQL server on a machine with IP-address 192.0.2.9 (not the real address). The database user is named "drupal". Connecting to it using the CLI mysql command works just fine and the connection is accepted.

mysql -u drupal -p -h 192.0.2.9
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
...


However, when Drupal tries to connect to the same MySQL server from the same host, using the same credentials, I get this error:

PDOException: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.0.2.9' (9)
in lock_may_be_available() (line 167 of /var/www/html/includes/lock.inc).


I have double checked that the correct database credentials and host are set in settings.php. I am at my wit's end, unable to figure out why this database connection works from the CLI, but not from within Drupal.

In case it matters: This is Drupal 7.41 and the OS on both machines is RHEL7.
Figured it out!

The client computer was running SELinux and was blocking outgoing connections over http. This pair of CLI command fixed it.

setsebool httpd_can_network_connect 1
setsebool -P httpd_can_network_connect 1