Skip to content
RIPE NCC member · Local Internet RegistryCall us on 020 3318 3449sales@hosting-telecom.uk

Forgotten MySQL or MariaDB root password

Cloud servers2 min read

How to log in to the database as the administrator without a password, and how to reset the root password in MariaDB and MySQL on your own server.

Contents

Try without a password first

Often you do not need the root password at all: the server administrator logs in to the database through sudo.

  • MariaDB — on any system: sudo mariadb.
  • MySQL on Ubuntu: sudo mysql. If root has already been switched to password login, log in with the maintenance account: sudo mysql --defaults-file=/etc/mysql/debian.cnf.
  • MySQL on AlmaLinux straight after installation: root has an empty password, and you can log in with mysql -u root. Set a password straight away: sudo mysql_secure_installation.

Once you are in, set a new password with a single command:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-password';

For your website, create a separate user with access to one database — the website does not need the root password.

Resetting the password in MariaDB

If you cannot log in at all, start the database for a couple of minutes without password checks and without network access:

sudo systemctl stop mariadb
sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking"
sudo systemctl start mariadb
sudo mariadb

Then, inside the client:

FLUSH PRIVILEGES;
ALTER USER root@localhost IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('new-password');
exit

This lets root log in both through sudo and with a password. Then return to normal start-up:

sudo systemctl stop mariadb
sudo systemctl unset-environment MYSQLD_OPTS
sudo systemctl start mariadb

Resetting the password in MySQL on AlmaLinux

sudo systemctl stop mysqld
sudo -u mysql /usr/libexec/mysqld --skip-grant-tables --skip-networking &
mysql -u root

Then, inside the client:

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-password';
exit

Stop the temporary instance and start the database as usual:

mysqladmin -u root -p shutdown
sudo systemctl start mysqld

For more details and other methods, see the MySQL documentation.

Didn’t find the answer? Ask our support team — we’ll help.

Choose which cookies we may use. You can change your mind at any time with the “Cookie settings” link at the bottom of every page.

Necessary

Always active

They keep the site secure and remember your cookie choice. They don’t identify you.

Google Analytics counts visits and shows which pages people find useful, so we can improve the site. Which cookies