Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change 'root' password in MySQL5.7

Tags:

mysql

I have forgotten my "Root" password of "MySQL" on my Windows machine. I tried using this link, But getting the error message(Pic attached). Can anyone help me out from this?

enter image description here

Steps Followed:-

1:- Stopped "MySQL process" in my computer service section.

2:- Created a .txt file having data ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

3:- Renamed the file to mysql-init.txt & saved into C drive.

4:- Opened command prompt & used the command C:> cd C:\Program Files\MySQL\MySQL Server 5.7\bin

5:- Then used C:> mysqld --init-file=C:\mysql-init.txt

After then I am getting this error message in my console.

like image 465
6797023 Avatar asked Sep 18 '25 11:09

6797023


2 Answers

Here a little bit twist with MySQL Community Server 5.7 I share some steps, how to reset MySQL 5.7 root password or set password. It will work CentOS 7 and RHEL7 as well.

  1. Stop your databases
service mysqld stop
  1. Modify /etc/my.cnf file add skip-grant-tables
vi /etc/my.cnf
[mysqld] 
skip-grant-tables
  1. Start MySQL
service mysqld start
  1. Select MySQL default database
mysql -u root
mysql> use mysql;
  1. Set a new password
mysql> update user set authentication_string=PASSWORD("yourpassword") where User='root';
  1. Restart MySQL database
service mysqld restart
mysql -u root -p
like image 172
Narendra Kumar Avatar answered Sep 20 '25 03:09

Narendra Kumar


For Linux This process is if you have root access on your server

First things first. Log in as root and stop the mysql daemon. Now lets start up the mysql daemon and skip the grant tables which store the passwords.

mysqld_safe --skip-grant-tables

You should see mysqld start up successfully. If not, well you have bigger issues. Now you should be able to connect to mysql without a password.

mysql --user=root mysql

update user set Password=PASSWORD('new-password') where user='root';
flush privileges;
exit;

Now kill your running mysqld, then restart it normally. Let me know if you have a problem

For windows

  1. Stop your MySQL server completely. This can be done from Wamp(if you use it), or start “services.msc” using Run window, and stop the service there.

  2. Open your MS-DOS command prompt using “cmd” inside the Run window. Then go to your MySQL bin folder, such as C:\MySQL\bin. Path is different if you use Wamp.

  3. Execute the following command in the command prompt:

    mysqld.exe -u root --skip-grant-tables
    
  4. Leave the current MS-DOS command prompt as it is, and open a new MS-DOS command prompt window.

  5. Go to your MySQL bin folder again.

  6. Enter “mysql” and press enter.

  7. You should now have the MySQL command prompt working. Type “use mysql;” so that we switch to the “mysql” database.

  8. Execute the following command to update the password:

    UPDATE user SET Password = PASSWORD('your_new_passowrd') WHERE User ='root';
    

    However, you can now run almost any SQL command that you wish.

After you are finished close the first command prompt, and type “exit;” in the second command prompt.

You can now start the MySQL service. That’s it.

Take a look at this answer for more explanation from the discussions

like image 29
Douglas Hosea Avatar answered Sep 20 '25 03:09

Douglas Hosea