Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

phpMyAdmin: Access denied for user 'root'@'localhost'

Error#1045 - Access denied for user 'root'@'localhost' (using password: NO)

Short answer

MySQL refused the root login. The message shows whether a password was sent: using password: NO means phpMyAdmin sent none. Log in with the mysql client first. If that fails, the password or account is wrong; if it works, fix the credentials in phpMyAdmin's config. As a last step, reset the root password.

  • Applies to MySQL 8.4, phpMyAdmin 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Skip phpMyAdmin and log in with the MySQL client. Its result tells you whether the problem is the server account or phpMyAdmin's settings. The end of the message tells you whether a password was sent.

    mysql -u root -p
    mysql -u root -p -h 127.0.0.1
  2. 2Match what you see, then jump to its fix

    1. 1(using password: NO), or (using password: YES) with a password you believe is rightA root password exists but none (or the wrong one) is being sentFix
    2. 2The error appears right on opening phpMyAdmin, before any login formconfig.inc.php holds credentials that no longer matchFix
    3. 3mysql -u root -p works, but connecting with -h 127.0.0.1 or from phpMyAdmin failsThe account matched is not the one you think (localhost vs 127.0.0.1)Fix
    4. 4No known password works and no other admin account existsThe root password is lost and has to be resetFix

What does the error look like?

  • #1045 - Access denied for user 'root'@'localhost' (using password: NO)
  • #1045 - Access denied for user 'root'@'localhost' (using password: YES)
  • mysqli::real_connect(): (HY000/1045): Access denied for user 'root'@'localhost'

Cause 1 of 4

A root password exists but none (or the wrong one) is being sent

1Check

A fresh install usually sets a root password. Logging in without one fails with this error. Old passwords left in an option file are also read by clients, so test with option files bypassed.

mysql -u root -p
mysqladmin --no-defaults -u root -p version

2Fix

Use the real password in phpMyAdmin's login form. If you do not know it, reset it (last cause below).

# phpMyAdmin login page: user root, the real password

3Verify

The mysql client login succeeds, and so does phpMyAdmin's.

mysql -u root -p -e 'SELECT CURRENT_USER();'

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 4

config.inc.php holds credentials that no longer match

1Check

phpMyAdmin authenticates against the MySQL server you use. Its FAQ lists wrong controluser and controlpass values as a cause of Access denied.

grep -n -E "controluser|controlpass|auth_type|\[.host.\]" /etc/phpmyadmin/config.inc.php

2Fix

Correct the user and password in config.inc.php, or remove the control user lines if you do not use that feature, and reload the page.

<?php
// config.inc.php
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['host'] = '127.0.0.1';

3Verify

The login form shows and signs in.

mysql -u root -p -h 127.0.0.1 -e 'SELECT 1;'

Confirmed by docs.phpmyadmin.net

Did this fix it?

Cause 3 of 4

The account matched is not the one you think (localhost vs 127.0.0.1)

1Check

localhost normally means a Unix socket connection; 127.0.0.1 forces TCP. Accounts are matched by user and host, so 'root'@'localhost' and an account for another host are different. Ask the server which row matched.

SELECT USER(), CURRENT_USER();
SELECT User, Host, plugin FROM mysql.user WHERE User = 'root';

2Fix

Use the host that matches an existing account, or create the account for the host you really connect from. Do not use '%' for root.

CREATE USER 'app'@'127.0.0.1' IDENTIFIED BY 'a-strong-password';
GRANT SELECT, INSERT, UPDATE, DELETE ON appdb.* TO 'app'@'127.0.0.1';

3Verify

Login over TCP works for that account.

mysql -u app -p -h 127.0.0.1 appdb -e 'SELECT 1;'

Confirmed by dev.mysql.com

Did this fix it?

Cause 4 of 4

The root password is lost and has to be reset

1Check

You need shell access to the server. The manual's recommended method restarts MySQL with an init file that holds one ALTER USER statement.

mysql --version

2Fix

Stop MySQL, write the init file, start the server with it, then delete the file and restart normally. Replace the sample password with your own.

# contents of /home/me/mysql-init (one line)
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

mysqld --init-file=/home/me/mysql-init &

3Verify

Log in with the new password, then remove the init file and restart MySQL normally.

mysql -u root -p
rm /home/me/mysql-init

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Use a named MySQL account with limited rights for applications, and keep root for administration.
  • Keep phpMyAdmin behind a VPN, an allow-list or HTTP auth, never open to the internet with root.
  • Store the root password in a password manager when the server is created.
  • Use localhost or 127.0.0.1 consistently in app config so one account matches.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.