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.
| Comment | File | Size | Author |
|---|---|---|---|
| #122 | reroll_diff.txt | 3.06 KB | ravi.shankar |
| #122 | 2610858-122.patch | 9.16 KB | ravi.shankar |
| #116 | 2023-03-15 13_26_33-Hyper.png | 10.63 KB | grevil |
| #116 | 2023-03-15 13_25_42-Hyper.png | 10.91 KB | grevil |
| #116 | 2023-03-15 13_24_42-Hyper.png | 15.59 KB | grevil |
Issue fork drupal-2610858
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 2610858-connection-refused-add-informative-error
changes, plain diff MR !3477
Comments
Comment #2
asghar commentedHi
I have configured the fresh Drupal-8.0.0-rc3 and Drush-8.0.0-rc3 versions. Run the multi commands and your mentioned commands but could not produce the errors.
Comment #3
asghar commentedSorry I forgot to mention OS and php version:
OS X EI Capitan
PHP 5.6.10 (cli) (built: Jun 12 2015 10:07:24)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
Comment #4
aspilicious commentedSame problem on D8 head
Comment #5
dawehnerIsn't that what happens if you have a broken settings.php with wrong credentials?
Comment #6
kostajh commented@dawehner that's right, but an easier to grok error message would be oh-so-welcome! I spent 30 minutes trying to figure this out in setting up a site on a CI environment. Turns out I had to set `127.0.0.1` instead of `localhost` in the MySQL host setting.
Comment #7
dawehner@kostajh
Interesting I have seen this in some real world as well. Did we something broke about that?
Yeah I have no idea why the PDO exception isn't that helpful.
Comment #8
rgracia commentedThanks for your replies guys. Here's a bit more info. The first try I created a settings.php myself as I was unaware the installation script now creates one if needed. So trashed the install and started again letting D8 generate it. Here is the output of said settings.php with comments omitted for brevity:
Again, setup El Cap 10.11.2 Beta (15C27e), MAMP 3.4, PHP 5.6.10, Drupal 8.0.0-rc3 and Drush 8.0.0-rc3.
UPDATE: I am getting same results with Drupal 8.0.0-rc1, Drupal 8.0.0-rc2 as well.
Comment #9
aspilicious commentedyes fresh installatie works
Comment #10
subson commentedChanging your localhost to 127.0.0.1 works for me.
Comment #11
cameron tod commentedIf you found this page from a Google search, double check that your database credentials are correct in settings.php or settings.local.php.
In my case, everything worked fine in drush, but I was getting a "The website encountered an unexpected error. Please try again later." via Apache/PHP under MAMP.
I use a custom settings.local.php, and as it turns out, I have env vars setup in my cli but not in Apache.
Logged errors looked like this:
Maybe #1409108: Improve "Failed to connect to database" Error message is a good issue to use to clean up error output?
Comment #12
cameron tod commentedComment #13
cameron tod commentedComment #14
cameron tod commentedComment #15
cameron tod commentedComment #16
bechtold commentedI just arrived here as well using google.
My eror message:
On
OSX 10.10.5
PHP 5.6.10
with a MAMP Pro setup.
The problem for me was the
'host' => 'localhost',.Replacing it with
'host' => '127.0.0.1',resolved the issue.
Took me a while to figure this out since the error message didn't help :-)
Comment #17
cilefen commentedComment #18
kostajh commentedHere's a patch that changes the example from `localhost` to `127.0.0.1`.
Comment #19
bechtold commentedI think this issue is asking about better error message and the patch is not solving this.
Also I got this error on a d7 install today that worked fine before #dcvie.
But only in drush, the page itself did work fine.
So maybe updating drush (which i did on the weekend) introduced this bug for me.
Comment #20
dawehnerYeah this would be indeed way more helpful, even its also odd that localhost is no longer possible to use.
Comment #21
jorgediazhav commentedFor MAMP, add this:
It worked for me.
Best!!!
Comment #22
rgracia commentedThank you all. jorgediazhav, your suggestion fixed it for me. Awesome!
Comment #23
cilefen commentedRe #20 it is probably because:
" A Unix socket file is used if you do not specify a host name or if you specify the special host name localhost."
http://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html
Comment #24
cilefen commentedThis should be an improvement. The problem with the confusing error is:
This patch catches the exception earlier and rethrows a better exception in the correct place. Example:
Access denied:
Socket not found (with 'localhost' config instead of '127.0.0.1', which means the server is not running or no socket exists-which could be various reasons):
Comment #25
dawehnerIMHO We should explain why we are doing that here.
Comment #26
cilefen commented@dawehner and I found we could wrap the offending call_user_func_array() and deal with all underlying exceptions. The stack traces look the same as in #24.
Comment #27
dawehnerthat code certainly breaks, it should be throw $e
Comment #28
cilefen commented@dawehner Huh-Did you try that? It results in the same situation as without any patch.
Comment #29
cilefen commentedI agree how #26 should not work though. It must be a toString happening.
Comment #30
cilefen commentedStuffing a stack trace into an exception message won't work.
Comment #31
cilefen commentedAnother way to do this, and for all database drivers, is to catch the exception where it occurs and rethrow an exception that won't be caught as a PDOException higher up.
Comment #33
artfrick commented[Sun Jan 31 12:28:30.844457 2016] [fcgid:warn] [pid 11232:tid 920] [client 127.0.0.1:63683] mod_fcgid: stderr: Drupal\\Core\\Database\\DatabaseConnectionException: SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'drupal8x' in C:\\Users\\alj456\\Sites\\devdesktop\\drupal8x\\core\\lib\\Drupal\\Core\\Database\\Driver\\mysql\\Connection.php on line 150This is now the exception with the patch from #31 applied. Is this better? Personally, I think it's helpful to have the actual line in the file, however, we've managed to lose the fact that it's a PDO exception.
Comment #34
cilefen commentedThis version provides some help when people have used "localhost" for the database connection, and the socket does not exist, and the confusing error that results.
Next, as for this not-very-confusing error in the issue summary:
PDOException: SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'drupal' in /core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php on line 83I think that is quite clear and doesn't require fixing.
Comment #35
dawehnerIt would be nice to document what this issue changes, so its clear that this improves the situation.
Comment #36
artfrick commentedComment #38
cilefen commentedThis is a lightweight approach to the specific error.
Comment #39
cilefen commentedComment #41
wooody commentedThanks jorgediazhav #21 works with me.
Comment #42
aangel commented@jorgediazhav Your suggestion worked for me, too.
Comment #44
nottaken commentedThanks @jorgediazhav. #21 worked for me too.
Comment #46
dawehnerShould we expand it to telling people that your mysql server might simply not be started in the first place, like after a reboot?
Comment #47
daffie commentedThe issue #728702: Visiting index.php should redirect to install.php if settings.php already has database credentials but database is empty. has landed.
Comment #48
xiwar commentedHi, i'm Matt Lambert from Redweb. I'm going to re-roll this patch.
Comment #49
xiwar commentedI could apply the patch, so no reroll is needed.
Comment #50
xiwar commentedComment #51
daffie commentedHi Matt, my mistake. #728702: Visiting index.php should redirect to install.php if settings.php already has database credentials but database is empty. has been applied to Drupal 8.3 and so should the patch from this issue. Can you try again?
Comment #52
cameron tod commentedI've moved this into a custom exception now to maintain the pattern in 8.3.x.
Comment #53
cameron tod commentedGah, forgot a return statement.
Comment #55
daffie commentedThe patch looks good. I do have a problem with it:
The problem that I have with this is that other databases can have the same problem and this message is specific for MySQL.
I was wondering if we also should add the same for PostgreSQL. The error code for a refused connection on PostgreSQL is 10061. What do you think @Cameron Tod?
Comment #56
cameron tod commentedThe last patch had a fatal error in it - that's what I get for using vim and not PHPStorm and not testing.
I've also added PostgreSQL handling. I don't know a lot about PostgreSQL so this is based on some educated guess work. If the tests pass I think its worth the PostgreSQL maintainers taking a look.
Comment #57
mradcliffeIt is very hard for me to trigger this test as a vary narrow use case of ipv6/ipv4 connection issues.
If I try an invalid host on the box, I get 08006.
I think the doc block in Drupal\Core\Database\Database::openConnection should be updated with
@throws \Drupal\Core\Database\DatabaseConnectionRefusedException.Thinking about this some more... I am not sure if PostgreSQL logic is solving the same problem as MySQL for the original issue.
1. In MySQL, localhost a special hostname to use the local socket, but PostgreSQL will connect to a local socket only if host does not exist (not currently supported by pgsql driver). We are allowing socket connections for both MySQL and PostgreSQL on the test bots so it's not possible to test non-existent socket.
2. I can write a kernel test to test 08006 though by adding a connection info with a non-existent host.
I think that we should remove the handling in pgsql driver, and then change the exception class to by MySQL specific and specifically-named after localhost socket connecting.
Maybe a follow-up issue about handling connection errors better? Or if this issue is supposed to handle catching any connection refusal, than I think that the error message needs to be a bit more specific per driver as 10061 is a narrow use case and 08006 are better for handling general connection failure.
Comment #58
cameron tod commentedI agree with the sentiment in #57. I think we should pull this issue back to just handling the MySQL issue, then maybe a follow up for the PostgreSQL, or perhaps a broader issue about handling connection errors better more generally.
Comment #59
awelch commentedCan I add I had this problem attempting to use drush using MAMP PRO, PHP 5.5.10.
GUI of site worked fine with just
but drush threw:
exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in core/lib/Drupal/Core/Database/Driver/mysql/Connection.php:146
Changing to
Fixed it
as per
https://modulesunraveled.com/blog/drush-not-working-mamp-heres-how-fix-it
Comment #60
cilefen commentedYes, see #2610858-23: Add informative error message for 'Connection refused' errors in MySQL. Drush is sending back exactly what the mysql client library reported.
Comment #63
snlnz commentedMAMP PRO: 4.2 (9213)
PHP: 7.1.6
Drush: 8.0.5
Drupal: 8.2.1
#15 fixed the issue.
Comment #64
mradcliffeI think there is consensus for #57, and this should offer some tasks for several contributors to rework the patch based on the comments in #57. First, update the issue summary to reflect the state of the issue. And a kernel or phpunit test could also be written for the new exception.
Comment #67
joachim commentedComment #69
vacho commentedPatch rerolled. For this useful message.
Comment #70
asvira commentedI updated issue summary and removed code related to pqsql and made exception class specific to MySQL.
Also, I customized error message depending on the reasons caused the issue.
Now error messages are:
1. The 'unix_socket' option is set in settings.php and the socket is not accessible.
2. The 'host' option is empty or set to 'localhost', which means to use default Unix socket, but socket is not accessible.
3. There is an issue with accessing the server via TCP/IP:
Comment #71
asvira commented#1409108: Improve "Failed to connect to database" Error message (https://www.drupal.org/project/drupal/issues/1409108) might be considered as resolved in the scope of current issue and we could close it once the current patch is committed. It will show a tip about incorrect Unix socket during installation if the socket is used and connection failed with error code 2002.
This is an example of how it looks (text only, without markup):
Comment #72
joachim commentedGreat improvement!
Comment #74
larowlanNo longer applies
nit: can we throw early here and avoid the elseif/else?
nit Use *an* IP address
this doesn't seem to be used?
Comment #75
daffie commentedComment #76
ravi.shankar commentedHere is re-roll of patch #70
Comment #77
daffie commented@ravi.shankar: Could you address the 3 points from @larowlan in comment #74?
Comment #78
ravi.shankar commentedHere I have addressed point no. 2 and 3 of @larowlan comment #74. @daffie could you please suggest how can I address point 1 of comment #74? I don't think so if we can avoid those if else.
Comment #79
daffie commented@ravi.shankar: The changes you made for #74.2 and #74.3 are correct. And thank you for working on this!
The change that @larowlan wants for #74.1 is the following:
My suggestion is also to create a switch statement for the $e->getCode() equals part. I think this will make the part the most readable:
Comment #80
daffie commentedWe also need to write some tests for the code that we add. My suggestion is to take Drupal\FunctionalTests\Bootstrap\UncaughtExceptionTest::testLostDatabaseConnection() as an example. See: https://github.com/drupal/core/blob/8.8.x/tests/Drupal/FunctionalTests/B....
Comment #81
Madhura BK commentedComment #82
Madhura BK commentedHave added the code as suggested in #79.
Comment #83
daffie commentedAlmost there. 2 minor nitpicks left:
Added spaces where their not supposed to be.
This line is not correctly alligned.
Comment #84
ravi.shankar commented@daffie Here I have made changes that mentioned in #83.
Comment #85
daffie commentedThese 4 lines are inset too much.
Now we still need some tests. See comment #80.
Comment #86
Madhura BK commentedComment #87
shubham.prakash commentedFixed the indentation issue, still need tests.
Comment #88
shubham.prakash commentedFixed the indentation issue, still need tests.
Comment #89
daffie commented@shubham.prakash: Could you also post an interdiff.txt file. Such a file makes it easier for reviewers to see what you have changed in your new patch file. See: https://www.drupal.org/documentation/git/interdiff.
Comment #90
shubham.prakash commentedForgot to provide interdiff, here it is.
Comment #91
daffie commented@shubham.prakash: Thank you for fixing the indentation. Would you be interested in coding the tests. See comment #80.
@asvira did the IS update, so removing the tag
Comment #92
alvarito75 commented#21 worked for me.
Comment #93
chankongching commented#21 worked for me too, with only adding
'unix_socket' => '/var/lib/mysql/mysql.sock',and host is remain unchanged. i.e. localhost
Comment #94
ressaThe patch in #88 works fine with Drupal 9, providing more details, if the connections is refused. It does seem like the same message is shown twice ... Now I just need to figure out the reason for the error, but that's another issue :-)
Currently
With patch
(removed the duplicate error message)
Comment #96
sharma.amitt16 commentedComment #97
sharma.amitt16 commentedComment #98
sharma.amitt16 commentedPatch #88 looks good to me. I have applied the patch to D9.1.x
Before patch applies I got below error message.
After patch apply I got below
It looks good to have more information about the database connection error.
Thanks for the patch
Comment #99
daffie commentedThe patch still automatic tests.
Comment #100
daffie commentedComment #101
anybodyJust ran into this problem and think this is still relevant. For normal users it's hard to understand what happened, for example if the database can not be connected.
Comment #103
mohit_aghera commented- Add the test cases to validate errors.
- Re-roll the patch, as patch wasn't applicable to 9.2.x
Comment #104
kapilv commentedComment #105
renatog commentedOn this case the expression is redudant
Error code for "Connection refused" error.We can use it:
It's easier and clear
Comment #106
ravi.shankar commentedAddressed comment #105.
Comment #107
tyler36 commentedIs there a valid reason why the patch $message text is not translatable?
Comment #112
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #114
joachim commentedMade a MR on 10.1.
Comment #115
andypostComment #116
grevil commentedLGTM!
The patch applies successfully to 10.1.x and works as expected!
Message for invalid "localhost" or empty host option:

Message for incorrect "unix_socket" entry:

Message for general invalid host option:

RTBC!
Comment #117
grevil commentedAlthough, I am unsure about the unix socket mentioning inside the first screenshot, for the "if (isset($connection_options['host']) && in_array(strtolower($connection_options['host']), ['', 'localhost'], TRUE))"Case. Maybe someone with more insight could have a look.
Edit: Note to me, I should read the message properly first...
Comment #118
larowlanAdding issue credits
Comment #121
larowlanCommitted to 10.1.x and 10.0.x, it doesn't apply cleanly to 9.5.x so marking for backport.
Thanks folks
Comment #122
ravi.shankar commentedAdded a patch for Drupal 9.5.x, and also added reroll diff between MR and the new patch, please review.
Comment #123
catchThere's a fair amount going on here and we're approaching the final bugfix release for 9.5.x, so I think we should leave this fixed in 10.0.x.
Comment #124
anybodyThanks @catch!
Removing "[Needs backport]" from the title so.