Talk to the NOC, IST hours — +91 75994 50220 support@bigdomainhost.com
Guide

Access denied for user 'root'@'localhost': how to fix MySQL error 1045 and 1698

You type mysql -u root -p, enter the password you are sure is right, and MySQL refuses. On a fresh Ubuntu server the cause is usually not the password at all. This guide shows how to read the error, find the real cause in a minute, and reset the root password on Ubuntu or Windows only if you actually need to.

Short answer: "Access denied for user 'root'@'localhost'" means MySQL or MariaDB rejected the root login, usually because root uses socket authentication (try sudo mysql), the password is wrong, or a different account matched. If the password is truly lost, restart with --skip-grant-tables or --init-file and set a new one with ALTER USER.

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 -p or 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.

Access denied for root: symptom, cause and fix
What you seeMost likely causeFix
ERROR 1698 on a fresh Ubuntu install, with or without a passwordroot uses auth_socket (MySQL) or unix_socket (MariaDB)Run sudo mysql; create a separate password user for apps
using password: NONo password sent: missing -p, empty variable in a script or configAdd -p and type the password at the prompt
using password: YES and you are sure the password is rightA different account matched, a password in an option file overrode yours, or shell quoting changed itTest with --no-defaults and a prompted password; check which account exists
using password: YES and the password is lostForgotten or never-recorded root passwordReset it (Ubuntu or Windows steps below)
'root'@'203.0.113.10' or another IP in place of localhostNo root account exists for that remote hostCreate a dedicated user for that host, or use an SSH tunnel
Works with sudo mysql, fails with -h 127.0.0.1Socket authentication only works over the local socket file, not TCPConnect without -h, or give the account a password
phpMyAdmin or Workbench rejects rootroot has no password on Ubuntu; it authenticates by Linux userCreate 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.1 or 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. Run SELECT USER(), CURRENT_USER(); after logging in: if CURRENT_USER() shows @localhost with no name, you were let in as the anonymous account. mysql_secure_installation offers to remove anonymous users.
  • Plugin. auth_socket or unix_socket in 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:

  1. Log on as Administrator. Open Services, find the MySQL service and stop it.
  2. Create C:\mysql-init.txt containing one line:
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'New-Strong-Password';
  3. Open Command Prompt as Administrator and start the server with the file. The backslash in the path is doubled:
    cd "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
    The right --defaults-file value is in the service's Path to executable field in Services. Adjust the folders for your version.
  4. When the server has started, delete C:\mysql-init.txt. Stop this server from a second Command Prompt in the same bin folder with mysqladmin -u root -p shutdown (using the new password), then start the service normally from Services.
  5. Log in with mysql -u root -p and 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 at 127.0.0.1:3307 with 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-address in /etc/mysql/mysql.conf.d/mysqld.cnf on Ubuntu) and a firewall rule that allows port 3306 from that IP alone.

Common mistakes

  • Running mysql -u root -p without sudo on a fresh Ubuntu server and then trying to reset a password that was never set. Try sudo mysql first.
  • 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 called root@localhost on any host.
  • Forgetting FLUSH PRIVILEGES under --skip-grant-tables, then wondering why ALTER USER fails.
  • Leaving the server in skip-grant-tables mode, or leaving an init file with the new password on disk.
  • Starting mysqld as Linux root. The manual warns this can leave root-owned files in the data directory that break later startups. Use sudo -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 )

Answers

Frequently asked questions

Still unsure? Call +91 75994 50220 or write to support@bigdomainhost.com — a human replies in IST hours.

What does error 1045 mean in MySQL?

Error 1045 (SQLSTATE 28000) is ER_ACCESS_DENIED_ERROR: the server rejected the user name, host and password combination you sent. The message names the account it tried, such as 'root'@'localhost', and says whether a password was sent (using password: YES) or not (using password: NO).

What is the difference between using password: YES and using password: NO?

YES means the client sent a password and it did not match the account. NO means no password was sent, usually because -p was left out or a script passed an empty value. With NO, add -p and type the password at the prompt before trying anything else.

Why does sudo mysql work but mysql -u root -p does not?

On Ubuntu, MySQL's root@localhost account uses the auth_socket plugin, and MariaDB's uses unix_socket. The server checks which Linux user is connecting over the socket, not a password. sudo makes you Linux root, so it matches; a password typed as another user never does.

What is the default MySQL root password on Ubuntu 24.04?

There is none. Ubuntu's mysql-server package sets root@localhost to authenticate with auth_socket, so you log in with sudo mysql and no password. If you need a password login for an app or phpMyAdmin, create a separate user with a password rather than changing root.

How do I reset the MySQL root password if I forgot it?

Stop the service, start mysqld with --skip-grant-tables and --skip-networking, connect with mysql -u root, run FLUSH PRIVILEGES, then ALTER USER 'root'@'localhost' with the new password or auth_socket. Stop that server with a normal kill and start the service again. The --init-file method avoids running an open server.

What is error 1698 access denied for user root?

Error 1698 is ER_ACCESS_DENIED_NO_PASSWORD_ERROR. On Ubuntu it usually appears when root uses socket authentication and you connect as a normal Linux user. Run sudo mysql instead, or create a separate account with a password for tools that need one.

Should I allow root to log in from any host to fix remote access denied?

No. Creating 'root'@'%' exposes your most powerful account to the network. Use an SSH tunnel to reach MySQL on localhost, or create a named user limited to one IP address and only the databases it needs, with the firewall open to that IP alone.

Can I get MySQL root access on cPanel shared hosting?

No. On shared cPanel hosting you create databases and users in cPanel and manage them in phpMyAdmin; there is no MySQL root account. Access denied there usually means the wrong user name, often missing the cPanel prefix, or the wrong password in the app config.

Support that picks up the phone.

In IST hours, in Hindi or English, and WhatsApp replies fastest at any time. Sales, migration and emergencies all reach the same engineers. No offshore queue, no 48-hour first reply.

Need cPanel or WHM with the ports already sorted?

Web & cPanel hosting comes with cPanel ready to log in to, reseller hosting adds white-label WHM, and a KVM VPS can have cPanel & WHM pre-installed. Call +91 75994 50220 or email support@bigdomainhost.com.