← Volver al blog

How to Reset MySQL Root Password

  • Xernode
  • 1 min de lectura
  • 4 de abril de 2026
Database password root mysql database reset

Introduction

If you’ve forgotten your MySQL root password, you can reset it easily with a few commands. This guide shows you how to recover access to your MySQL server on Linux.

Step-by-step Guide

  1. Stop the MySQL service
    • sudo systemctl stop mysql
  2. Start MySQL in safe mode (skip grant tables)
    • sudo mysqld_safe --skip-grant-tables &n
      • Wait a few seconds for MySQL to start.
  3. Log in to MySQL without a password
    • mysql -u root
  4. Change the root password
    • For MySQL 5.7+ and MariaDB 10.1+:n
      • FLUSH PRIVILEGES;
      • ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
    • For older versions:n
      • FLUSH PRIVILEGES;
      • SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');
    • Replace newpassword with your desired password.
  5. Exit MySQL and restart the service
    • exit
    • sudo systemctl stop mysql
    • sudo systemctl start mysql
  6. Test the new root password
    • mysql -u root -pn
      • Enter your new password when prompted.

Recommendations

  • Use a strong, unique password for the root user.
  • Store your credentials securely.
  • Regularly back up your databases.

Call To Action

eed a secure VPS for your databases? Check out our hosting plans! https://xernode.com/#pricing

Related Guides