1

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

4
  • Maybe because you don't have a password set? Commented May 15, 2021 at 7:59
  • @Crimin4L I have a set password which is root. If I use this password in PHP code then I am seeing this error Access denied for user 'root'@'localhost' (using password: YES) Commented May 15, 2021 at 8:07
  • Not sure it will make any difference, but have you tried setting the same password for 127.0.0.1, ::1 and localhost? It seems odd to have different credentials for those since they are all local addresses. Could be worth a try? Or even just try with 127.0.0.1 instead. (And don't forget to flush privileges if you do any changes to the users) Commented May 15, 2021 at 8:15
  • 2
    Avoid using root. Create a new user with CRUD privilege and access to db Commented May 15, 2021 at 8:24

1 Answer 1

1

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 🤐

Sign up to request clarification or add additional context in comments.

1 Comment

'indra'@'localhost' 😄

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.