mysqli_real_connect(): (HY000/1698): Access denied for user ‘root’@’localhost’ – How to Fix
Debian 9, phpMyAdmin 4.9. Instead of installing phpMyAdmin from packages, we just unpack it from the archive, log in to the empty database as root for the first time and get an error:
mysqli_real_connect(): (HY000/1698): Access denied for user 'root'@'localhost'
In short: on Debian and Ubuntu, the MariaDB/MySQL root user logs in by default not with a password but through the socket – by the operating system user name (the unix_socket or auth_socket plugin). From the console as root everything works, but phpMyAdmin runs as www-data, so the server rejects it. The fix is to create a separate user with a password for phpMyAdmin or to set a password for root.
Below: why it happens, two proper solutions, and the old method that many people still recommend even though it is unsafe.
Why error 1698 appears
Code 1698 means the server refused the login because password authentication is not set up for this account. On Debian and Ubuntu, the database root is tied to the system root:
- in the console as root (or via
sudo) you get into the database without a password – the server checks who you are in the system; - phpMyAdmin connects as the web server user (
www-data), not as root, and is refused even with an empty password field; - root has no password, so there is no “correct” password to type into phpMyAdmin either.
This is a security feature: root cannot be brute-forced through a website or phpMyAdmin. So it is better not to break this setup and create a separate user for phpMyAdmin instead.
Solution 1: a separate user for phpMyAdmin (recommended)
Log in to the database from the server console:
sudo mariadb
(for MySQL – sudo mysql) and create a user with full privileges. Replace Strong-Pass with your own strong password:
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'Strong-Pass'; GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION; EXIT;
Now log in to phpMyAdmin as admin with that password. Root still logs in only via sudo, as before. FLUSH PRIVILEGES is not needed after CREATE USER and GRANT – the privileges apply immediately.
If the user is only needed for one website, grant access to one database rather than all of them (mysite is your database name):
GRANT ALL PRIVILEGES ON mysite.* TO 'admin'@'localhost';
Solution 2: a password for root, keeping sudo login
If you need exactly root in phpMyAdmin, in MariaDB 10.4 and later you can keep socket login and add a password:
ALTER USER root@localhost
IDENTIFIED VIA unix_socket
OR mysql_native_password
USING PASSWORD('Strong-Pass');
After that sudo mariadb works without a password, and phpMyAdmin lets root in with the password. Both solutions were tested on MariaDB 11.8 (November 2026).
In MySQL 8.0 and 8.4 (for example, on Ubuntu) the command is different:
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'Strong-Pass';
Note: this switches root to password-only login, and sudo mysql without -p will no longer work. The mysql_native_password plugin is disabled by default in MySQL 8.4 and removed in MySQL 9, so use caching_sha2_password for MySQL.
The old method and why it no longer works
A common tip (I used to give it too) is to simply reset root’s authentication plugin:
use mysql; update user set plugin='' where User='root'; flush privileges; exit
On Debian 9 with MariaDB 10.1 this worked, but the method has two problems:
- Root is left without a password. Unless you set one right away, anyone with access to the server can log in to the database as root – including through phpMyAdmin if its settings allow passwordless login (
AllowNoPassword). - It does not work in MariaDB 10.4 and later. There
mysql.useris a view, and the command fails withERROR 1356 (HY000)– tested on MariaDB 11.8. Accounts are changed only withCREATE USER,ALTER USERandGRANT.
Similar errors
- 1045 Access denied … (using password: YES) – the password is wrong or the user has a different host (for example,
'admin'@'%'was created, but you connect vialocalhost). - 1524 Plugin ‘mysql_native_password’ is not loaded – MySQL 8.4 and later: switch the user to
caching_sha2_password, as in solution 2. - “Login without a password is forbidden by configuration” – phpMyAdmin refuses a user without a password (
AllowNoPasswordis off by default). Setting a password is better than turning this option on.
Need your own server for a website and phpMyAdmin? You can get a Debian or Ubuntu VPS from Vultr (affiliate link).
