Add an admin account in WordPress via MySQL

There can be times when your WordPress admin user is hijacked or your account is locked out accidental. In such case, you may need to add a new admin user for your WordPress installation.

In this article, we will consider that you have cPanel hosting control panel. However the steps can also work for you if you have different hosting control or no control panel. You just need the interface to access your MySQL database to execute few queries.

Add an admin account in WordPress via MySQL

We will assume that your database table prefix is set to “wp_“. If you are using different database prefix, then you will need to adjust your query accordingly.

[1] First, login to your cPanel control panel.

[2] From your cPanel, go to “Databases” section and click on phpMyAdmin as shown in following image:

phpMyAdmin
phpMyAdmin

[3] In phpMyAdmin, click on your WordPress database name and go to “SQL” tab as shown below:

MySQL database SQL query
MySQL database SQL query

[4] In SQL query editor, execute the following three queries:

INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_status`)
VALUES ('new-user', MD5('your-password'), 'firstname lastname', '[email protected]', '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');

Replace new-user and your-password in the first query and then click on “Go” button.

[5] This will refresh the screen and you will see “1 row affected” after each SQL query.

You should be able to login to your WordPress admin area using the username and password which you have mentioned in step [4].

Leave a Reply