Creating Wordpress Admin User Directly from MySQL

Posted by Ray on January 26, 2024

A guide for creating wordpress administrator user directly from mysql database without logging into a wordpress admin.

Creating a User Directly from the Database

  1. SSH into the site’s host server.

  2. Type $ su to enable super user access, then type the super user’s root password.

  3. Enter the mysql shell, and use the project’s database:
    1
    2
    3
    
     $ mysql
     mysql> SHOW DATABASES;  # to check the project's database name
     mysql> USE <project_database>; # do not forget the semicolon
    
  4. Show the database tables to determine the table prefix. The default will be “wp_”
    1
    
     mysql> SHOW TABLES;
    
  5. Insert your user information through the command:
    1
    
     mysql> INSERT INTO <table_prefix>_users (user_login, user_pass, user_email, user_registered, user_status) VALUES ('<your_username>', '<your_password>', '<your_email>', '<date_time_today>', 0);
    

    The date time today format should be: yyyy-mm-dd hh:mm:ss

  6. Search for your username in the Users tables, and take a note of your ID. Then, add your user meta data.
    1
    2
    
         mysql> SELECT * FROM <table_prefix>_users;
         mysql> INSERT INTO <table_prefix>_usermeta (user_id, meta_key, meta_value) VALUES ('<your_user_id>', 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');
    
  7. Use online tools for wordpress password hashers (such as: https://www.useotools.com/wordpress-password-hash-generator) to generate a hash for your password.

  8. Copy the hashed password, and run the command:
    1
    
     mysql> UPDATE `<table_prefix>_users` SET `user_pass` = '<your_password_hash>' WHERE user_login = '<your_username>';