Reset the MySQL root Password
sudo
/etc/init.d/mysql stop
This command stops the running MySQL service.
If you try to start MySQL in mysqld_safe mode without stopping the regular
service, you may encounter conflicts.
mkdir
/var/run/mysqld
This command creates the directory used by
MySQL for its socket file and other runtime data. If this directory doesn't
exist, MySQL might fail to start.
sudo
chown mysql /var/run/mysqld/
This command sets the ownership of
/var/run/mysqld to the mysql user. This is necessary to allow MySQL to create
and use files within this directory.
sudo
mysqld_safe --skip-grant-tables &
This command starts MySQL in "safe"
mode without enforcing user authentication or privileges. The & at the end
runs it in the background, allowing you to continue using the terminal.
sudo
mysql --user=root mysql
Since --skip-grant-tables is enabled, no
password is required. This command connects to MySQL as root and selects the
mysql database, which contains user information and privileges.
update
mysql.user set authentication_string=null where user='root';
clears the existing authentication string.
flush
privileges;
ensures the updated privileges take effect.
alter
user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
sets a new password for the root user. In this
case, the new password is 'root'.
Another
flush privileges; to finalize the changes.
Exiting
MySQL and Testing New Password:
bash
Copy code
exit
mysql -u
root -p
After
exiting MySQL, the mysql -u root -p command allows you to reconnect to MySQL
with the root user. It will prompt you for a password, where you should enter
'root'.
Comments
Post a Comment