How to Reset the MySQL or MariaDB Root Password

MySQL and MariaDB 2 min readUpdated Oct 29, 2026
4 views0 comments

Forgot the root password of your database server? You can reset it if you have root access to the machine itself. The idea is simple: start the database for a moment without password checks and without network access, set a new password, then restart it normally. This works for MySQL 5.7 and 8 and for MariaDB 10.2 and newer.

While password checks are off, anyone who can log in to the server can access the database. That is why we also turn off networking and keep this window as short as possible.

Before you start: check the easy way

On many Ubuntu, Debian and MariaDB installs the database root account uses socket authentication: it does not want a password at all, it trusts the Linux root user. Try:

sudo mysql

If you get a prompt, you are in. Set a password from there with the ALTER USER statement in step 5 and you are done. If you get “Access denied”, continue below.

Step 1: Stop the database

sudo systemctl stop mysqld        # MySQL on RHEL, AlmaLinux, Rocky
sudo systemctl stop mysql         # MySQL on Debian / Ubuntu
sudo systemctl stop mariadb       # MariaDB

Use the one that matches your system. The same service name is used in the next steps.

Step 2: Start it in safe mode

sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking"
sudo systemctl start mysqld
  • --skip-grant-tables turns off password checks.
  • --skip-networking allows local connections only.

Step 3: Log in without a password

mysql -u root

Step 4: Reload the permission tables

With grant tables disabled, account statements are blocked until you reload them:

FLUSH PRIVILEGES;

Step 5: Set the new password

ALTER USER 'root'@'localhost' IDENTIFIED BY 'A-long-new-password-here';
EXIT;

Pick a long passphrase and store it in a password manager.

Step 6: Go back to normal mode

sudo systemctl unset-environment MYSQLD_OPTS
sudo systemctl restart mysqld

Test it:

mysql -u root -p

If something goes wrong

Symptom Fix
ALTER USER says the account does not exist List accounts with SELECT user, host, plugin FROM mysql.user; and use the real host value, for example 'root'@'127.0.0.1'.
“Can’t connect to local MySQL server through socket” The service did not start. Check sudo journalctl -u mysqld -n 50.
Still in safe mode after a reboot You forgot unset-environment. Run it and restart the service.
Root works with sudo mysql but not with a password The account uses socket authentication. That is normal and secure; create a separate admin user for applications.

Good habits afterwards

  • Applications should never use root. Create a user per application with only the rights it needs.
  • Keep the database port closed to the internet and allow only the machines that need it.
Did you find this helpful?Tap the heart to let us know. It takes one second and no account.

Leave a Comment

Your email address will not be published. Required fields are marked *

Bigland Network IT

Practical tutorials for people who run, build and fix things: operating systems, databases, development, networking and security.

Topics

More

Site

© 2026 Bigland Network. All rights reserved.Always test commands on a non-production system first.
Scroll to Top