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 mariadbThen, inside the client:
FLUSH PRIVILEGES;
ALTER USER root@localhost IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('new-password');
exitThis 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 mariadbResetting the password in MySQL on AlmaLinux
sudo systemctl stop mysqld
sudo -u mysql /usr/libexec/mysqld --skip-grant-tables --skip-networking &
mysql -u rootThen, inside the client:
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-password';
exitStop the temporary instance and start the database as usual:
mysqladmin -u root -p shutdown
sudo systemctl start mysqldFor more details and other methods, see the MySQL documentation.
Didn’t find the answer? Ask our support team — we’ll help.