Site icon Nuventure Blog

WordPress: How to create Admin users via MySQL commandline

A WordPress user can belong to any of the six pre-defined roles.

   • Super Admin – Has access to all features including site network administration.

   • Administrator – Has access to all the administration features within a single site.

   • Editor – Can publish and manage posts including the posts of other users.

   • Author – Can publish and manage their own posts.

   • Contributor – Can write and manage their own posts but cannot publish them.

   • Subscriber – Can only manage their profile.

Upon installing WordPress, an Administrator account is automatically created. Thus, every WordPress installation comes with an admin user.

In case a WordPress admin user loses their password, they can use the Lost your password? link and the related Email account for resetting the password.

But in some case, we may not have access to this Admin account, or may not want to reset the password, but would like another Admin account created for maintenance or troubleshooting purpose.

If you ever face such a scenario, we have laid out the necessary steps required to create a WordPress Admin user from MySQL commandline.

First, let us get the necessary information about the WordPress database.
This information is stored in the WordPress configuration file, which is named wp-config.php. Identify the location of wp-config.php in the server.

And in terminal, switch to the directory containing this configuration file.

List the Database Host, Database Name, Database User, Database Password, and WordPress table prefix from WordPress configuration file.

#  grep ‘DB_HOST\|DB_NAME\|DB_USER\|DB_PASSWORD\|$table_prefix’ wp-config.php


Next, login to the MySQL Server as WordPress Database user.

#  mysql -h $DB_HOST -u $DB_USER -p

mysql>  use $DB_NAME;


Execute the following MySQL commands to create a WordPress user.

mysql>   insert into `$DB_NAME`.`$table_prefix_users` (`id`, `user_login`, `user_pass`, `user_nicename`, `user_email`, `user_url`, `user_registered`, `user_activation_key`, `user_status`, `display_name`) values (‘$WP_ID’, ‘$WP_USER’, md5(‘$WP_PASSWORD’), ‘$WP_USER_NICE’, ‘$EMAIL’, ‘$WEBSITE’, ‘$DATE&TIME’, ”, ‘0’, ‘$WP_USER_DISPLAY’);

mysql>   insert into `$DB_NAME`.`$table_prefix_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) values (null, ‘$WP_ID’, ‘$table_prefix_capabilities’, ‘a:1:{s:13:”administrator”;s:1:”1″;}’);

And then set the User Level as 10 for the newly created WordPress user. This user level pertains to the Administrator role.

mysql>   insert into `$DB_NAME`.`$table_prefix_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) values (null, ‘$WP_ID’, ‘$table_prefix_user_level’, ’10’);

Exit from MySQL command line.

mysql>   exit


Finally, login in to the WordPress Dashboard using the new password, so that WordPress will automatically update the MD5 hash of password to a salted one.

Done.


Exit mobile version