Error establishing a database connection WordPress on Mac

The issue:

When we tried to setup WordPress in Mac we encountered issue related to database connection. Following is the issue:

Error establishing a database connection

In WordPress setup it is usually a common practice to use MySQL database on the same machine which is localhost. So the database host becomes the localhost.

The WordPress default setup comes with default wp-config.php there it has like this:

/** MySQL hostname */
define('DB_HOST', 'localhost');

The reason:

Mac OS can not determine the database host using localhost. It understands the localhost as the IP: 127.0.0.1

The solution:

We had to use the local IP instead of localhost like below:

/** MySQL hostname */
define('DB_HOST', '127.0.0.1');