Database connection error: Access denied for user on localhost

I'm facing a problem with my database connection after reinstalling WAMP server. I'm using PHP PDO, but I keep getting an error message. Here's what I've tried:

class DBConnector {
    private $host = 'localhost';
    private $db = 'mystore';
    private $user = 'root';
    private $pass = '';
    public $connection;

    function __construct() {
        if (!$this->connection) {
            try {
                $dsn = "mysql:host={$this->host};dbname={$this->db}";
                $this->connection = new PDO($dsn, $this->user, $this->pass);
                $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $error) {
                echo "Connection failed: " . $error->getMessage();
                exit;
            }
        }
    }
}

The error says access is denied for user @localhost to database 'mystore'. Any ideas what might be causing this? I've double-checked my credentials, but I'm still stuck. Help would be much appreciated!

Have you verified that your MySQL server is actually running and accessible? After a WAMP reinstall, it’s not uncommon for services to be in a stopped state. Check your WAMP tray icon to ensure MySQL is active.

Also, double-check your MySQL root password. Even if you think it’s blank, it might have been set during the reinstall. You can try connecting via command line:

mysql -u root -p

If that works, but your code doesn’t, the issue might be with PDO. Ensure you have the proper PHP extensions enabled in your php.ini file.

Lastly, if all else fails, consider backing up your databases and doing a clean MySQL install. Sometimes that’s the quickest way to resolve persistent connection issues.

Hey there Ethan_Cosmos! :slightly_smiling_face:

Have you tried checking if your MySQL service is actually running? Sometimes after reinstalling WAMP, the service doesn’t start automatically.

Right-click on the WAMP icon in your system tray and check if the MySQL service is active—if it’s not, try starting it manually. I’m also curious, have you been able to connect to any other databases since the reinstall, or is it just ‘mystore’ that’s causing issues?

Also, have you considered connecting with a GUI tool like HeidiSQL or phpMyAdmin for a bit more insight? Sometimes those tools can provide detailed error messages that might help pinpoint the problem.

Let me know how things go—database troubles can be a pain, but discussing it here might get us closer to a solution!

hey mate, have u checked ur mysql user permissions? sometimes after reinstalling wamp, the default settings can get messed up. try logging into mysql as root and running:

GRANT ALL PRIVILEGES ON mystore.* TO ‘root’@‘localhost’;
FLUSH PRIVILEGES;

that might fix it for ya. lmk if u need more help!