What the error is telling you
Every version of this error has the same shape, and each part is a clue:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
- 1045 is MySQL's
ER_ACCESS_DENIED_ERROR. The server received a user name and a password (or no password) and rejected them. - 'root'@'localhost' is the account the server tried to match. A MySQL account is a user name plus a host, so
'root'@'localhost'and'root'@'%'are two different accounts that can have different passwords. - using password: YES means your client sent a password and it did not match. using password: NO means no password was sent at all, usually because you left out
-por a script passed an empty value.
A closely related message has no password part:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
Error 1698 is ER_ACCESS_DENIED_NO_PASSWORD_ERROR. On Ubuntu you usually see it when root is set up for socket authentication and you connect as an ordinary Linux user. No password will ever work in that case, because the server is not checking a password. It is checking who you are logged in as.
Quick diagnosis
Match what you typed and what you saw, then jump to the fix.
| What you see | Most likely cause | Fix |
|---|---|---|
ERROR 1698 on a fresh Ubuntu install, with or without a password | root uses auth_socket (MySQL) or unix_socket (MariaDB) | Run sudo mysql; create a separate password user for apps |
using password: NO | No password sent: missing -p, empty variable in a script or config | Add -p and type the password at the prompt |
using password: YES and you are sure the password is right | A different account matched, a password in an option file overrode yours, or shell quoting changed it | Test with --no-defaults and a prompted password; check which account exists |
using password: YES and the password is lost | Forgotten or never-recorded root password | Reset it (Ubuntu or Windows steps below) |
'root'@'203.0.113.10' or another IP in place of localhost | No root account exists for that remote host | Create a dedicated user for that host, or use an SSH tunnel |
Works with sudo mysql, fails with -h 127.0.0.1 | Socket authentication only works over the local socket file, not TCP | Connect without -h, or give the account a password |
| phpMyAdmin or Workbench rejects root | root has no password on Ubuntu; it authenticates by Linux user | Create an admin user with a password and log in as that |
If you see ERROR 2002 or ERROR 2003 instead, the client could not connect to the server at all. That usually points to a stopped service, a wrong socket path or a firewall, not a password problem.
Step 1: try sudo before anything else
On Ubuntu 24.04 and 22.04 the packaged mysql-server creates 'root'@'localhost' with the auth_socket plugin. Ubuntu's own server documentation says no password is required, because MySQL checks the Linux user instead. The MySQL manual explains how: the plugin reads the operating-system user of the process on the other end of the Unix socket, and lets you in only if that name matches the MySQL user name. So Linux root can log in as MySQL root with no password, and your normal login cannot, whatever password you type.
MariaDB does the same thing with its unix_socket plugin. Since MariaDB 10.4, root@localhost is created to try unix_socket first and a password second, but the password is initially set to an invalid value, so only the socket route works until you set one. Ubuntu 24.04 ships MariaDB 10.11 and Ubuntu 22.04 ships 10.6, so both behave this way.
Check which server you have, then log in through the socket:
mysql --version
sudo mysql
If sudo mysql drops you at a mysql> or MariaDB [(none)]> prompt, nothing is broken. Confirm how root authenticates:
SHOW CREATE USER 'root'@'localhost';
Look for IDENTIFIED WITH 'auth_socket' on MySQL or IDENTIFIED VIA unix_socket on MariaDB. Either one means: use sudo for admin work and do not give apps the root account.
Step 2: give apps and GUI tools their own user
Most people hit this error because a web app, phpMyAdmin or MySQL Workbench is trying to log in as root with a password, and on Ubuntu root has no password to match. Leave root on socket authentication and create accounts with passwords instead. From sudo mysql:
-- one user per application, limited to its own database
CREATE DATABASE shopdb;
CREATE USER 'shopapp'@'localhost' IDENTIFIED BY 'Use-A-Long-Random-Password';
GRANT ALL PRIVILEGES ON shopdb.* TO 'shopapp'@'localhost';
-- optional: a full admin for phpMyAdmin or Workbench
CREATE USER 'dbadmin'@'localhost' IDENTIFIED BY 'Another-Long-Random-Password';
GRANT ALL PRIVILEGES ON *.* TO 'dbadmin'@'localhost' WITH GRANT OPTION;
On MySQL 8.0, a password account uses caching_sha2_password, which is the default plugin. Put the application user in your app's config (for WordPress, wp-config.php). If the site still shows a database error after that, our guide to Error establishing a database connection covers the WordPress side.
If you really want root to log in with a password, you can switch it. On MySQL:
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'New-Strong-Password';
On MariaDB, keep the socket and add a password as a second method:
ALTER USER 'root'@'localhost' IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('New-Strong-Password');
Write the password down before you leave the session. After the MySQL change, sudo mysql on its own no longer works; you need mysql -u root -p.
Step 3: when the password should be right but is refused
If you get using password: YES and you are confident about the password, check these in order.
Let the client prompt for the password
Typing the password on the command line exposes it to your shell. Characters such as $, ! and & can be expanded or cut off before MySQL sees them, so the server receives something else. Use -p with no value and type the password at the prompt:
mysql -u root -p
Rule out option files
Clients read option files such as /etc/mysql/my.cnf and ~/.my.cnf. The MySQL manual notes that if you get access denied even without typing a password, an option file may be supplying a wrong one. The manual suggests testing with --no-defaults, which stops the client reading option files. Put it first:
mysql --no-defaults -u root -p
If that works, look for a password= line under [client] or [mysql] in those files and fix or remove it.
Check which account actually exists
Log in through sudo mysql (or any working admin user) and list the accounts for that user name:
SELECT user, host, plugin FROM mysql.user WHERE user IN ('root', '');
Three things to look for:
- Host mismatch. If root exists only as
'root'@'localhost', a connection with-h 127.0.0.1or from another machine does not match it. - Anonymous users. A row with an empty user name, for example
''@'localhost', can match before your named account. MySQL sorts accounts with the most specific host first and uses the first row that matches. RunSELECT USER(), CURRENT_USER();after logging in: ifCURRENT_USER()shows@localhostwith no name, you were let in as the anonymous account.mysql_secure_installationoffers to remove anonymous users. - Plugin.
auth_socketorunix_socketin the plugin column means the password is not checked at all. Go back to Step 1.
localhost is not 127.0.0.1
On Linux, a client connecting to localhost uses the Unix socket file. --host=127.0.0.1 forces TCP/IP instead. Socket authentication works only over the socket file, and an account created for localhost may not match a TCP connection. If mysql -u root -p works but your app fails, compare the host setting in the app with the account's host.
Reset a forgotten root password on Ubuntu
If root has a password and nobody knows it, start the server with grant checks switched off, set a new password, then restart normally. The MySQL manual describes this as the generic method. With --skip-grant-tables anyone can connect with all privileges, so the server also turns on skip_networking and refuses remote connections while it runs. Do this from SSH or a server console, and do not leave it running.
MySQL 8.0
sudo systemctl stop mysql
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
sudo -u mysql mysqld --skip-grant-tables --skip-networking &
# wait a few seconds for the server to start, then:
mysql -u root
At the mysql> prompt, reload the grant tables first, because account statements such as ALTER USER are disabled until you do:
FLUSH PRIVILEGES;
-- either put root back on Ubuntu's default socket login
ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
-- or give it a password instead
-- ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'New-Strong-Password';
EXIT;
Then stop the temporary server with a normal kill (not kill -9) and start the service again:
sudo kill $(pidof mysqld)
sudo systemctl start mysql
sudo mysql # or: mysql -u root -p
MariaDB 10.6 and 10.11
sudo systemctl stop mariadb
sudo mkdir -p /run/mysqld
sudo chown mysql:mysql /run/mysqld
sudo -u mysql mariadbd --skip-grant-tables --skip-networking &
# wait a few seconds for the server to start, then:
mysql -u root
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('New-Strong-Password');
EXIT;
sudo kill $(pidof mariadbd)
sudo systemctl start mariadb
The MySQL manual also documents a safer variant that never opens an unauthenticated server: put the single ALTER USER line in a file readable only by the mysql user and start mysqld --init-file=/path/to/file. The server runs that statement at startup. Delete the file as soon as the server is up, then stop that server and start the service normally. On Ubuntu, AppArmor can stop mysqld reading a file outside the paths its profile allows; if the password does not change, use the steps above instead.
Reset the root password on Windows
On Windows the MySQL manual recommends the init-file method:
- Log on as Administrator. Open Services, find the MySQL service and stop it.
- Create
C:\mysql-init.txtcontaining one line:ALTER USER 'root'@'localhost' IDENTIFIED BY 'New-Strong-Password'; - Open Command Prompt as Administrator and start the server with the file. The backslash in the path is doubled:
The rightcd "C:\Program Files\MySQL\MySQL Server 8.0\bin" mysqld --defaults-file="C:\\ProgramData\\MySQL\\MySQL Server 8.0\\my.ini" --init-file=C:\\mysql-init.txt --console--defaults-filevalue is in the service's Path to executable field in Services. Adjust the folders for your version. - When the server has started, delete
C:\mysql-init.txt. Stop this server from a second Command Prompt in the samebinfolder withmysqladmin -u root -p shutdown(using the new password), then start the service normally from Services. - Log in with
mysql -u root -pand the new password.
Access denied for root from another machine
If the error names an IP address, for example 'root'@'203.0.113.10', the server is reachable but has no account for root at that host. That is the safe default; do not fix it by creating 'root'@'%'. Two better options:
- Use an SSH tunnel. Keep MySQL listening on localhost only and forward a port over SSH:
ssh -L 3307:127.0.0.1:3306 user@your-server, then point your tool at127.0.0.1:3307with a password account. Our SSH tunnel guide explains the flags. - Create a named user for one IP.
CREATE USER 'report'@'203.0.113.10' IDENTIFIED BY '...';with only the privileges it needs. You also need MySQL listening on a public address (bind-addressin/etc/mysql/mysql.conf.d/mysqld.cnfon Ubuntu) and a firewall rule that allows port 3306 from that IP alone.
Common mistakes
- Running
mysql -u root -pwithout sudo on a fresh Ubuntu server and then trying to reset a password that was never set. Trysudo mysqlfirst. - Writing
'root@localhost'as one quoted string. The user and host parts are quoted separately:'root'@'localhost'. The single-string form means a user literally calledroot@localhoston any host. - Forgetting
FLUSH PRIVILEGESunder--skip-grant-tables, then wondering whyALTER USERfails. - Leaving the server in skip-grant-tables mode, or leaving an init file with the new password on disk.
- Starting
mysqldas Linux root. The manual warns this can leave root-owned files in the data directory that break later startups. Usesudo -u mysql. - Using root in application configs. One leaked config then exposes every database on the server. Give each app its own user.
- Opening
'root'@'%'to the internet to make a remote tool work. Use a tunnel or a limited user instead.
On Bigdomainhost servers
On a Bigdomainhost KVM VPS you have full root access, so every command above works as written on an Ubuntu server. If a reset goes wrong and SSH is not an option, the out-of-band console in the control panel gives you a terminal on the server. On Web & cPanel hosting you do not get a MySQL root account: you create databases and users in cPanel and manage them in phpMyAdmin, and remote database access is off by default and enabled per IP. There, an access-denied error almost always means the app is using the wrong database user name, including the cPanel account prefix, or the wrong password.
Stuck on a reset? Message us on WhatsApp at +91 75994 50220 at any time, call during IST working hours, or email support@bigdomainhost.com.
This guide was written by the Bigdomainhost team with AI assistance. Commands and behaviour were checked against the MySQL 8.0 Reference Manual, MariaDB documentation and Ubuntu Server documentation on 28 November 2026; package versions are those listed for Ubuntu 24.04 and 22.04 on that date.
Sources (checked )
- MySQL 8.0 Reference Manual: B.3.2.4 Problems Connecting to MySQL (Access denied, using password YES/NO, --no-defaults)
- MySQL 8.0 Reference Manual: B.3.3.2 How to Reset the Root Password (Windows, Unix and generic)
- MySQL Documentation: MySQL 8.0 Error Reference: server errors 1045 and 1698
- MySQL 8.0 Reference Manual: Access Control, Stage 1: Connection Verification (account matching, anonymous users, CURRENT_USER)
- MySQL 8.0 Reference Manual: Socket Peer-Credential Pluggable Authentication (auth_socket)
- MySQL 8.0 Reference Manual: Specifying Account Names
- Ubuntu Server documentation: Install and configure a MySQL server
- MariaDB Knowledge Base (Wayback Machine snapshot): Authentication from MariaDB 10.4
- MariaDB Knowledge Base (Wayback Machine snapshot): ALTER USER