{"id":330,"date":"2026-12-05T16:14:22","date_gmt":"2026-12-05T08:14:22","guid":{"rendered":"https:\/\/getdevioapp.xyz\/?p=330"},"modified":"2026-12-05T16:14:25","modified_gmt":"2026-12-05T08:14:25","slug":"php-mysql-error-2002-mac","status":"publish","type":"post","link":"https:\/\/getdevioapp.xyz\/index.php\/2026\/12\/05\/php-mysql-error-2002-mac\/","title":{"rendered":"Fix PHP MySQL Error 2002 on Mac: localhost vs. 127.0.0.1"},"content":{"rendered":"<div lang=\"en\">\n<p>If a local PHP app on your Mac reports <code>SQLSTATE[HY000] [2002] No such file or directory<\/code> but your database client connects, check how PHP is reaching MySQL before changing the password or reinstalling anything. With PDO MySQL on Unix, <code>localhost<\/code> uses a Unix socket, whereas <code>127.0.0.1<\/code> uses TCP. They can reach the same server, but only when both connection paths are configured correctly.<\/p>\n<h2>1. Find out which connection already works<\/h2>\n<p>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 <a href=\"https:\/\/getdevioapp.xyz\/index.php\/2026\/11\/11\/devio-mysql-connection-charset\/#mysql-english-summary\">Devio DBX MySQL connection guide (Chinese, with an English summary)<\/a>.<\/p>\n<p>If you expect a TCP connection on port 3306, run this read-only check in Terminal. Replace <code>3306<\/code> with the actual port if it differs:<\/p>\n<pre><code>lsof -nP -iTCP:3306 -sTCP:LISTEN<\/code><\/pre>\n<p>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 <a href=\"https:\/\/getdevioapp.xyz\/index.php\/2026\/11\/11\/devio-port-conflict-troubleshooting\/\">Devio port-conflict guide (Chinese)<\/a> to identify it.<\/p>\n<h2>2. Make PHP use the same transport<\/h2>\n<p>If DBX or your other client is using TCP to connect to MySQL on the same Mac, set your PHP app\u2019s database host to <code>127.0.0.1<\/code> and its port to the same value. This is an example PDO DSN, not a command to run:<\/p>\n<pre><code>mysql:host=127.0.0.1;port=3306;dbname=your_local_database;charset=utf8mb4<\/code><\/pre>\n<p>Replace the example port and database name with yours. Keep the username and password in the project\u2019s 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\u2019s 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.<\/p>\n<p>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 <code>unix_socket<\/code> with <code>host<\/code> and <code>port<\/code>:<\/p>\n<pre><code>mysql:unix_socket=\/path\/to\/mysql.sock;dbname=your_local_database;charset=utf8mb4<\/code><\/pre>\n<p>Do not switch to <code>127.0.0.1<\/code> 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.<\/p>\n<h2>3. Verify the server, not just the login<\/h2>\n<p>Once PHP connects, run the following read-only query from your app\u2019s existing database console or test harness. Run it in DBX too if you need to compare the two connections:<\/p>\n<pre><code>SELECT @@hostname AS server_host,\n       @@port AS server_port,\n       DATABASE() AS selected_database;<\/code><\/pre>\n<p>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. <code>DATABASE()<\/code> is <code>NULL<\/code> if no schema is selected. The port shown is the server\u2019s configured TCP port, not the transport this connection used.<\/p>\n<h2>What the next error tells you<\/h2>\n<ul>\n<li><strong>Still \u201cNo such file or directory\u201d:<\/strong> check whether the running app still reads <code>localhost<\/code> or an old socket path.<\/li>\n<li><strong>\u201cConnection refused\u201d:<\/strong> check the address and port in the actual app configuration against the listener; changing the password won\u2019t repair an unreachable endpoint.<\/li>\n<li><strong>\u201cAccess denied\u201d:<\/strong> a MySQL server answered. Check that it is the intended server, then check the account and permissions rather than changing the port blindly.<\/li>\n<li><strong>\u201cCould not find driver\u201d:<\/strong> check that the app\u2019s PHP runtime loads <code>pdo_mysql<\/code>. If this is a local-only site, use the <a href=\"https:\/\/getdevioapp.xyz\/index.php\/2026\/11\/19\/apple-silicon-mac-php-local-check\/\">Devio PHP execution check (Chinese)<\/a> and add the line below to its temporary test file. Open it through the same local site, then delete it. A result of <code>pdo_mysql: missing<\/code> means changing the DSN will not fix the missing driver.<\/li>\n<\/ul>\n<pre><code>echo extension_loaded('pdo_mysql') ? \"pdo_mysql: loaded\\n\" : \"pdo_mysql: missing\\n\";<\/code><\/pre>\n<p>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.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Fix PHP MySQL Error 2002 on Mac. Check the MySQL listener, choose the right PDO connection, and compare the server and schema reported by PHP and DBX.<\/p>\n","protected":false},"author":1,"featured_media":201,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-330","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-troubleshooting"],"_links":{"self":[{"href":"https:\/\/getdevioapp.xyz\/index.php\/wp-json\/wp\/v2\/posts\/330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/getdevioapp.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/getdevioapp.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/getdevioapp.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/getdevioapp.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=330"}],"version-history":[{"count":5,"href":"https:\/\/getdevioapp.xyz\/index.php\/wp-json\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":359,"href":"https:\/\/getdevioapp.xyz\/index.php\/wp-json\/wp\/v2\/posts\/330\/revisions\/359"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/getdevioapp.xyz\/index.php\/wp-json\/wp\/v2\/media\/201"}],"wp:attachment":[{"href":"https:\/\/getdevioapp.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/getdevioapp.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/getdevioapp.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}