How to Create and Add Admin User With MySQL in WordPress

Sometimes it happens that you lock ourselves out of the WordPress admin dashboard because you either made changes in the database or the admin account was deleted due to an accident or technical issue.

To fix that we need to create and add new admin account directly in the database by using the MySQL syntax.

To do so, open your favourite database administration tool look for the SQL-Query import function.

In that case I use the popular adminer which is a database administration tool that consists of only one file which you can upload via FTP to your website root folder. Another popular tool is phpmyadmin which is alraedy installed on most website hosting servers.

SQL Query

Paste the code below into the SQL Query field of phpmyadmin or Adminer and change the name, username, mail and password which you need to create for the new user. (Bold parts)

INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`,
VALUES ('wpstaging', MD5('password123'), 'Rene Hermenau', '', '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');

How to create an admin user with SQL for WordPress
SQL Query

If your website is not using the default WordPress prefix _wp also change the prefix of of the database tables wp_userswp_usermeta, wp_capabilities and wp_user_level to the one your website is using.

So if your website is using the table prefix wpstg0_ the new table name for wp_capablities is wpstg0_capabilities.

After executing the function you will be able to access your WordPress dashboard again with that new user credentials.