← 返回博客

Fix PHP MySQL Error 2002 on Mac: localhost vs. 127.0.0.1


If a local PHP app on your Mac reports SQLSTATE[HY000] [2002] No such file or directory but your database client connects, check how PHP is reaching MySQL before changing the password or reinstalling anything. With PDO MySQL on Unix, localhost uses a Unix socket, whereas 127.0.0.1 uses TCP. They can reach the same server, but only when both connection paths are configured correctly.

1. Find out which connection already works

Open your working database profile and note its connection type, host and port, or its socket path if it uses one. For Devio, use the address and port shown for the running MySQL instance. Do not assume the port is 3306 or copy credentials from a different project. If you need a second check, follow the Devio DBX MySQL connection guide (Chinese, with an English summary).

If you expect a TCP connection on port 3306, run this read-only check in Terminal. Replace 3306 with the actual port if it differs:

lsof -nP -iTCP:3306 -sTCP:LISTEN

A listener tells you that a process is accepting TCP connections on a local address. It does not tell you that the listener belongs to the MySQL instance you intended, or that your account can log in. No output means no matching listener was shown to your current user; check the service status, configured port and bind address before drawing a conclusion. If a different service owns the port, leave it running and use the Devio port-conflict guide (Chinese) to identify it.

2. Make PHP use the same transport

If DBX or your other client is using TCP to connect to MySQL on the same Mac, set your PHP app’s database host to 127.0.0.1 and its port to the same value. This is an example PDO DSN, not a command to run:

mysql:host=127.0.0.1;port=3306;dbname=your_local_database;charset=utf8mb4

Replace the example port and database name with yours. Keep the username and password in the project’s existing local configuration; do not put them in a public PHP file. Back up that local configuration before editing, and reload it using your project’s usual procedure. Retry from the same PHP runtime that serves the failing app: a Terminal PHP command and a browser request can use different configurations.

If your project intentionally uses a Unix socket instead, keep the socket transport and point PDO to the exact path supplied by the running MySQL instance. Do not combine unix_socket with host and port:

mysql:unix_socket=/path/to/mysql.sock;dbname=your_local_database;charset=utf8mb4

Do not switch to 127.0.0.1 for a socket-only server, or when PHP is running in a separate container or virtual machine. In those cases, loopback may not reach the intended server.

3. Verify the server, not just the login

Once PHP connects, run the following read-only query from your app’s existing database console or test harness. Run it in DBX too if you need to compare the two connections:

SELECT @@hostname AS server_host,
       @@port AS server_port,
       DATABASE() AS selected_database;

Compare these values with the intended instance and schema. A mismatch is a useful clue, but a match is not proof that both clients reached the same instance. DATABASE() is NULL if no schema is selected. The port shown is the server’s configured TCP port, not the transport this connection used.

What the next error tells you

  • Still “No such file or directory”: check whether the running app still reads localhost or an old socket path.
  • “Connection refused”: check the address and port in the actual app configuration against the listener; changing the password won’t repair an unreachable endpoint.
  • “Access denied”: a MySQL server answered. Check that it is the intended server, then check the account and permissions rather than changing the port blindly.
  • “Could not find driver”: check that the app’s PHP runtime loads pdo_mysql. If this is a local-only site, use the Devio PHP execution check (Chinese) and add the line below to its temporary test file. Open it through the same local site, then delete it. A result of pdo_mysql: missing means changing the DSN will not fix the missing driver.
echo extension_loaded('pdo_mysql') ? "pdo_mysql: loaded\n" : "pdo_mysql: missing\n";

If the change did not help, restore the saved configuration. None of these checks require stopping an unrelated process, changing database privileges, or deleting MySQL files.


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注