Try to replace localhost to 127.0.0.1 and check
This is commonly caused by a different instance of PHP being used by the PHP command line compared to the web server. In particular, mysql.sock might be misconfigured.
Refer this magento devdocs link http://devdocs.magento.com/guides/v2.0/install-gde/trouble/php/tshoot_phpini.html
You can also refer - PDO Exception error on indexPDO Exception error on index
It will works