ERROR 1410 (42000): You are not allowed to create a user with GRANT

Error Message

ERROR 1410 (42000): You are not allowed to create a user with GRANT

Description

This error occurs when attempting to create a new MySQL user and grant privileges in a single statement using MySQL 8.0 or later versions. MySQL no longer allows the implicit creation of users through the GRANT statement, which was possible in earlier versions.

Causes

  • Using pre-MySQL 8.0 syntax to create users with GRANT
  • Trying to grant privileges to a non-existent user
  • Recently upgraded to MySQL 8.0 without updating scripts
  • Incorrectly specified user causing MySQL to attempt user creation
  • Using deprecated authentication methods
  • Lacking permissions to create users

Solutions

  1. Use the two-step approach:

    -- Step 1: Create the user
    CREATE USER 'username'@'hostname' IDENTIFIED BY 'password';
    
    -- Step 2: Grant privileges
    GRANT SELECT, INSERT, UPDATE ON database_name.* TO 'username'@'hostname';
    
    -- Apply the changes
    FLUSH PRIVILEGES;
  2. Verify user existence before granting:

    -- List all users
    SELECT user, host FROM mysql.user;
    
    -- Create the user only if needed
    CREATE USER IF NOT EXISTS 'username'@'hostname' IDENTIFIED BY 'password';
  3. Use compatible authentication plugins for older clients:

    -- Create user with mysql_native_password authentication
    CREATE USER 'username'@'hostname' IDENTIFIED WITH mysql_native_password BY 'password';
    
    -- Grant privileges
    GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'hostname';
    FLUSH PRIVILEGES;

Prevention

  1. Update scripts and applications for MySQL 8.0 compatibility:

    • Always create users explicitly with CREATE USER
    • Then grant privileges separately
  2. Use IF NOT EXISTS clause to prevent errors:

    CREATE USER IF NOT EXISTS 'username'@'hostname' IDENTIFIED BY 'password';
  3. Check user existence programmatically before executing statements:

    SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'username' AND host = 'hostname');
  4. Use version-specific code paths in applications that need to support multiple MySQL versions

Edit this page on GitHub