MySQL connection error: 'root' user denied access on localhost

Hey guys, I’m stuck with a MySQL connection problem. When I try to run my PHP script, I get these errors:

Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in /var/www/html/frontend/htdocs/cfg.php on line 2

Warning: mysqli_query(): Couldn't fetch mysqli in /var/www/html/frontend/htdocs/cfg.php on line 4

Here’s my cfg.php file:

<?php
$db = new DatabaseConnection();
$config = new Configuration();

if ($_SERVER['SERVER_NAME'] == 'localhost') {
    $config->setBaseUrl('http://localhost/');
    $config->setApiUrl($config->getBaseUrl() + 'api/htdocs/api.php');
    $config->setAuthLoginUrl($config->getBaseUrl() + 'auth/htdocs/login.php');
    $config->setAuthVerifyTokenUrl($config->getBaseUrl() + 'auth/htdocs/verify_token.php');
} else {
    $config->setBaseUrl('http://example.com');
    $config->setApiUrl('http://api.example.com/api.php');
    $config->setAuthLoginUrl('http://auth.example.com/login.php');
    $config->setAuthVerifyTokenUrl('http://auth.example.com/verify_token.php');
}
?>

I can’t figure out what’s wrong. Any ideas on how to fix this? Thanks!

The error indicates that MySQL is rejecting the connection for the ‘root’ user rather than an issue in your PHP script code. It is important to verify that the MySQL service is running and that the credentials in your DatabaseConnection class match what is configured in your MySQL server. There is also a possibility that the ‘root’ user does not have the necessary privileges when connecting from localhost. As an alternative, consider creating a dedicated MySQL user with appropriate privileges to improve security and avoid using the root account in your web applications.

hey max, sounds like ur mysql denies perms. check if root pwd’s ok and if mysql’s runnin. maybe create a new user with proper perms too.

hmm, interesting issue! have u tried checkin the DatabaseConnection class? it might be usin wrong credentials. also, maybe ur mysql config is set to only allow connections from specific IPs? just wonderin, have u ever successfully connected before or is this a new setup?