Skip to content
Docs

SQLSTATE[HY000] [2002] Connection refused

On Tanzahost the database host is never 127.0.0.1 or localhost. Attach the database to your app and its DB_HOST is filled in for you.

Updated 17 Oct 20262 min read

The fix on Tanzahost: attach the database to your app, and don't type DB_HOST yourself. Your database runs in a sandbox of its own, not inside your app, so 127.0.0.1 and localhost never work. Its host is a name like mysql.duka.internal, and the app gets it as a variable.

  1. Open the database from your project's Overview.

  2. Under Apps with its connection details, switch on your app.

  3. The app redeploys by itself, without downtime, with DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD and DATABASE_URL set.

If you pasted your old .env into Variables, the database's values replace the DB_ lines you typed as soon as it's attached. See Connect an app to a database.

Why it happens

Error 2002 means your app couldn't open a connection at all: nothing answered at that host and port. On your computer, or on a cPanel host, the database runs next to the code, so 127.0.0.1 works. On Tanzahost every app and every database runs in its own sandbox. Your app's own 127.0.0.1 is only the app itself, and nothing listens there on port 3306.

With DB_HOST=localhost, PHP tries a file on the app's own disk instead of the network, and the same mistake reads SQLSTATE[HY000] [2002] No such file or directory. The fix is the same.

Asha's shop has a MySQL named mysql in project duka. Its host is mysql.duka.internal, on port 3306. Only apps in duka can reach it: an app in another project gets this error too, because the name doesn't exist there.

How to check

  1. Open the project's Variables tab and find DB_HOST. It should show as added by the database, with a host ending in .internal.

  2. If it says "Redeploy to apply", the live version still runs with the old values. Choose Redeploy.

  3. Open the database's page. If it isn't running yet (a new one takes about a minute to start), wait until it is, then deploy again.

  4. Open the app's terminal and print what the app really sees:

Terminal
printenv DB_HOST DB_PORT

If DB_HOST is right and the error stays, make sure your code reads it. Laravel's config/database.php does. A cached config from your computer doesn't follow you: bootstrap/cache starts empty on every start.