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
- Stop the database:
systemctl stop mariadb(MySQL:systemctl stop mysql, ormysqldon AlmaLinux/Rocky). - 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/mysqldowned by mysql first if it complains). - Connect:
mariadb -u root. - Reload the permission tables and set the new password:
FLUSH PRIVILEGES; ALTER USER 'root'@'localhost' IDENTIFIED BY 'New-Strong-Password'; EXIT;
- Stop the temporary server:
mariadb-admin -u root -p shutdown(orkill $(pidof mariadbd)). - Start the database normally:
systemctl start mariadb, and test withmariadb -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.