Just a guess, but it sounds like
Your command line php executable uses a different php.ini
file than the web server
The php.ini
file your command line version uses has a different, and invalid, value set for the mysql.default_socket ini setting
When you specify a MySQL server host as localhost
, you're telling the system to use this socket file to connect to the database server. Check the value when using your local web-server, and make sure that same value is set for the command line program's ini
file.
You might also have some luck changing the server to 127.0.0.1
from localhost
. (When the system sees an IP address, it knows not to connect via the socket file, but instead to connect via TCP)