Skip to content
Tarisu Limited

Reset a Forgotten MySQL or MariaDB Root Password Safely

Recover the MySQL or MariaDB root account on your own Ubuntu, Debian or AlmaLinux server. This guide covers backups, skip-grant-tables with networking disabled, exact commands and troubleshooting.

Guides 8 min read Tarisu Limited

Reset a Forgotten MySQL or MariaDB Root Password Safely

When this guide applies

This guide is for people who administer their own server, VPS or dedicated machine and have lost the MySQL or MariaDB root password. It assumes you still have sudo access through SSH or a provider console. It covers MySQL 8.0/8.4 and MariaDB 10.6 or newer on Ubuntu 22.04/24.04, Debian 12 and AlmaLinux 9. If you are on shared web hosting or a managed database, you normally cannot start MySQL with --skip-grant-tables; ask your provider to reset the database user or use the control panel.

Do not use this method on a server you do not own. Keep a backup first. The temporary mode described below disables authentication, so it must never be left running and must never listen on a public interface.

Before you start

You need shell access as root or a user with sudo, a console or rescue session if the machine is remote, and a maintenance window because applications will fail to connect while MySQL is stopped. If you have a Tarisu VPS or dedicated server and have lost SSH as well, use the provider console or support channel before changing database files.

Back up first

If you can still log in to MySQL, make a logical backup:

sudo mysqldump -uroot -p --all-databases --single-transaction --routines --triggers > /root/mysql-backup.sql

If you cannot log in, stop the database and copy the data directory. The default is /var/lib/mysql; confirm it from your configuration if you changed it.

sudo systemctl stop mysql
sudo tar -czf /root/mysql-datadir-$(date +%F).tar.gz /var/lib/mysql

Replace mysql with mariadb or mysqld if that is the service on your system. A raw data-directory copy is only useful if you keep the same server version and configuration; test restores when possible.

Identify the service name

Distribution and serverStop commandStart command
Ubuntu 22.04/24.04, MySQL 8.0sudo systemctl stop mysqlsudo systemctl start mysql
Debian 12, MariaDB 10.11sudo systemctl stop mariadbsudo systemctl start mariadb
AlmaLinux 9, MySQL 8.0/8.4sudo systemctl stop mysqldsudo systemctl start mysqld
AlmaLinux 9, MariaDB 10.6+sudo systemctl stop mariadbsudo systemctl start mariadb

If you are unsure, run sudo systemctl status mysql mariadb mysqld 2>/dev/null and note which unit is active. Do not run the stop command twice for the wrong service.

Reset the root password

1. Stop the normal database service

Use the stop command from the table. Confirm that no database process is still listening on port 3306:

sudo ss -lntp | grep 3306

You should see no MySQL or MariaDB process after the service stops. If a process remains, check its command line before killing it; do not stop unrelated services.

2. Start a temporary server without grants and without networking

The two important options are --skip-grant-tables, which disables authentication, and --skip-networking, which blocks TCP access. Start the server manually rather than through systemd so it is easy to stop.

For MySQL on Ubuntu, Debian or AlmaLinux:

sudo nohup /usr/sbin/mysqld --skip-grant-tables --skip-networking --user=mysql >/tmp/mysql-skip.log 2>&1 &

For MariaDB on Ubuntu or Debian:

sudo nohup /usr/sbin/mariadbd --skip-grant-tables --skip-networking --user=mysql >/tmp/mariadb-skip.log 2>&1 &

For MariaDB on AlmaLinux 9, the server binary is usually under /usr/libexec:

sudo nohup /usr/libexec/mariadbd --skip-grant-tables --skip-networking --user=mysql >/tmp/mariadb-skip.log 2>&1 &

Check the log if the command returns immediately:

sudo tail -n 50 /tmp/mysql-skip.log

Use /tmp/mariadb-skip.log for MariaDB. The server is ready when you can connect through the local socket.

3. Connect and load the grant tables

Connect as root without a password. For MySQL use mysql; for MariaDB use mariadb.

sudo mysql -uroot

MariaDB:

sudo mariadb -uroot

At the prompt, reload the privilege tables and inspect the root accounts:

FLUSH PRIVILEGES;
SELECT user, host, plugin FROM mysql.user WHERE user = 'root';

You should see one or more rows such as root at localhost. The plugin column tells you how that account authenticates. On MySQL 8 it is often caching_sha2_password; on MariaDB it may be unix_socket or mysql_native_password. Reset the exact user and host shown, not every root row.

4. Set the new password

For MySQL 8.0/8.4 and MariaDB 10.4 or newer, use ALTER USER:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'Replace-With-Strong-Pass';
FLUSH PRIVILEGES;
\q

If your earlier query showed root at another host, substitute that host, for example root at 127.0.0.1. Use a long unique password and keep it in a password manager. Do not paste it into a public ticket or chat.

5. Stop the temporary server

Because grants are loaded again, shut down with the new password:

sudo mysqladmin -uroot -p shutdown

MariaDB:

sudo mariadb-admin -uroot -p shutdown

If shutdown is refused, find only the temporary process:

sudo pgrep -af 'mysqld.*skip-grant-tables'

Then stop that exact PID with sudo kill PID. Do not use a broad pkill mysqld on a server that may have another instance.

6. Start the normal service and test

sudo systemctl start mysql

Use mariadb or mysqld where appropriate. Then log in with the new password:

mysql -uroot -p -e 'SELECT VERSION();'

MariaDB:

mariadb -uroot -p -e 'SELECT VERSION();'

You should see the server version and no authentication error. Check the service state and process list:

sudo systemctl status mysql --no-pager
sudo pgrep -af 'mysqld|mariadbd'

There should be no --skip-grant-tables process. Finally, restart your application and confirm that its database connection works. If the application uses a separate database user, its password is unchanged; update only that user if you also changed it.

Troubleshooting common errors

Access denied for user root at localhost

This usually means the password was set for a different host, or the client is connecting as a different account. Reconnect to the temporary server and inspect the rows:

SELECT user, host, plugin FROM mysql.user;

Use the exact user and host. Do not run ALTER USER 'root'@'%' unless you deliberately created that account. If MySQL 8 uses caching_sha2_password and an old client cannot connect, update the client or use a supported TLS connection; do not weaken the server just to make an old application work.

Can't connect through socket

If the client cannot find the socket path reported in the error, often /var/run/mysqld/mysqld.sock on Debian/Ubuntu or /var/lib/mysql/mysql.sock on AlmaLinux, the temporary server may have failed to create it. Check the log and the data-directory permissions:

sudo tail -n 50 /tmp/mysql-skip.log
sudo ls -ld /var/run/mysqld /var/lib/mysql

The runtime directory should be writable by the mysql user. If you restored a backup and see permission errors, correct ownership only after confirming the original owner:

sudo chown -R mysql:mysql /var/lib/mysql

Port 3306 already in use

If the normal server did not fully stop, the temporary server cannot bind. Find the process:

sudo ss -lntp | grep 3306

Stop the service again, wait a few seconds, and retry. If the service will not stop, inspect sudo journalctl -u mysql -n 50 --no-pager or the MariaDB equivalent before forcing a kill.

ALTER USER failed

A failed ALTER USER often means the user does not exist at that exact host, or you did not run FLUSH PRIVILEGES first. Confirm the row from mysql.user. If the root account is missing, stop and restore from backup or ask your provider; do not guess a new root account on a production server.

The application still cannot connect

The application normally uses an application user, not root. Check its host and password in the application configuration, then reset that user if needed:

ALTER USER 'appuser'@'localhost' IDENTIFIED BY 'New-App-Pass';
FLUSH PRIVILEGES;

Keep the application password out of source control and rotate it if it may have been exposed.

Security notes after the reset

  • Never leave --skip-grant-tables running. It disables authentication and is unsafe on any reachable server.
  • Always combine it with --skip-networking during recovery, and use a console or SSH session rather than exposing MySQL to the internet.
  • Use a unique root password, store it in a password manager, and update any scripts or configuration files that used the old password.
  • Review accounts and privileges after the incident:
SELECT user, host, plugin FROM mysql.user;
SHOW GRANTS FOR 'root'@'localhost';
  • Check authentication logs and application logs for unexpected access. If the server was exposed while grants were disabled, treat the incident as a possible compromise and rotate all database credentials.
  • Keep the pre-change backup and the post-change dump. A reset is not a backup; only a tested restore is a recovery plan.

Frequently asked questions

Can I do this on shared hosting?

Usually not. Shared hosting normally does not give you the ability to start MySQL with --skip-grant-tables. Use the provider's database tool or contact support. On a Tarisu web hosting account, open a support request if you cannot access the database user through the control panel.

Does this work for MySQL 8.4?

Yes. The ALTER USER syntax is the same for MySQL 8.0 and 8.4. Service names may differ, so use the table above and confirm the active unit before stopping anything.

What if root uses the auth_socket plugin?

If sudo mysql already works without a password, you do not need --skip-grant-tables. Connect and run the ALTER USER statement directly. If socket authentication was removed or the account no longer works, use the temporary-server method.

Why do I need FLUSH PRIVILEGES twice?

The first reload lets the temporary server use the grant tables so ALTER USER can run. The second reload makes the new password effective before you shut down. Skipping them is a common cause of access-denied errors.

Will this change the application user's password?

No. This procedure changes only the root account and host you name. If the application uses another user, its password remains as it was unless you change it separately.

What if I do not have sudo access?

Stop and contact the server owner or hosting provider. Password recovery requires administrative control of the machine; it cannot be done from an ordinary website account.

Related posts