Check Socket Login First
sudo mysql
sudo mariadbOn Ubuntu, MySQL's root account uses the auth_ plugin, and MariaDB's root uses unix_ by default. The server checks that the Linux user is root; there is no password. If one of these commands opens the client, no reset is needed: you already have root access. An error such as access denied for 'root'@'localhost' when you run mysql -u root without sudo is expected in that setup and is not a lost password; see MySQL access denied for user.
Also check whether another administrative account still works, for example one used by a backup tool. With it you can run ALTER USER without any of the steps below.
Before You Reset
- The server is open while grant tables are skipped: anyone on the machine can connect without a password and change anything. Keep this window short.
- Networking must be off:
--skip-networkingcloses TCP, so nobody can connect from outside during that time. MySQL 8.0 and later turn it on by themselves with skipped grant tables; MariaDB does not. Always add it. - The databases go down: stop the application or plan a maintenance window, and take a server snapshot first.
Step 1: Stop the Service
| Server | Command |
|---|---|
| MySQL on Ubuntu | sudo systemctl stop mysql |
| MySQL on Rocky, Alma | sudo systemctl stop mysqld |
| MariaDB | sudo systemctl stop mariadb |
Step 2: Start Without Grant Tables
On Ubuntu and Debian, create the socket folder, then start the server by hand as the mysql user:
sudo mkdir -p /run/mysqld
sudo chown mysql:mysql /run/mysqld
sudo -u mysql /usr/sbin/mysqld --skip-grant-tables --skip-networking &For MariaDB, the program is /usr/sbin/mariadbd. On AlmaLinux and Rocky Linux, the same /usr/sbin names are links to the programs in /usr/libexec, and the socket lives in /var/lib/mysql, so the first two lines are not needed there. Wait a few seconds for the start-up messages to finish.
Step 3: Set the Password
sudo mysqlInside the client, load the grant tables first, then change the account. On MySQL 8.x:
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'STRONG_PASSWORD';On MariaDB, keep socket login and add a password next to it:
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('STRONG_PASSWORD');Replace STRONG_ inside the client, never on a command line. On Ubuntu's MySQL, you can instead restore socket login with IDENTIFIED WITH auth_, which needs no password at all.
Step 4: Restart Normally
Still in the same client session, stop the server you started by hand, then leave the client and start the service:
SHUTDOWN;
EXIT;
sudo systemctl start mysqlThe last line runs in the shell. Use mariadb or mysqld as the service name where that applies. Check with pgrep -a mysqld (or mariadbd) that the manual process has ended before you start the service; if SHUTDOWN was refused, end it with sudo kill and that process ID, a normal kill rather than kill -9. Then confirm the service is active and test the login with mysql -u root -p.
The Init File Method
MySQL's manual also describes a reset with an init file: a text file holding the ALTER USER statement, passed to the server with --init-file at start. The server runs it and starts with grant tables enabled. The file holds the new password in plain text, so make it owned by the mysql user with mode 600, start the server with --skip-networking as well, and delete the file as soon as the server is up. On Ubuntu, the AppArmor profile limits which folders mysqld may read, so a file in a home directory can be refused; the error log then shows why. Restart normally afterward.
If the Server Does Not Start
Read the error log, /var/log/mysql/error.log on Ubuntu's MySQL, or sudo journalctl -u mariadb, as described in Linux log files. A socket error from the client is covered in MySQL error 2002. Never delete files in /var/lib/mysql to make it start.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations