MySQL Error: Access Denied for DEFINER While restoring backup


MySQL Error: Access Denied for DEFINER

Error Message:

scss
ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER, SET USER privilege(s) for this operation

๐Ÿ“Œ Cause:

This error occurs when importing or executing SQL that includes a DEFINER clause like:

sql
/*!50017 DEFINER=`user`@`host`*/


๐Ÿ” Why this happens:

  • The specified DEFINER user does not exist on the server.

  • The current user running the import lacks SUPER or SET USER privileges (especially on MySQL 8.0+).

  • MySQL is attempting to enforce security boundaries for triggers, views, or procedures.


โœ… Fixes:

1. ๐Ÿงน Remove or Ignore the DEFINER Clause (Most Recommended)

SQL Method:

Edit the SQL dump and remove or comment out lines like:

sql
/*!50017 DEFINER=`user`@`host`*/

Command Line Method:

Filter during import using sed:

  • For .sql:

    bash
    sed 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/g' backup.sql | mysql -u root -p database_name
  • For .sql.gz:

    bash
    zcat backup.sql.gz | sed 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/g' | mysql -u root -p database_name

2. ๐Ÿง‘โ€๐Ÿ’ป Create the Missing DEFINER User

If you want to retain the DEFINER:

Example for agarwals@%:

sql
CREATE USER 'agarwals'@'%' IDENTIFIED BY 'your_password'; 
GRANT ALL PRIVILEGES ON *.* TO 'agarwals'@'%';

3. ๐Ÿ” Grant Required Privileges to the Current User

If you control the server and are importing as a different user:

For MySQL < 8.0.26:

sql
GRANT SUPER ON *.* TO 'your_user'@'your_host';

For MySQL โ‰ฅ 8.0.26:

sql
GRANT SET USER, TRIGGER ON *.* TO 'your_user'@'your_host';

4. ๐Ÿงฐ Frappe/ERPNext Specific: Restore Backup Without DEFINER

bash
gunzip -c backup.sql.gz | sed 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/g' > clean_backup.sql bench --site your-site-name restore clean_backup.sql

โœ… Summary Table

Fix Method

Use When

Remove DEFINER

Best for most use cases

Create DEFINER user

If you must keep original trigger ownership

Grant SUPER / SET USER

You control server and need to preserve definer

Use sed in Frappe restore

Quick workaround for Frappe users