When in Drupal 8, the password for user #1 (the administrator) is lost and the email notification don’t work, it is possible to set the password via a database query.
You’ll need access to your hosting server’s shell and database in order to reset administrator’s password in Drupal.
Generate a new password
First, you have to generate a password hash that is valid for your site.
Execute the following commands from the command line, in the Drupal 8 root directory:
1 2 3 4 |
$ php core/scripts/password-hash.sh 'your-new-pass-here' password: your-new-pass-here hash: $S$Do7UQjqtEELNccdi92eCXcVJ2KnwUeHrSbK3YhFm8oR3lRJQbMB2 |
Be careful not to include more or less characters as the hash.
In case of Drupal 7 the correct path will be
1 |
$ php scripts/password-hash.sh 'your-new-pass-here' |
table is “users” instead of “user_field_data” and you don’t need to clean the cache.
Update the administrator’s password.
Now you need to update the user password, in our case we need to update the Administrator’s password thus UID for Administrator is 1 in both Drupal 7 and 8.
With the new password we need run the following SQL statement.
1 2 |
UPDATE users_field_data SET pass='$S$Do7UQjqtEELNccdi92eCXcVJ2KnwUeHrSbK3YhFm8oR3lRJQbMB2' WHERE uid = 1; |
Cleaning the Cache
At this point if you try to login in the Drupal 8 website you will rejected, it’s because the login system don’t read directly the table users_field_data instead of a cache for entities is used.
To flush the cache for a specific user entity with no compromise the rest of cache of your system you can use the following SQL statement.
1 2 |
DELETE FROM cache_entity WHERE cid = 'values:user:1'; |
Now you should be able to login successfully!