WP CLI : Error establishing a database connection in LocalWP

If you are using localwp for your local WordPress development then you might be face this database error issue at first time in WP CLI connection. Same things happen to me. Thats why I am helping you out. Here is the simple solution.

You need to replace your DB_HOST from your wp-config.php file like below.

Old
-----
localhost

New
-----
localhost:/Users/YOUR_MACOS_USERNAME/Library/Application Support/Local/run/VALUE_FROM_PHP_INI/mysql/mysqld.sock.

YOUR_MACOS_USERNAME – whatever your mac username

VALUE_FROM_PHP_INI – click the ( i ) icon next to the php version of you localwp app. It will open your phpinfo in the tab. Now search for Loaded Configuration File in that page. you should see something like below.

Users/YOUR_MACOS_USERNAME/Library/Application Support/Local/run/GP1tVe1q8/conf/php/php.ini

you need to copy the code between run and conf and place there.

Now you can connect your database with wp cli.

Run the below code from your terminal.

wp option get siteurl

If everything goes good, you will get your siteurl in your terminal.