Skip to content

Instantly share code, notes, and snippets.

@munkyonline
Created March 23, 2021 01:46
Show Gist options
  • Save munkyonline/3dff9b6ad746a80d6d850f9b0191c7b7 to your computer and use it in GitHub Desktop.
Save munkyonline/3dff9b6ad746a80d6d850f9b0191c7b7 to your computer and use it in GitHub Desktop.
[Reset WP Password] Reset password via MySQL OR WP-Cli
Resetting the WordPress admin password through MySQL
Let’s begin by looking up the username and password you set for your WordPress database in your wp-config.php file first, do that by navigating to the directory WordPress is installed in and then open wp-config.php with nano:
# nano wp-config.php
Find the following lines in your wp-config.php file:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
Now that we have all the information we need we are going to open a MySQL command prompt with the following command:
# mysql -u username_here -p -d database_name_here
Enter the password when prompted and then enter the following query in the MySQL command prompt:
mysql> use 'database_name_here';
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
Look for the users table with the following query:
mysql> show tables LIKE '%users';
+---------------------------------------+
| Tables_in_database_name_here (%users) |
+---------------------------------------+
| wp_users |
+---------------------------------------+
1 row in set (0.00 sec)
Use the prefixed users table from the output of the query above in the following query, for an example we’ll use ‘wp_users’. We also use ‘admin’ as the administrator user, your administrator username might be different:
mysql> SELECT ID, user_login, user_pass FROM wp_users WHERE user_login = 'admin';
+----+------------+------------------------------------+
| ID | user_login | user_pass |
+----+------------+------------------------------------+
| 1 | admin | $P$BiD1utsVDNrPVFm7.wcwPGzc.rKbu5. |
+----+------------+------------------------------------+
1 row in set (0.00 sec)
The query above outputs a row containing the current encrypted WordPress password of the administrator user, we are going to change it using this query, be sure to substitute ‘new_password’ for your own password:
mysql> UPDATE wp_users SET user_pass=MD5('new_password') WHERE user_login = 'admin';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Now check if the password has changed:
mysql> SELECT ID, user_login, user_pass FROM wp_users WHERE user_login = 'admin';
+----+------------+----------------------------------+
| ID | user_login | user_pass |
+----+------------+----------------------------------+
| 1 | admin | 88162595c58939c4ae0b35f39892e6e7 |
+----+------------+----------------------------------+
1 row in set (0.00 sec)
Then type exit to exit the MySQL command prompt:
mysql> exit
Bye
Resetting the WordPress admin password through wp-cli
If you do not want to bother going the long route through MySQL you can change your WordPress administrator password with a neat tool called wp-cli which is considered the swiss army knife of WordPress. To download wp-cli execute the following command:
# curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Check if wp-cli is working:
# php wp-cli.phar --info
Then make wp-cli executable and move it to ‘/usr/local/bin’:
# chmod +x wp-cli.phar
# mv wp-cli.phar /usr/local/bin/wp
Now navigate to the root directory of your WordPress installation and then execute the following wp-cli command:
# wp user update admin --user-pass=new_password
Again, make sure that you replace ‘admin’ with your WordPress administrator username and ‘new_password’ with your desired password.
If you are running wp-cli as root then add the ‘–allow-root’ option at the end of the command like this:
# wp user update admin --user-pass=new_password --allow-root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment