- Try to
flush privileges;in MySQL - Consult A2Hosting support
SQLSTATE[28000] [1045] Access denied for user, but credentials are correct (shared hosting)
Hi everyone, I’m stuck with a persistent SQLSTATE[HY000] [1045] error on a shared hosting environment (A2Hosting), and after 2 days of debugging I still can’t figure it out.
Here is the exact Laravel error:
SQLSTATE[28000] [1045] Access denied for user 'user-db'@'localhost' (using password: YES)
What’s strange is that the same database name, user, password and host work fine inside phpMyAdmin, but Laravel cannot connect.
✅ What I have already tested
- Checked
.envvalues multiple times All values are correct:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=db-name
DB_USERNAME=db-user
DB_PASSWORD="pw"
The user/password were copied directly from the hosting control panel and also tested manually.
- Tested PDO connection with a plain PHP script
Created
testdb.php:
$servername = "localhost"; // also tried 127.0.0.1
$username = "db-user";
$password = "pw";
$dbname = "db-name";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected!";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Result: Connection failed: SQLSTATE[HY000] [1045] Access denied for user So the issue is not Laravel, but the MySQL authentication at hosting level.
- Tried different DB_HOST values
localhost127.0.0.1- The server name from phpinfo:
nl1-ss102 - The hosting domain (suggested by support) None of them work.
- Cleared Laravel caches Ran:
php artisan config:clear
php artisan route:clear
php artisan view:clear
php artisan cache:clear
Also removed the entire bootstrap/cache folder.
No effects.
-
Recreated the database completely Deleted DB Created a new one with a new name Created a brand new MySQL user Granted ALL PRIVILEGES Copied fresh credentials into
.envStill same 1045 error. -
Checked PHP configuration The MySQL extension is enabled. No custom socket path listed in phpinfo.
❗ Important note I have other Laravel applications on the same hosting using:
DB_HOST=localhost
…and they all connect to MySQL without any issue. Only this new site fails, even though configuration is identical.
Given all the tests above, what else could cause an Access Denied (1045) error even with:
- correct user & password
- correct host
- correct database
- privileges granted
- working phpMyAdmin access Is there something specific to shared hosting (A2Hosting) that can cause misrouting or a wrong MySQL internal host, or does this mean the hosting provider hasn't created the user correctly at MySQL server level?
Any idea or angle would be appreciated.
Thanks in advance!
The issue was caused by an incorrect database password. Even though I had already copied/pasted the credentials multiple times, it seems there was a hidden character or formatting issue in the password.
I regenerated the password in cPanel, copied it again into .env, and the connection worked instantly.
Sorry for that
Thanks to everyone who took the time to help!
Please or to participate in this conversation.