xXSamXx's avatar

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

  1. Checked .env values 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.

  1. 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.

  1. Tried different DB_HOST values
  • localhost
  • 127.0.0.1
  • The server name from phpinfo: nl1-ss102
  • The hosting domain (suggested by support) None of them work.
  1. 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.

  1. 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 .env Still same 1045 error.

  2. 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!

0 likes
16 replies
Glukinho's avatar
  1. Try to flush privileges; in MySQL
  2. Consult A2Hosting support
xXSamXx's avatar

Thanks for the suggestion I tried to run FLUSH PRIVILEGES; but on shared hosting (A2Hosting) I don’t have the required privileges to execute that command — phpMyAdmin returns the usual “Access denied” message.

I’ve contacted A2Hosting support and asked them to check the MySQL user privileges on their side and to run a privilege reload if needed. I’ll update the thread as soon as they reply.

JussiMannisto's avatar

You don't normally need to flush privileges. It would only be needed if they manually edited the mysql.user table instead of granting permissions normally, using the grant statement. That seems unlikely.

Glukinho's avatar

I always have to use flush privileges after grant all privileges on...., new users are not applied without it, as far as I remember. Maybe it is different on modern versions of Mysql/Mariadb, I'm not sure.

Glukinho's avatar

@jussimannisto I just tried on Mariadb 11.8.2 - you're right, flush privileges is not neccessary after grant all privileges.... Thanks!

1 like
DigitalArtisan's avatar

It is obvious now. You have:

Access denied for user 'user-db'@'localhost' 

And you have

$username = "db-user";

It's either config cached, or your not using the .env you think you are.

Jsanwo64's avatar

try running

php artisan optimize:clear

Access is getting denied for user 'user-db'@'localhost'

But your user in config is different.

xXSamXx's avatar
xXSamXx
OP
Best Answer
Level 1

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!

Snapey's avatar

You might get this issue if your password contain a hash # symbol.

You paste this into your .env unquoted like everything else, but then dotenv sees the # as the start of a comment and truncates the password early.

Things like db passwords should be quoted "....."

@xxsamxx I see you quoted the password in your original post so this is probably not your issue, but I'll leave this here for others with db connection issues

shahriar_shaon's avatar

I actually ran into the same problem recently on a shared hosting environment. Even though my database credentials were correct, the connection kept failing.

What I learned is that on shared hosting, DB_HOST=127.0.0.1 usually doesn’t work. In most cases, you have to use

DB_HOST=localhost

Some providers even give you a custom hostname, something like:

DB_HOST=mysql.yourdomain.com

You can find the correct value in cPanel under MySQL Databases -> Hostname.

Another thing to watch out for is the MySQL port. Some hosts use a different port (like 3307), and if the port doesn’t match, you’ll still get the same “Access denied” error even with the right credentials.

sebastiangperez's avatar

Hi, for some reason our .env file or something is wrong because your connection is using another user for login, check every file.

also , check if you .env file is on the server, sometimes some file managers or ftp dont copy those files or add some things to make it visible and not hidden. happened to me with some .htaccess

Snapey's avatar

dont use ftp or file manager to deploy your project. Always pull from source control.

dipeshkhanal79's avatar

I had faced this kind of problems in the past and from then i always use "" to add values in .env file.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE="databasename"
DB_USERNAME="username"
DB_PASSWORD="pa$$word"
LaryAI's avatar
Level 58

This is a very frustrating situation, but you’ve done good troubleshooting already! Here are several things to try and some possible causes for persistent SQLSTATE[28000] [1045] errors on shared hosting, even when credentials work in phpMyAdmin.

1. Double-Check the MySQL Username Format

On shared hosting, the MySQL username is often prefixed with your cPanel name. For example, if your cPanel user is webuser, then your database user should be webuser_dbuser, not just dbuser.

Check your user in phpMyAdmin info panel or cPanel > MySQL Databases to confirm the real username.
Make sure your .env uses the full name, e.g.:

DB_DATABASE=webuser_dbname
DB_USERNAME=webuser_dbuser
DB_PASSWORD=thepassword

2. MySQL Permissions for User@Host

Even if the user/password/db are correct, the user must be granted access for connections from localhost:

  1. In cPanel > MySQL Databases, ensure your user is added to the database.
  2. Make sure they have all privileges.
  3. Some panels let you "reset" or reassign the database user to refresh permissions.

3. Password Integrity

If your password contains special characters (quotes, symbols), try setting a simpler password to rule out escaping/character issues between PHP, PDO, or the MySQL driver.

4. Socket vs Host Connection

Some shared hosting setups use a Unix socket instead of TCP for "localhost". But if your PHP script outside Laravel also fails, this is less likely. Still, you can force TCP by using 127.0.0.1 as host instead of localhost (as you have tested). If possible, check the host value in config/database.php for your connection:

'mysql' => [
    'host' => env('DB_HOST', '127.0.0.1'), // try both 'localhost' and '127.0.0.1'
    // ...
],

And in .env:

DB_HOST=127.0.0.1

5. Test with Pure MySQL CLI

If you have SSH access, try connecting via the CLI:

mysql -u webuser_dbuser -p -h localhost dbname

See if that works, or if you get access denied as well.

6. Hosting-specific Restrictions or Propagation

On some hosts, database/user changes can take several minutes to propagate, or are restricted to connections only from certain scripts (not command line or external scripts). If you just created the user/database, wait 10+ minutes and try again.

Additionally, on A2Hosting/Many shared hosts:

  • The PHP process may be running as a different user with limited MySQL access.
  • Sometimes old definitions persist. Try "changing" the password in cPanel to force a refresh.

7. File Permissions

Make sure your .env file is readable by the PHP user, so Laravel is able to fetch the credentials.

8. Config Caching Issues

You already cleared cache, but just to confirm, run these for good measure:

php artisan config:clear
php artisan cache:clear
php artisan config:cache

9. Last-Resort: Contact Host Support

Sometimes, the user creation process fails silently or MySQL access tables are not updated for CLI/PHP users. Tell A2Hosting support:

  • "I've created a user and database, attached as per docs."
  • "User/password match, work in phpMyAdmin, but PHP PDO connection fails with 1045 error."
  • "I have other sites/apps on same plan that work."

Ask them to explicitly check the grant tables and privileges for "yourcpaneluser_dbuser"@"localhost".


TL;DR Checklist

  • Confirm the full MySQL username is used (with hosting prefix).
  • Verify the user is assigned to the database with all privileges.
  • Try a simple password and avoid quotes/odd symbols.
  • Test both localhost and 127.0.0.1 as DB_HOST.
  • Ensure you wait a few minutes after user/db changes.
  • If all fails, contact A2Hosting support to verify the user creation/privilege-granting on their end.

Example Working .env

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=webuser_dbname
DB_USERNAME=webuser_dbuser
DB_PASSWORD=yourpassword

Replace webuser with your cPanel user.


If you confirm all of the above and it still fails, the most likely issue is with hosting-side user grants or propagation. Only the host’s support team can resolve that.

Let me know if you need help with any of the steps!

Please or to participate in this conversation.