How To Create a New Admin Account in WordPress Via Mysql

One day, I forgot my WordPress login account i.e. wp-admin username and password. I tried to change the username and password from the MySQL Database, but still, I couldn’t log in. I got locked out of my own account. While researching on the internet, I realized this could happen either via some code error, a hack, or an accidental admin account deletion, or even a malicious site partner changing information on the site.

However, I spent a little while looking into the right way to fix this issue and finally came up with a solution that I thought I’d share. You should create a  new admin account in order to regain access to the administration interface. For this,  You will need to interact directly with the database. This can be done by following steps:-

Note: You should backup database before performing any MySQL changes. This tutorial requires a basic knowledge of how phpMyAdmin works.


Create a New Admin Account in WordPress Via Mysql


Step 1: Login to your phpMyAdmin account with username and password. If you are logged in from Cpanel, you can directly access your phpMyadmin.

Step 2: Select  the database of your site and Select the SQL tab to run the Query.

Step 3: Now you will see SQL editor where you can run some query that will create a new admin account for you. Below is the code to create a new admin account named adminwith the password newpass.

INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_status`) VALUES ('admin', MD5('newpass'), 'FirstnameLastname', 'youremail@example.com', '0');

INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, (Select max(id) FROM wp_users), 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');

INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, (Select max(id) FROM wp_users), 'wp_user_level', '10');

IMPORTANT NOTICE : Make Sure You change your table prefix.

Step 5: After replacing the data fields you need, click the Go button to create your wp user admin.

Step 6: you should see the message 1 row affected after each of the three SQL statements. This means the insertion ran successfully. Now visit your WordPress admin login  as normal and use the new admin login information. You should get access to the admin interface without any issue.

VIOLA !! Now, you can log in with your new username and password via WordPress admin.

CONCLUSION

Always regularly backup your databases. Even my personal site got hacked a few month ago by the hacker. Thanks to him, he didn’t delete my database but just changed the user account password. I didn’t have a backup of my database. Although I have taken precautionary measures of my site for being hacked, I keep the regular backup of my sites. Also, regular malware scan of the site is necessary.

Speak your mind in the comment below, if you have any question.