Error with mysqli_real_connect(): (HY000/1698): Access denied for user 'root'@'localhost'

I have followed all the steps of the LAMP installation tutorial in ubuntu 20.04 and even mysql is all enabled with flush privileges I don’t understand why I get this error mysqli_real_connect(): (HY000/1698): Access denied for user ‘root’@’ localhost’

Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Site Moderator
Site Moderator badge
May 12, 2022

Alternatively, if you still haven’t run mysql_secure_installation:

You need to change the authentication parameters, to do so just run this command first.

$ sudo mysql

Then run This ALTER Query to change the authentication parameters.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'mynewpassword';

And now, you are able to run mysql_secure_installation command.

$ sudo mysql_secure_installation

Hope that solve your issue,

Site Moderator
Site Moderator badge
April 4, 2022

Hi @DavidLuceroSigcho,

If you’ve used the mysql_secure_installation then you’ll need to use a password when connecting to your root user.

  1. mysql -u root -p

This will ask you for the root MySQL password. Once you give it, you’ll enter.

If you don’t want to use a password for MySQL when you are root, you can create a file in /root/.my.cnf and add the following:

  1. [mysql]
  2. user=root
  3. password=RootPasswordHere

where RootPasswordHere is your actual password.