If you try to connect to MAMP’s MySQL database server from PHP’s command line interface (CLI) and the connection is failing it may be because you need to connect to the MySQL server via a socket. This is the default behaviour with a MAMP installation. Sockets are file-based and limit the connection to those from your own machine. On the other hand, specifiying a host and port — as you likely do on your production servers — allows for network connections.
To connect via a socket you need to omit the port number when calling mysqli_connect
and specify the path to the socket file. Here’s an example:
$db = mysqli_connect('HOST_GOES_HERE',
'USER_GOESHERE',
'PASSWORD_GOES_HERE',
'DATABASE_GOES_HERE',
null, // No port
'/Applications/MAMP/tmp/mysql/mysql.sock'); // MAMP socket
