use 127.0.0.1 instead of localhost.
to quote the mysql documentation:
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs: the client connects using a Unix socket file. The --socket option or the MYSQL_UNIX_PORT environment variable may be used to specify the socket name.
unix sockets are faster which is why it tries to use them, but there are circumstances where mysql cannot reach find a socket. most common one is if it's containerized.
6.2.4 Connecting to the MySQL Server Using Command Options - MySQL Documentation