Database Connection Issue: SQLSTATE[HY000] [1044] - Access Denied for User at Localhost to 'ecom'

I recently set up my local environment again and now I face a problem when trying to connect to my database using PHP PDO. The error states that access is denied for the user when accessing the ‘ecom’ database at localhost. I am not entirely sure if the credentials or connection details are correct. I would really appreciate some help to resolve this issue. Below is a sample of my updated code. Any advice to fix this would be very welcome.

<?php
class DBHandler {
    private $dataSource = "mysql:host=127.0.0.1;dbname=ecom";
    private $userName = "root";
    private $userPassword = "";
    public $dbConnection;

    public function __construct() {
        try {
            $this->dbConnection = new PDO($this->dataSource, $this->userName, $this->userPassword);
            $this->dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (PDOException $error) {
            die('Connection error: ' . $error->getMessage());
        }
    }
}
?>

Hey Daisy, have you tried checking the privileges for your MySQL user? I had a similar issue not long ago, and it turned out that my user didn’t have the right access to the ‘ecom’ database. I remember resolving it by updating the user permissions using a command like GRANT ALL PRIVILEGES ON ecom.* TO ‘root’@‘localhost’, then doing a FLUSH PRIVILEGES, although every setup can be a bit different.

It might also be worth confirming whether you’re using 127.0.0.1 or localhost. Sometimes these can behave a bit differently due to how MySQL handles them. I’m really curious if you have any custom settings in place that might be conflicting with this connection. What version of MySQL are you running? And have you tried troubleshooting via phpMyAdmin or another management tool?

I’m eager to see what steps you end up taking! Keep us updated on what you discover.

A similar issue occurred for me once, and I discovered that the problem was not only with user privileges but also with inconsistencies in configuration files. I experienced a mismatch between the credentials defined in the code and those in my MySQL configuration. In my case, I had overlooked a custom my.cnf directive that restricted access to certain hosts. Ensuring that both the PHP settings and database configuration reflect the same credentials and host settings resolved the issue. It might be worth reviewing your configuration files to resolve any discrepancies.

hey daisy, i had a similiar hiccup. check your host string and user grants, sometimes 127.0.0.1 vs localhost can behave odd. might also be a slight config mismatch, reverify the creds are correct in your my.cnf. hope it sorts out!

Hey Daisy, I’ve seen errors like this pop up before and it can sometimes be due to more subtle issues than just user permissions. I was in a situation where I ended up having forgotten to actually create the ‘ecom’ database even though I assumed it was there, and that led to this access denied vibe. Have you double-checked that the database exists as expected on your local setup?

Also, it might be interesting to see if any environment-specific overrides are kicking in—sometimes configuration in php.ini or even within your development tool can cause unexpected behavior. Are you using any frameworks or container setups where the database host details could be set elsewhere? I’d love to hear if you’ve noticed any other configuration files that might be getting in the way. What steps have you taken so far, and are there any tools you’re using to debug these kinds of issues? Always curious to know the process others follow!

My experience with similar issues has shown that the problem can often be traced back to more than just the user credentials. I encountered an access denied error once where the issue was actually due to an inconsistency between the defined host in the PDO connection string and the privileges given to the user. In that instance, double-checking the database’s existence and confirming that the account had been granted proper privileges resolved the error. I recommend verifying the host parameter and cross-checking all related configuration files, including any MySQL-specific settings that might override default behaviors.