Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

amrsubzero's avatar

Laravel cannot connect to database on shared hosting

I'm trying to deploy a laravel project on shared-hosting, and i can make it work to see and browse the project on the domain. but the database.

I have a directory on the root called laravel and a the public folder files all in public_html folder, and modified the index.php to be able to see the laravel folder on the root.

here's my .env file :

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=mydatabase
DB_USERNAME=myuser
DB_PASSWORD=mypassword

gives an error :

SQLSTATE[28000] [1045] Access denied for user 'myuser'@'localhost' (using password: YES)

i know, sometimes i need to config clear and cache clear. but in my case, whenever i make a change in the .env file, i can see it on the error. so it maybe refreshing continuously without requiring do call these commands. like if i change myuser to anotheruser i can see the change in the error, even i don't see a file called config.php in bootstrap/cache directory.

i tested a file that contains mysqli connection to the same database with the exact same credientals, and it successfully connects. the laravel thing doesn't dunno why.

i'm running Cpanel version 78.0.38 on shared hosting with php version 7.3.10. and i can clearly see there's no prefix before the database name and database user name. like oldschool cpanel. so i assume i don't need to include the prefixes as it is appearing in phpmyadmin without it.

Pdo :

N|Pdo

What causes this issue?

Thanks.

0 likes
6 replies
parjuag's avatar

Bro, 3 things:

  • Test changing DB_HOST=localhost to DB_HOST=127.0.0.1 can fix the problem.
  • In CPANEL: In the User Privileges in BD section, see that the user has all privileges checked.
  • Make sure the permissions for folders are 755 and 644 files, in the folder that contains the app, and give 755 privileges to storage and everything inside it. This command is useful:
  • For folders: find / any_folder -type f -exec chmod 644 {} ;
  • For files: find / any_folder -type d -exec chmod 755 {} ;
amrsubzero's avatar

@parjuag i've already performed these steps with no luck.

  • tried 127.0.0.1 and localhost

  • Cpanel database user has full privileges.

  • folders and files has its requries permissions.

  • created storage symlink.

But didn't also work.

PDO problem? i can see it included and enabled in phpinfo.

N|Pdo

Its getting me crazy!

Ap3twe's avatar

Test a fresh laravel project. Then post the error

amrsubzero's avatar

@ap3twe i will test with fresh laravel project. but u may notice that i posted the error in this topic.

Please or to participate in this conversation.