The Missing Socket File Behind HY000/2002
When you encounter the PHP warning mysqli_connect(): (HY000/2002): No such file or directory, it is almost never a problem with your username, password, or database name. Despite what the error message implies, the server itself exists; the specific issue lies in how MySQL on macOS exposes its network interface versus how PHP attempts to reach it.
In your specific scenario, you have successfully created the vanilla database and the vanilla_user3 user via the command line. The SQL commands executed without error, confirming that the server process is running and accepting connections. However, the moment you switch from the MySQL shell to PHP's mysqli_connect() function, the connection fails. This discrepancy points directly to a mismatch between the network protocol required by your application and the one currently available on your local machine.
The error code HY000/2002 specifically indicates that the socket file could not be found. On Linux systems, this usually means the Unix socket isn't in the expected /var/run/mysqld location or has incorrect permissions. On macOS, particularly when running MySQL via Homebrew (which is standard for development environments), the default configuration often hides the socket path or requires a specific path that doesn't align with PHP's default expectations.
To resolve this without changing your entire architecture, you have two primary paths: using TCP/IP sockets or correcting the Unix socket path.
Option 1: Switching to TCP/IP (The Easiest Fix)
The most robust solution for development on macOS is to force the connection to use a standard TCP/IP socket rather than trying to guess the local Unix socket path. When you connect via localhost using mysqli_connect(), PHP defaults to looking for a Unix socket file, often named /tmp/mysql.sock. However, Homebrew installations frequently place this file in /var/run/mysqld/mysqld.sock, or they may not create it by default if the configuration prefers TCP.
You can bypass this entirely by passing 127.0.0.1 as the host instead of "localhost". This forces PHP to initiate a standard TCP connection over port 3306, which is universally supported regardless of where the socket file resides.
Update your connection code as follows:
// Force TCP/IP connection by using the IP address
$con = mysqli_connect("127.0.0.1", "vanilla_user3", "vanilla_password", "vanilla");
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
If your my.cnf or my.ini file has the skip-networking option enabled, this will still fail. In that case, you must ensure network access is allowed by commenting out that line in your configuration file before restarting the MySQL service.
Option 2: Verifying and Pointing to the Socket File
If you prefer to stick with Unix sockets (useful for avoiding TCP overhead in high-concurrency scenarios), you must locate the actual socket file created by your MySQL instance. On macOS, this is often located at /var/run/mysqld/mysqld.sock.
You can verify its existence using the ls command:
ls -l /var/run/mysqld/mysqld.sock
If the file exists but you still get the "No such file or directory" error in PHP, it is almost certainly a permissions issue. The file might be owned by mysql (UID 25) while your PHP process runs as your user (UID 1000). You can fix this with:
sudo chown $(whoami):$(whoami) /var/run/mysqld/mysqld.sock
Alternatively, you can explicitly tell mysqli_connect() where to find the socket by passing it as the fourth argument in your connection string:
$socket_path = '/var/run/mysqld/mysqld.sock';
$con = mysqli_connect("localhost", "vanilla_user3", "vanilla_password", "vanilla", $socket_path);
Best Practices for Local Development
While mysqli_connect works, consider the trade-offs when building applications. Direct connection strings are prone to these exact configuration mismatches. For production-grade applications, especially those using modern PHP stacks, utilizing PDO with explicit DSN (Data Source Name) parameters offers better portability. You can define the socket path or host clearly within the DSN string, making your code resilient to environment changes without needing to edit multiple files.
Furthermore, if you are migrating from legacy projects or working on a team where mysqli is required, remember that mysqli_connect does not support persistent connections out of the box in the same way new mysqli() does. Always instantiate the connection object directly rather than using the procedural shortcut if you plan to manage resources manually or use prepared statements extensively.
By switching the host parameter to an IP address or explicitly defining the socket path, you align your PHP application's expectations with the actual file system layout of your macOS environment, eliminating the HY000/2002 error.
