WP CLI Error establishing a database connection

Issue Summary

With my site in Local running when I run a WP CLI command like “wp plugin list”, I get “Error establishing a database connection” in the command line. I am able to access the site fine in a browser in this state.

In wp-config.php, I tried changing the DB_HOST to alternately and (originally it was localhost), but both gave me the “Error establishing a database connection” when trying to access the site in the browser.

Troubleshooting Questions

  • Does this happen for all sites in Local, or just one in particular?

It’s the only site I have in Local, so not sure.

  • Are you able to create a new, plain WordPress site in Local and access it in a Browser?



Here’s a screenshot of my local site setup. I had to use a site host of “localhost:10003” to get around some other issue.

System Details

  • Which version of Local is being used?

  • What Operating System (OS) and OS version is being used?
    Mac OS 11.6.5

  • Attach the Local Log. See this Help Doc for instructions on how to do so:

local-lightning.log (40.0 KB)

Security Reminder

Local does a pretty good job of scrubbing private info from the logs and the errors it produces, however there’s always the possibility that something private can come through. Because these are public forums, always review the screenshots you are sharing to make sure there isn’t private info like passwords being displayed.

I have also been experiencing this problem.

I tried the solution discussed at WP-Cli : Error establishing a database connection - Amazingly simple solution

but that didn’t do the trick for me.

My other problem that has been happening is that opening the terminal through “open site shell” in the GUI hasn’t been loading the proper environment, int that I get something like:

Last login: Thu May 19 16:22:15 on ttys011
You have mail.
/Users/Gray/Library/Application\ Support/Local/ssh-entry/Y6w0PGDjs.sh ; exit;

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
Grays-MacMini:~ Gray$

So I used a workaround of loading that .sh file directly, by copying ‘/Users/Gray/Library/Application\ Support/Local/ssh-entry/Y6w0PGDjs.sh’ directly into the command line, and running it.

That loads up my environment properly with

Grays-MacMini:~ Gray$ /Users/Gray/Library/Application\ Support/Local/ssh-entry/Y6w0PGDjs.sh
Setting Local environment variables...
WP-CLI:   WP-CLI 2.6.0
Composer: 2.1.5 2021-07-23
PHP:      7.4.1
MySQL:    mysql  Ver 8.0.16 for macos10.14 on x86_64 (MySQL Community Server - GPL)
Launching shell: /bin/zsh ...
➜  public git:(live) ✗ 

and then I can run commands like wp plugin list for testing.

make sure you don’t have any wp-cli.local.php or wp-cli.local.yml files in the root.

Maybe that’ll work for you too.

Thanks! The solution discussed at WP-Cli : Error establishing a database connection - Amazingly simple solution worked for me.

1 Like

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.