When you run a command in Drupal console, ie. drupal gm and encounter the error:

[ERROR] SQLSTATE[HY000] [2002] No such file or directory                                                                                                                                                                     

Solution: Visit the settings.php file of your site or your subsite {{ Drupal root }}/sites/{{sub-site or default }}/settings.php, change host: 'localhost' to 'host' => '127.0.0.1', then re-run Drupal console.

$databases['default']['default'] = array (
  'database' => 'drupal8',
  'username' => 'root',
  'password' => '',
  'prefix' => '',
  'host' => '127.0.0.1',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);