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 127.0.0.1 and 127.0.0.1:8889 (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?
Yes.
Replication
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.
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.
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.