When setting up a project with an existing database, you might encounter an issue when trying to create a new admin user using the following command:
php bin/magento admin:user:create
You could see an error message like this:
No Administrators role was found, data fixture needs to be run
How to Resolve the Issue
To fix this error, you need to execute a few MySQL commands to set up the necessary roles and clear any existing admin users. Run the following commands in your MySQL client:
-- Create the Administrators role
INSERT INTO authorization_role
(role_id, parent_id, tree_level, sort_order, role_type, user_id, user_type, role_name)
VALUES
(1, 0, 1, 1, 'G', 0, '2', 'Administrators');
-- Grant full access to the Administrators role
INSERT INTO authorization_rule
(rule_id, role_id, resource_id, privileges, permission)
VALUES
(1, 1, 'Magento_Backend::all', null, 'allow');
-- Remove any existing admin users
DELETE FROM admin_user;
After running these commands, try executing the admin:user:create command again. The new admin user should now be created without any issues.
Happy coding!