Change password root in MySQL on Redhat Centos

Change password root in MySQL on Redhat Centos

Method 1: If You Know the Current Root Password

Step-by-Step

  1. Log in to MySQL as root:

mysql -u root -p
  1. Change the root password:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewSecurePassword!'; FLUSH PRIVILEGES; EXIT;

Make sure to replace 'NewSecurePassword!' with a strong, secure password.

Method 2: If You Forgot the Root Password

Step-by-Step

  1. Stop the MySQL Service:

sudo systemctl stop mysqld
  1. Start MySQL in Safe Mode (no password check):

sudo mysqld_safe --skip-grant-tables &

Wait a few seconds after this command.

  1. Login without password:

mysql -u root
  1. Set a new root password:

FLUSH PRIVILEGES; ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewSecurePassword!'; EXIT;
  1. Stop the MySQL Safe Mode and Restart the Service:

sudo systemctl stop mysqld sudo systemctl start mysqld
  1. Test login with new password:

mysql -u root -p

Tips for Security

  • Use a strong password with upper/lowercase letters, numbers, and symbols.

  • Never leave the root account with a blank or weak password.

  • Restrict remote root access for added security.

Souy Soeng

Souy Soeng

Hi there šŸ‘‹, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
šŸ‘Æ I’m looking to collaborate on open-source PHP & JavaScript projects
šŸ’¬ Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close