Often when you are working on a local Drupal site you may forget your admin password and you haven't setup SMTP yet, in this case there are multiple ways to recover your admin password, this article we'll see how to reset the admin password by changing password in database and resetting password using drush.
Using SQL/Database
Prerequisite:
- Access to your drupal site's codebase i.e the project folder of your drupal site
 - Access to database which is used by your drupal site
 
Generating password hash
- Go to your site's document root and execute
 
php core/scripts/password-hash.sh 'newpasswd'
Replace 'newpasswd' with your password.
You will see the output similar to this:
password: newpasswd         hash: $S$Eno1EhlPNMqE2RfDOBT13tzGCAdN9PgKqJFGI.4sBSj1XgJfPH68
$S$Eno1EhlPNMqE2RfDOBT13tzGCAdN9PgKqJFGI.4sBSj1XgJfPH68 is the hash which we need to update in the database.
Updating password in database
You can either use GUI like phpmyadmin and change pass = $S$Eno1EhlPNMqE2RfDOBT13tzGCAdN9PgKqJFGI.4sBSj1XgJfPH68 in the users_field_data. 
The uid of admin is 1 by default in drupal. The same mechanism can be used to change password of any user by supplying the appropriate uid.
OR
You can use mysql command line and execute:
UPDATE users_field_data 
SET pass='$S$Eno1EhlPNMqE2RfDOBT13tzGCAdN9PgKqJFGI.4sBSj1XgJfPH68' 
WHERE uid = 1;
Clearing the cache
Even after updating the password in users_field_data table,may still won't allow you to login this is because the login system is referring to the password stored in cache_entity table and not users_field_data, so you will need to delete the record in cache_entity using gui, or by using mysql command line and executing:
DELETE FROM cache_entity WHERE cid = 'values:user:1';
For drupal 7 or older
Generating password hash
Go to your site's document root and execute
php scripts/password-hash.sh 'newpasswd'
Replace 'newpasswd' with your password.
Updating password in database
You need to make the changes mentioned for Drupal 8 or newer  in users table instead of users_field_data
If you have exceeded your login attempts you may need to delete corresponding records from flood table.
This flood table records username and ip which has failed login attempts.
Using Drush
If you have drush installed with your drupal installation you can easily reset the password for any user even the admin
There are 2 ways of using drush to change password:
Using drush to generate password reset link
You can generate a link to reset with the following command:
drush uli --name=<username>
or
drush uli --uid=<user id>
Replace  and  with the username and the id of the user whose password needs to get reset. If you do not specify uid or name drush will operate on the user with uid 1.
This will generate a one-time login link which looks like this:
http://default/user/reset/2/1604915661/Ua9e50at_ggrlLqr5ulcX39CbpLjLO85Tczhz8nbdCM/login
Replace default with your hostname or IP and log in.
Alternatively, you can use option --uri <hostname/IP> to specify the hostname or IP of your website and drush will generate the link for your site.
Copy this link on your browser and you will get the password reset screen.
Using drush to reset password through the command line
You can change the password of  someuser to somepass like this:
drush user:password someuser "somepass"
References:
Resetting the administrator password with sql-query in Drupal 8
Resetting the administrator password with sql-query (Drupal 7)
Recovering the administrator password
    
Top comments (1)
It worked!! Thank you!