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.