Updated: 28 September 2026 · Applies to: Ubuntu 24.04, Debian 12, AlmaLinux 9 and Rocky Linux 9

Ubuntu 18.04 is end of life; these steps are for MariaDB 10.11 and MySQL 8.0 on Ubuntu 24.04 / Debian 12, and also work on AlmaLinux 9 / Rocky Linux 9.

First try: log in through the system root account

On Ubuntu, Debian and AlmaLinux/Rocky, the database root user normally logs in with the system root account, without a password:

sudo mariadb        # or: sudo mysql

If that works, set or change the password there (see step 4 below).

Reset the password

  1. Stop the database: systemctl stop mariadb (MySQL: systemctl stop mysql, or mysqld on AlmaLinux/Rocky).
  2. Start it without permission checks and without network access: mariadbd-safe --skip-grant-tables --skip-networking & (MySQL: mysqld_safe --skip-grant-tables --skip-networking &; on Ubuntu create /var/run/mysqld owned by mysql first if it complains).
  3. Connect: mariadb -u root.
  4. Reload the permission tables and set the new password:
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'New-Strong-Password';
EXIT;
  1. Stop the temporary server: mariadb-admin -u root -p shutdown (or kill $(pidof mariadbd)).
  2. Start the database normally: systemctl start mariadb, and test with mariadb -u root -p.

Note: On MariaDB, ALTER USER … IDENTIFIED BY replaces the socket login with a password. To keep both (password and sudo mariadb), use: ALTER USER root@localhost IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('New-Strong-Password'); On cPanel servers, change the root password in WHM instead, so cPanel keeps working.

Related services

KVM VPS hosting

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

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Managed dedicated servers

our engineers look after the server for you, from $135 a month.

Was this answer helpful?

Still stuck? Open a ticket