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
orSET 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 | You control server and need to preserve definer |
Use | Quick workaround for Frappe users |