AwardSpace免费主机数据库连接报错求助:PDOException SQLSTATE[HY000] [2002]
Hey there, let’s troubleshoot that SQLSTATE[HY000] [2002] Connection refused error you’re hitting when trying to log into your admin panel. This is a super common issue with shared hosting setups like AwardSpace, and it almost always traces back to incorrect connection settings or hosting-specific constraints. Here’s how to work through it step by step:
1. Replace localhost with AwardSpace’s Dedicated Database Host
AwardSpace keeps databases on separate servers from your web hosting space—so using localhost in your PDO connection won’t work (your web server doesn’t run MySQL locally).
- Log into your AwardSpace control panel and head to the MySQL Databases section.
- Look for the Database Host field (it’ll look something like
mysqlX.awardspace.net, where X is a number). - Update your PDO connection string in
entraradmin.phpto use this exact host instead oflocalhost.
Example corrected DSN:
$dsn = 'mysql:host=mysqlX.awardspace.net;dbname=your_database_name;charset=utf8mb4';
2. Double-Check Your Database Credentials
Typos or mismatched credentials are the #1 culprit here:
- Confirm the Database Username matches exactly what’s listed in your control panel (it’s usually a combination of your account prefix and database name, like
abc123_mydb). - Make sure your password has no extra spaces or typos—even one wrong character will block the connection.
3. Verify the Database Name in Your DSN
It’s easy to mistype the database name in your PDO string. Cross-reference the dbname parameter with the exact name shown in AwardSpace’s MySQL Databases section.
4. Add the Port Number (If Needed)
While AwardSpace uses the default MySQL port (3306) for most plans, some setups require explicitly specifying it. Try adding the port to your DSN if the above steps don’t fix the issue:
$dsn = 'mysql:host=mysqlX.awardspace.net;port=3306;dbname=your_database_name;charset=utf8mb4';
5. Validate Your PDO Connection Code
Make sure your initialization code is structured correctly. A common mistake is missing parameters or incorrect syntax. Here’s a solid example of a valid PDO connection:
try { $pdo = new PDO( 'mysql:host=mysqlX.awardspace.net;dbname=your_database_name;charset=utf8mb4', 'your_db_username', 'your_db_password', [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ] ); } catch (PDOException $e) { die("Database connection failed: " . $e->getMessage()); }
6. Check if the Database Service is Accessible
Rarely, AwardSpace might have temporary database server issues. Test if you can access phpMyAdmin directly through your control panel—if that works, the problem is definitely in your connection code (not the server itself).
If you’ve tried all these steps and still get the error, double-check that entraradmin.php was uploaded correctly via FTP (no file corruption) and that its permissions are set to 644 (standard for PHP files on shared hosts).
内容的提问来源于stack exchange,提问作者E. AMARAL

