Using MySQL Triggers and Views in Amazon RDS
Join the DZone community and get the full member experience.Join For Free
originally written by michael coburn
i recently had an opportunity to migrate a customer from a physical server into amazon’s rds environment. in this particular case the customers’ platform makes extensive use of mysql triggers and views. i came across two significant issues that prevented me from following amazon’s documentation, which basically states “use mysqldump” but doesn’t call out a specific method of dealing with mysql triggers and views.
amazon relational database service (amazon rds) is a great platform if you’re looking for complete hands-off management of your mysql environment, but comes at a cost in the area of flexibility, i.e. you don’t have super privilege and this brings up additional challenges.
- you need to ensure you set log_bin_trust_function_creators=1 ( by default this is off, 0).
- you need to clean up your mysqldump syntax.
is easy, you simply make a configuration change within the amazon rds
gui on the node’s parameter group to set
log_bin_trust_function_creators=1 and then a restart of your amazon rds
node. the restart is required since without the super privilege you
lose access to changing dynamic variables on the fly.
#2 is a little more complex. if you go with vanilla mysqldump (from say a 5.5 mysqldump binary) on a schema that has triggers and views, you will see error 1227, something like this:
error 1227 (42000) at line 27311: access denied; you need (at least one of) the super privilege(s) for this operation
you’re seeing this message because mysql in amazon rds doesn’t provide the super privilege, and thus you cannot set up a trigger or view to run as a different user — only a user with super can do that.
mysqldump will generate syntax for a trigger like this:
delimiter ;; /*!50003 create*/ /*!50017 definer=`root`@`%`*/ /*!50003 trigger `after_insert_lead` after insert on `leads` for each row begin update analytics.mapping set id_lead = new.id_lead where mc_email = new.email; end */;; delimiter ;
and for a view like this:
/*!50001 create algorithm=undefined */ /*!50013 definer=`web`@`%` sql security definer */ /*!50001 view `admin_user_view` as select ...
the problem is in the “definer” lines.
here’s one method that worked for me:
- identify all the definer lines in your schema. i found it helpful to dump out a –no-data and then weed through that to get a unique list of the definer lines
- create a sed line for each unique definer line (see my example in a moment)
- include this sed line in your dump/load script
here’s what my sed matches looked like:
sed -e 's//*!50017 definer=`root`@`localhost`*///' -e 's//*!50017 definer=`root`@`%`*///' -e 's//*!50017 definer=`web`@`%`*///' -e 's//*!50017 definer=`cron`@`%`*///' -e 's//*!50013 definer=`cron`@`%` sql security definer *///' -e 's//*!50013 definer=`root`@`localhost` sql security definer *///' -e 's//*!50013 definer=`root`@`%` sql security definer *///' -e 's//*!50013 definer=`web`@`%` sql security definer *///'
note: the example above won’t directly work due to wordpress “helpfully” stripping my text… you need to escape the forward slashes and asterisks.
a big caveat: this method is akin to a brute force method of getting your data into amazon rds — you’ve lost the elegance & security of running your triggers and views as separate defined users within the database — they are all now going to run as the user you loaded them in as. if this is a show-stopper for you, contact percona and i’d be happy to take on your case and develop a more comprehensive solution.
now all that’s left is to integrate this into your dump flow. something like this should work:
mysqldump --host=source | sed -e ... lots of lines | mysql --host=destination
i hope this helps someone!
Published at DZone with permission of Peter Zaitsev, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.