Fixing MySQL DEFINER Errors on AWS RDS (Trigger Import)
When importing a MySQL dump into Amazon RDS, you might hit an error like:
|
1 2 |
ERROR 1227 (42000) at line 1278: Access denied; you need (at least one of) the SUPER or SET_USER_ID privilege(s) for this operation |
In the dump you’ll often see a trigger, view, or routine created with a DEFINER that doesn’t match your current user, for example:
|
1 2 3 4 5 6 7 8 9 |
/*!50017 DEFINER=`dx_stage_db_user`@`%`*/ /*!50003 TRIGGER before_insert_tbl_user BEFORE INSERT ON tbl_user FOR EACH ROW BEGIN DECLARE max_serial INT; SELECT IFNULL(MAX(WWID), 0) + 1 INTO max_serial FROM tbl_user; SET NEW.WWID = max_serial; END */ |
On RDS you typically don’t have SUPER (legacy) or SET_USER_ID (modern) privileges, so MySQL refuses to create the object with someone else’s DEFINER. TL;DR (the quick, safe fix) Strip the DEFINER=… clauses from the dump so MySQL defaults to the importing user.
|
1 2 3 4 5 6 7 8 9 |
# 1) Make a copy cp db.sql db.nodef.sql # 2) Remove all DEFINER=... (handles triggers, views, routines, events) sed -E -i 's/DEFINER=`[^`]+`@`[^`]+`//g' db.nodef.sql # 3) Import the cleaned file mysql -h $DB_HOST -u $DB_USER -p $DB_NAME < db.nodef.sql |
Notes The versioned comments like /*!50017 … */ are fine to keep. Removing only the DEFINER=… text is enough. On macOS, use sed -E -i ” ‘s/…/…/g’ file.sql (empty string after -i). Alternative: keep an explicit definer (without elevated privileges) If you prefer to keep an explicit definer, rewrite to CURRENT_USER:
|
1 2 |
sed -E -i 's/DEFINER=`[^`]+`@`[^`]+`/DEFINER=CURRENT_USER/g' db.sql |
CURRENT_USER is allowed in object definers and avoids the privilege requirement. Why this happens (in one