PHP MySQL connection returns “Connection refused”

Website URL

https://laanadiduradedios.infinityfreeapp.com

Error Message

Hello,

My PHP website is hosted on InfinityFree and cannot connect to its MySQL database. The exact error is:

SQLSTATE[HY000] [2002] Connection refused

The MySQL hostname shown in my control panel is sql208.infinityfree.com. The database exists and I can access it through phpMyAdmin.

Could you please check why the MySQL server is refusing connections from my hosting account and confirm the correct hostname?

Thank you.

Other Information

SQLSTATE[HY000] [2002] Connection refused

Beep boop, I’m a bot! I dug through the forum to get you started. I sometimes get things wrong though, so do with this what you will until a human has a look.

Could you post the lines of your PHP code that set up the connection, with the password removed? I need to see the hostname, database name and username your script uses, and whether it’s the PDO DSN or a config file.

Your control panel host is sql208, and the database on your account has a name ending in la_anadidura_dios after the account prefix. I’d like to compare those against what your code has, character by character. I also need to know whether the script is running on your InfinityFree site, or somewhere else such as your own computer or another host.

I’m asking about the last point because of what the knowledge base says about connections. InfinityFree databases can only be reached from inside an InfinityFree hosting account, not from other locations. It also says the hostname must be the one from your control panel’s MySQL Databases section, never localhost.

The article doesn’t list “Connection refused” as one of its examples, so I can’t say yet which of these is the cause.

Here’s the article:

The script is running on my InfinityFree website: https://laanadiduradedios.infinityfreeapp.com, not on my computer or another host.

The connection is made with PDO using a DSN. The relevant code is:<?php
$host = getenv(‘DB_HOST’) ?: ‘127.0.0.1’;
$database = getenv(‘DB_NAME’) ?: ‘la_anadidura_dios’;
$username = getenv(‘DB_USER’) ?: ‘root’;
$password = getenv(‘DB_PASSWORD’) ?: ‘’; // redacted
$dsn = “mysql:host={$host};dbname={$database};charset=utf8mb4”;
$pdo = new PDO($dsn, $username, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]); My .env values are:

DB_HOST=sql208.infinityfree.com
DB_NAME=if0_43408642_la_anadidura_dios
DB_USER=if0_43408642
DB_PASSWORD is set but redacted.
The exact error returned by the test is SQLSTATE[HY000] [2002] Connection refused. The database and tables are visible in phpMyAdmin. Could you help identify why the connection is refused?

Did you read?

Again, the answer is so obvious

Stop relying too much on AI and use the grey matter within your head

No, not really.

The specific code tries to read the DB_HOST environment variable, and if it’s not set or empty, use 127.0.0.1.

But using 127.0.0.1 would cause that Connection Refused error. So I suspect that getenv('DB_HOST') is not actually returning the configuration from your .env file.

A simple line saying var_dump(getenv('DB_HOST')); just before the database connection will tell you whether that’s the case.