Oli Ullah
Oli Ullah

Reputation: 161

Database connection error Access denied for user 'root'@'localhost' (using password: NO)

Whenver i go to locallhost/Blog/app/DB i encounter this error. I have tried so many times to connect to the database but could not connect. Even if i use my mysql password which is "root" this does not work.Please someone help me Here is my connection code:

host = "localhost";
$username = "root";
$password ="";
$db_name ="blog";

$conn = new mysqli($host, $username, $password, $db_name);

if($conn->connect_error){
    die(' Database connection error '.$conn->connect_error);
}

This is my phpmyadmin user account:enter image description here

Upvotes: 1

Views: 1773

Answers (1)

matigo
matigo

Reputation: 1461

As Indra said, avoid using root. This account should only be used by a human, even in a development environment. If you need an account that has complete and total access to everything, create one:

CREATE USER 'indra'@'localhost' IDENTIFIED WITH mysql_native_password BY 'superSecretPassword!123';
GRANT ALL ON *.* TO 'indra'@'localhost' WITH GRANT OPTION;

The root account in MySQL should only be used when you are configuring replication or fixing something what went really, really wrong 🤐

Upvotes: 1

Related Questions