How to create a WordPress admin user with Linux/MySQL commands

1. Locate the wp-config.php file and extract database name

WP keeps the MySQL connection details in the wp-config.php file. We want to extract the DB_NAME and table_prefix values that will help us in inserting the new user into the database.. Navigate to the WP installation folder and run:

 grep -i 'DB_NAME\|$table_prefix' wp-config.php

Example:

[root@web wp]# grep -i 'DB_NAME\|$table_prefix' wp-config.php
define( 'DB_NAME', 'plothost_wp795' );
$table_prefix = 'wpy2_';
[root@web wp]# 

In our case, the database name is plothost_wp795 and the table prefix is wpy2_ . We need these values for the next step.

2. Insert the new admin user into the MySQL/MariaDB database

Execute the following 3 commands to insert in the database the new user:

mysql -e "INSERT INTO WORDPRESSDATABASE.TABLEPREFIX_users (ID, user_login, user_pass, user_nicename, user_email, user_status, display_name,user_registered) VALUES ('101', 'USERNAME', MD5('PASSWORD'), 'USERNAME', 'EMAILADDRESS', '0', 'Admin101',now());"

mysql -e "INSERT INTO WORDPRESSDATABASE.TABLEPREFIX_usermeta (user_id, meta_key, meta_value) VALUES ('101', 'TABLEPREFIX_user_level', '10');"

mysql -e "INSERT INTO WORDPRESSDATABASE.TABLEPREFIX_usermeta (user_id, meta_key, meta_value) VALUES ('101', 'TABLEPREFIX_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');"

WORDPRESSDATABASE is the DB_NAME value from the previous step.
TABLEPREFIX is the $table_prefix value from the previous step.
USERNAME is a user name of your choice for WP.
EMAILADDRESS is the email address you want to use for the new account.

Example for our WordPress test installation:

# mysql -e "INSERT INTO plothost_wp795.wpy2_users (ID, user_login, user_pass, user_nicename, user_email, user_status, display_name,user_registered) VALUES ('101', 'plothost', MD5('25odqkmcExk27bSuc'), 'plothost', 'admin@plothost.com', '0', 'Admin101',now());"

# mysql -e "INSERT INTO plothost_wp795.wpy2_usermeta (user_id, meta_key, meta_value) VALUES ('101', 'wpy2_user_level', '10');"

# mysql -e "INSERT INTO plothost_wp795.wpy2_usermeta (user_id, meta_key, meta_value) VALUES ('101', 'wpy2_capabilities', 'a:1:{s:13:\"administrator\";b:1;}');"

In the code boxes above, the bold values are the values that you need to modify for your situation.

Resources:
Roles and Capabilities
mysql -e command

Related articles:
How to reset a WordPress password from command-line

Leave a Reply