WP-Cli : Error establishing a database connection - Amazingly simple solution

Hello,

On MacOs Catalina with Local 5.6.3+4270, inside the public folder, running wp-cli returns the following error:

% wp option get siteurl
Error: Error establishing a database connection.

I found no errors in Local’s logs.

Viewing the database in the browser with Adminer works.

Accessing to the database with Sequel Pro with the following parameters fails:

  • username, pwd, database & socket : exactly as they appear in Local’s database tab
    With Sequel Pro, the Test connection returns “connection succeeded”. However, after connecting to the db, a popup appears stating that SequelPro encountered an unexpected error error into the db.

I have reviewed most articles in the forum about WP-Cli problems with Local but found no clear solution.

Any help on that matter ?

Thanks !

2 Likes

Try changing your DB_HOST value in the wp-config.php file from localhost to localhost:/Users/YOUR_MACOS_USERNAME/Library/Application Support/Local/run/VALUE_FROM_PHP_INI/mysql/mysqld.sock.

  • YOUR_MACOS_USERNAME - whatever your username that you log into your computer with.
  • VALUE_FROM_PHP_INI - Click the (i) icon next to the version of PHP in Local. Once the php info page loads in your browser, search the page for “Loaded Configuration File”. You should see something like /Users/xxx/Library/Application Support/Local/run/GP1tVe1q8/conf/php/php.ini. Grab the random set of characters between “run/” and “/conf”. In my example, GP1tVe1q8.

This worked for me when I experienced the same problem.

6 Likes

Awesome @brandondove , that worked just perfectly !
(For the records, I directly copied the socket path from Local’s database tab)
Thank you and have a great week-end !
Antoine.

3 Likes

Happy to help.

Oh, ha! That was way smarter than how I told you to get it. :man_facepalming:

No, you gave the right solution by adding the socket to DB_HOST in wp-config.php, I just simplified the process :smiley_cat:

1 Like

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