Problem/Motivation
There are several cases when Drupal can get "2002 Connection refused" error:
1. The 'unix_socket' option is set in settings.php and the socket is not accessible.
SQLSTATE[HY000] [2002] No such file or directory
2. The 'host' option is empty or set to 'localhost', which means to use default Unix socket, but socket is not accessible.
SQLSTATE[HY000] [2002] No such file or directory
3. There is an issue with accessing the server via TCP/IP:
3.1. Host name could not be resolved:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not resolve
3.2. Host is not accessible.
3.3. Host is accessible, but connection on specified port refused:
SQLSTATE[HY000] [2002] Connection refused
3.4. I'm sure there are more cases.
Sometimes it takes significant time to discover the root cause of the error (especially for unexperienced developers). It would be great if Drupal provided more informative messages for MySQL error 2002. Perhaps with some tips how to resolve the issue.
Proposed resolution
The proposal is to introduce special exception class for MySQL 2002 error and provide additional information in error message.
Remaining tasks
Perhaps there will be a follow-up issues for pgsql and sqlite.