Can't connect to MySQL using 'localhost' but using '' it's ok?

Solution 1:

One thing you might check is (which requires you to login to the MySQL console) - check to make sure that you have permissions to login to root via localhost.

mysql -h -u root -p

-- Once you have successfully logged in --

mysql> select user,host from mysql.user;
| user | host                           |
| root |                      | 
| root | ::1                            |
| root | localhost                      | <-- Make sure you have a localhost entry for root
3 rows in set (0.00 sec)

Just throwing it out there, just in case this is what the issue is.

Solution 2:

Most MySQL clients are odd in the fact that if you specify the host as localhost, they alias that to a socket connection instead of a TCP connection. Your options are to either stick with or, if the client supports it (like the mysql CLI binary does with the --protocol flag), force it to use TCP instead of a unix socket.