0

I want to create admin user from the root directory Is it possible? How can I create a new admin user without CLI (SSH) and database?

2

Please create one file named "user.php" in your root of magento and copy paste below code in your file

<?php
use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

$params = $_SERVER;
 
$bootstrap = Bootstrap::create(BP, $params);
 
$objectManager = $bootstrap->getObjectManager();
$userFactory = $objectManager->get('\Magento\User\Model\UserFactory');

try{
    $adminInfo = [
        'username'  => 'magento',
        'firstname' => 'magento',
        'lastname'    => 'magento',
        'email'     => 'magento@helloworld.com',
        'password'  =>'magento@123',       
        'interface_locale' => 'en_US',
        'is_active' => 1
    ];
    $userModel = $userFactory->create();
    $userModel->setData($adminInfo);
    $userModel->setRoleId(1);
    $userModel->save(); 
    echo "User is sucessfully created!";
} catch (\Exception $ex) {
    echo $ex->getMessage();
    exit;
}

Run this script in browser or you can also run this file in CLI. After creation of user please go to admin panel and try to login with the below credentials as per mentioned in script.

Username :- magento
Password :- magento@123
1
  • Could you please approve my answer too? – ZealousWeb Feb 10 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.