Solve no such file or directory problem in WP CLI

We all are used to update all plugins and themes from the admin panel of our WordPress site. It’s so easy. Enter to your dashboard, open plugin page and update individual plugin.

Update theme or plugin from WP CLI is also so easy. But at first time, most of the dev face some issue to connect WP CLI with Mysql. Here is the problem what I face during my first try after installing the WP CLI in my local machine.

PHP Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /Applications/MAMP/htdocs/plugin/wp-includes/wp-db.php on line 1653
Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /Applications/MAMP/htdocs/plugin/wp-includes/wp-db.php on line 1653
Error: No such file or directory
Error establishing a database connection
This either means that the username and password information in your wp-config.php file is incorrect or we can’t contact the database server at localhost. This could mean your host’s database server is down.

Are you sure you have the correct username and password?
Are you sure you have typed the correct hostname?
Are you sure the database server is running?

If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums. No such file or directory

Solution

This problem occurs for the DB_HOST value, what is defined in wp-config.php file. To fix this problem, you need to add 127.0.0.1 as a value of DB_HOST instead of localhost.

Now WP CLI will run smoothly.