Change MySQL Root Password
This guide explains how to reset a lost or forgotten root password for MySQL or MariaDB on your Linux server. You temporarily start the database without permission checks, set a new password and then restart the service normally.
Warning: This procedure involves restarting your database server and temporarily bypassing its normal security checks. Perform these steps with caution, preferably during a maintenance window with no active database connections.
Step 1: Restart MySQL in Safe Mode
Stop the running MySQL service and restart it with the --skip-grant-tables option. This disables permission checks and allows you to log in without a password; --skip-networking blocks network access in the meantime. With MariaDB on Debian or Ubuntu, the service is called mariadb — and root can often log in as the system user root via mysql without a password, so try this first.
The first line creates the socket directory, which is removed when the service stops. The & at the end runs the process in the background so you can continue using the terminal.
Step 2: Connect to MySQL
Once the server is running in safe mode, connect as the root user — no password is required at this point.
Step 3: Set the New Password
Run the following SQL commands to update the root password. FLUSH PRIVILEGES reloads the permission tables first — without it, ALTER USER fails in safe mode. Replace MyNewPass with your own strong password.
The commands below apply to MySQL 5.7.6+ and MariaDB 10.1.20+. Older versions used UPDATE mysql.user SET Password=..., which is deprecated and should no longer be used.
Step 4: Restart MySQL Normally
Stop the manually started safe-mode process (you will be asked for the new password) and bring MySQL back up normally so that all security checks are re-enabled.
Your MySQL root password has now been changed and the server is running normally with all security checks active.
Further Documentation
For advanced MySQL configuration and administration, refer to the official documentation.