# WP CLI Error establishing a database connection

**URL:** <https://community.localwp.com/t/wp-cli-error-establishing-a-database-connection/31677>\
**Category:** Support\
**Tags:** wp-cli\
**Created:** [May 13, 2022, 5:45pm UTC](https://community.localwp.com/t/wp-cli-error-establishing-a-database-connection/31677 "2022-05-13T17:45:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrengy](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/mrengy/32/14655_2.png) [@mrengy](https://community.localwp.com/u/mrengy)\
**Post date:** [May 13, 2022, 5:45pm UTC](https://community.localwp.com/t/wp-cli-error-establishing-a-database-connection/31677/1 "2022-05-13T17:45:26Z")

</div>

### 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`](https://wordpress.stackexchange.com/questions/290793/wp-cli-error-establishing-a-database-connection-in-localhost-mamp) 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.

 ![Screen Shot 2022-05-13 at 1.39.51 PM](https://us1.discourse-cdn.com/flex019/uploads/lwpforums/original/2X/e/e9b107841bff5ea2cb0a2942e5f26ab161861e6b.jpeg)

#### System Details

- Which version of Local is being used?  
6.3.1+5826

- 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](https://community.localwp.com/uploads/short-url/3hIn8c7LQCezoFHdaK3PjpMAG9a.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.

---

<div class="post-metadata">

**Author:** ![Gray](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/gray/32/15122_2.png) [@Gray](https://community.localwp.com/u/Gray)\
**Post date:** [May 19, 2022, 11:30pm UTC](https://community.localwp.com/t/wp-cli-error-establishing-a-database-connection/31677/2 "2022-05-19T23:30:16Z")

</div>

I have also been experiencing this problem.

I tried the solution discussed at [WP-Cli : Error establishing a database connection - Amazingly simple solution](http://community.localwp.com/t/wp-cli-error-establishing-a-database-connection-amazingly-simple-solution/20794)

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:

```auto
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

```auto
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.

---

<div class="post-metadata">

**Author:** ![mrengy](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/mrengy/32/14655_2.png) [@mrengy](https://community.localwp.com/u/mrengy)\
**Post date:** [May 20, 2022, 1:53am UTC](https://community.localwp.com/t/wp-cli-error-establishing-a-database-connection/31677/3 "2022-05-20T01:53:13Z")

</div>

Thanks! The solution discussed at [WP-Cli : Error establishing a database connection - Amazingly simple solution](https://u4054288.ct.sendgrid.net/ls/click?upn=NsZFUs1OhiCxyWQ6q-2FwHTCu6Irapy4TeEiKQLy80itOyeafFBDH57KNuCAy-2BvFhlUxRx9ykrM6TtD6ivVY0yL50TNCw9-2F6-2F0BQyBhnCjyTFwlW60ai6TkyfNmSLaeO49ZF-2FqHZhQBbm0URFAMSPEngP8swzy2if-2FmQcgogx2cDs-3D09u2_mH-2B-2F3Fisoq9w5rfg0e4KmnGZHN0h8zZGZweaVLVC-2Ffg1YgZQeJOt4LkczQJ4y39TiWkuARG1qxudExZR-2FgV2inDo1QGnM-2BY7bHRaek4XJ96SVg2gaWFcy11BCF7-2FRzBLZINrOwEc48Z7eqdhHZ-2Fj3K9nMcwOgknopKxCcWzJLImyN09-2FqvfIhZ3cz-2BOdq70SNVOKFo1i7XJFujQXlYjEkQ-3D-3D) worked for me.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/system/32/1_2.png) [@system](https://community.localwp.com/u/system)\
**Post date:** [May 21, 2022, 1:53pm UTC](https://community.localwp.com/t/wp-cli-error-establishing-a-database-connection/31677/4 "2022-05-21T13:53:30Z")

</div>

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