Symfony console connection refused with MAMP

Recently I switched from a custom system setup on my Mac (Apache, MySQL, PHP, ...) to the MAMP Pro system. Unfortunately the Symfony console won't work with the default configuration in parameters.yml like the following:

# Database parameters
database_driver:   pdo_mysql
database_host:     127.0.0.1
database_port:     ~
database_name:     database
database_user:     myuser
database_password: password

The result is a refused connection. But why?

Turns out:

MAMP by default doesn't allow TCP connections.

According to this answer on Stackoverflow, the easiest solution is to just use localhost instead of 127.0.0.1 as database_host.

The longer explanation is:

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. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option.