Update Magento2 Admin Password with SQL

U

There have been more than enough times where I have set a password for a local environment where I have instantly forgotten what it was, and never had the chance for my password manager to save it for me. 

Careless I know.

I do not think there is a way that this can be update through bin/magento – as far as I am aware, you can only create a new user (bin/magento admin:user:create and then follow the prompts) and then reset the password through the admin panel of the original user where you had forgotten the password.

God forbid if your memory is like mine, you will end up with tens of new admin users simply created to reset the original admin user. So now I just use the following SQL statement to update the admin_user table directly.

First go into <MAGENTO_ROOT>app/etc/env.php and find the array key ‘crypt’. As it’s value you will find a string. Copy this string and update the following SQL statement with your crpyt string, followed by what you want your new password to be. See below where my new desired password is ‘myNewPassword’, and my admin user is ‘LocalAdmin’.

UPDATE admin_user SET password = CONCAT(SHA2('CRYPTSTRINGGOESHEREmyNewPassword', 256), ':CRYPTSTRINGGOESHERE:1') WHERE username = 'LocalAdmin';

Open up MySQL and select your database, run the script, and for god’s sake – take a note of the password is before you forget it again.

About the author

Matt

I am London based Ecommerce Business Analyst & Technical Lead. Always keen to chat.

By Matt