Updated: 12 October 2026 · Applies to: MySQL 8.4 (tested with 8.4.11 on Ubuntu 26.04 and AlmaLinux 10.2), MariaDB 10.11 and 11.8 (tested with 10.11.18 on AlmaLinux 10.2 and 11.8.6 on Debian 13), cPanel and WHM (according to cPanel)

To change the MySQL root password when you can still log in, run this as the MySQL root user:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'New-Strong-Password';

If the password is lost, stop the database server, start it once with --skip-grant-tables --skip-networking, run FLUSH PRIVILEGES; and the same ALTER USER statement, then restart it normally. Both are explained step by step below. In MySQL and MariaDB the administrator account is root; it is separate from the Linux root user.

1. First check how root logs in

Many servers do not use a MySQL root password at all. Try this as the Linux root user (or with sudo):

sudo mysql
  • Ubuntu with MySQL: root uses the auth_socket plugin, so sudo mysql logs in without a password. Any password you type is ignored: mysql -u root -p with a wrong password still logged in as the Linux root user.
  • MariaDB (10.4 and later, according to MariaDB; we saw it on AlmaLinux 10): root is created as IDENTIFIED VIA mysql_native_password USING 'invalid' OR unix_socket. sudo mariadb works; a password login works only after you set one.
  • AlmaLinux with the MySQL 8.4 package from AppStream: the first start logged root@localhost is created with an empty password !. mysql -u root works until you set a password, so set one straight away.

To see the method:

$ sudo mysql -e "SELECT user,host,plugin FROM mysql.user WHERE user='root';"
user	host	plugin
root	localhost	auth_socket

(Lab output from Ubuntu 26.04.) If sudo mysql logs you in, you do not need a reset: go to step 2.

2. Change the root password (MySQL)

  1. Log in: sudo mysql or mysql -u root -p
  2. Set the new password:
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'New-Strong-Password';
  3. Test it in a new session: mysql -u root -p

On Ubuntu, where root uses auth_socket, you must name the plugin. Without it, the statement above reported no error, but root stayed on auth_socket with no password stored. This works:

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'New-Strong-Password';

After that, sudo mysql without a password answers ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO), and you log in with mysql -u root -p. To go back to password-free login for the Linux root user: ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;

Two other ways that worked in our tests: SET PASSWORD FOR 'root'@'localhost' = 'New-Strong-Password'; and mysqladmin -u root -p password 'New-Strong-Password', which asks for the current password first. Avoid typing passwords on the command line: MySQL warns Using a password on the command line interface can be insecure, and they end up in your shell history.

3. Change the root password (MariaDB)

To add a password and keep sudo mariadb working:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('New-Strong-Password');

Afterwards, sudo mariadb still logged in, and a normal Linux user could log in with mariadb -u root -p and the new password. A wrong password gave ERROR 1698 (28000): Access denied for user 'root'@'localhost'.

Be careful with ALTER USER 'root'@'localhost' IDENTIFIED BY '...' on MariaDB: it replaced the socket login with a password-only login, so sudo mariadb then failed with ERROR 1045. To set both methods explicitly:

ALTER USER 'root'@'localhost' IDENTIFIED VIA unix_socket
  OR mysql_native_password USING PASSWORD('New-Strong-Password');

4. Reset a lost MySQL root password

  1. Stop the server: sudo systemctl stop mysql (Ubuntu) or sudo systemctl stop mysqld (AlmaLinux, Rocky Linux and RHEL).
  2. On Ubuntu, recreate the socket folder, which systemd removes when the service stops:
    sudo mkdir -p /var/run/mysqld
    sudo chown mysql:mysql /var/run/mysqld
    Without it, the next step stopped with mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
  3. Start a temporary server without permission checks and without network access:
    # Ubuntu
    sudo mysqld_safe --skip-grant-tables --skip-networking &
    # AlmaLinux, Rocky Linux and RHEL (no mysqld_safe there)
    sudo -u mysql /usr/libexec/mysqld --skip-grant-tables --skip-networking &
  4. Connect with mysql -u root (no password) and run both statements:
    FLUSH PRIVILEGES;
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'New-Strong-Password';
    Without FLUSH PRIVILEGES first, MySQL refuses: ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement.
  5. Stop the temporary server with the new password: mysqladmin -u root -p shutdown
  6. Start the service normally (sudo systemctl start mysql or mysqld) and test with mysql -u root -p.

If step 4 answers ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded, root uses auth_socket and was never locked out. Stop the temporary server with sudo pkill -x mysqld, start the service normally and use sudo mysql as in step 2.

According to MySQL, an alternative is the --init-file option: put the ALTER USER line in a file readable by the mysql user, start mysqld --init-file=/path/to/file once, and delete the file afterwards. That also worked in our test.

5. Reset a lost MariaDB root password

MariaDB's systemd service passes the variable MYSQLD_OPTS to the server, so you can do it with systemctl:

sudo systemctl stop mariadb
sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking"
sudo systemctl start mariadb
sudo mariadb -u root
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED VIA unix_socket
  OR mysql_native_password USING PASSWORD('New-Strong-Password');
EXIT;
sudo systemctl stop mariadb
sudo systemctl unset-environment MYSQLD_OPTS
sudo systemctl start mariadb

Do not forget unset-environment, or the next restart runs without permission checks again. MariaDB also answered ERROR 1290 when FLUSH PRIVILEGES was missing.

On a cPanel server

According to cPanel, use WHM » Home » SQL Services » MySQL Root Password: enter and confirm the new password and click Change Password. cPanel stores it in /root/.my.cnf, which you should not edit by hand, and its documentation warns against asterisks, full stops and other special characters in this password because phpMyAdmin then stops working. Looking for the server's root login instead? See change the root password through WHM or change the VPS root password in the client area.

Still denied?

Check that you use the right account: 'root'@'localhost' and 'root'@'127.0.0.1' can be different accounts. See ERROR 1045 when logging in to MySQL and access denied for root@localhost after installing MySQL. To create a separate account for your applications instead of using root, see create a MySQL user and grant permissions.

Official documentation: How to reset the root password (MySQL 8.4) and MariaDB authentication.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Managed cPanel VPS

cPanel included; we handle updates, security and monitoring.

Server management

on-demand administration, hardening, migrations and monitoring.

Was this answer helpful?

Still stuck? Open a ticket