ChatGPT解决这个技术问题 Extra ChatGPT

Can I CREATE TRIGGER in an rds DB?

I'm trying to create a trigger on a table in my Amazon RDS database, and I can't seem to make it happen.

I tried to create a trigger on a table in the mysql client I use (Navicat), and got the error that I needed the SUPER privilege to do so. After some searching, I found that you could SET GLOBAL log_bin_trust_function_creators = 1 to get around this. I tried that using these instructions: http://getasysadmin.com/2011/06/amazon-rds-super-privileges/ (and then restarting the DB server for good measure), but no luck.

I also tried creating the trigger and setting the variable via the mysql commmand line to make sure Navicat wasn't adding anything unwanted to my sql commands, but that failed, too. It also seems from searching that there's no way to grant yourself the SUPER privilege.

So ... is creating a trigger possible in RDS?

The answer by foxybagga should be the accepted one imo since it is more accessible than the CLI variant (no offence Garvice).

f
foxybagga

Its easy!

Open the RDS web console.

Open the “Parameter Groups” tab.

Create a new Parameter Group. On the dialog, select the MySQL family compatible to your MySQL database version, give it a name and confirm.

Select the just created Parameter Group and issue “Edit Parameters”.

Look for the parameter ‘log_bin_trust_function_creators’ and set its value to ’1′.

Save the changes.

Open the “Instances” tab. Expand your MySQL instance and issue the “Instance Action” named “Modify”.

Select the just created Parameter Group and enable “Apply Immediately”.

Click on “Continue” and confirm the changes.

Again, open the “Instances” tab. Expand your MySQL instance and issue the “Instance Action” named “Modify”.

Dont forget: Open the “Instances” tab. Expand your MySQL instance and issue the “Instance Action” named “Reboot”.

Via - http://techtavern.wordpress.com/2013/06/17/mysql-triggers-and-amazon-rds/


Cool. Thanks! Beats the hell out of CLI :-)
So glad to see amazon finally implemented this in the RDS web console! It was such a pain to do in the CLI
This works well. There's one more step if you are trying to do this from a mysqldump file. You need to remove the DEFINER blocks in order to be able to create the triggers. Here is a good article on doing this: percona.com/blog/2014/07/02/…
This did not work for me somehow.. Do you have to have any special RDS "dedicated or not shared" or anything like that?
REBOOT! In all the other places I read about updating log_bin_trust_function_creators and modifying the instance, nobody else mentioned rebooting afterwards. A tad obvious in retrospect, but that's always the way. Thanks for ending my grinding frustration and self-doubt :)
L
LowFieldTheory

No it is actually not impossible it just takes far too much extra work.

First off it seems to be impossible to apply Super Privileges to a default parameter group. So what I had to do was to create a new DB Parameter group either through the Console, or the CLI.

What I found was, the key is that the default region was not the region I was trying to use so I had to use a --region parameter to apply it to the group in the correct region where I was deploying my DB Instance

rds-create-db-parameter-group --db-parameter-group-name allow-triggers --description 'parameter group to allow triggers' --region your-region 

Next I had to create a DB Instance which used that parameter group. (Again through the console or CLI)

rds-create-db-instance

Then I had to modify the Parameter group to allow log_bin_trust_function_creators only accomplishable through the CLI

rds-modify-db-parameter-group --db-parameter-group-name yourgroupname --region yourRegion --parameters 'name=log_bin_trust_function_creators,value=true,method=immediate'

Lastly I had to modify the created DB Instance to allow triggers, also CLI only.

rds-modify-db-instance --db-instance-identifier your-db-instance-id --db-parameter-group-name allow-triggers --apply-immediately

I'm not in a position to test this right now, but I take it this worked for someone since you were awarded the bounty. I will definitely file this away for next time. Thanks!
So how did you go about solving this issue? I'm curious since this was the only way I was able to add triggers to RDS. (And took me about 1.5 days of piecing together bits and pieces from various help threads)
I didn't mean to imply that we solved it, we just went without triggers. Nice work finding this out, and I will give this a try next time.
How can I get the database instance id? Needed to run rds-modify-db-instance --db-instance-identifier **your-db-instance-id** --db-parameter-group-name allow-triggers --apply-immediately?
I appear to have been able to get this working through the RDS web console by simple setting the log_bin_trust_function_creators to 1 in the parameters section. This is one of the steps in @Garvice's answer anyway, so it can't hurt.
佚名

In addition to the parameter group modification that others have already mentioned, there is a further challenge that arises when using a MySQL database dump (via mysqldump) to create triggers in an AWS RDS instance. You may get a message like this:

ERROR 1227 (42000) at line 875: Access denied; you need (at least one of) the SUPER privilege(s) for this operation

This happens because the dump contains "definer" entries with a username that's different than your RDS master username. One solution is to replace the definer username with your RDS master username. Another solution is not to use mysqldump to create your database.

See this blog post for more information:

http://www.percona.com/blog/2014/07/02/using-mysql-triggers-and-views-in-amazon-rds/


This is exactly what I've faced with, thanks, Willie!
n
nickdnk

EDIT: It turns out Multi-AZ for MySQL uses "physical replication" and not logical replication, so this may not be correct. At least that's what their documentation says: https://aws.amazon.com/rds/details/multi-az/ - I have asked on their forums what this means, but did not get a reply. What's weird is that my RDS Multi-AZ instance claims it's a "master in a replication setup", even though I have no read replicas.

As the question has already been addressed, this is a comment more than an answer:

I'm surprised nobody takes into account why this feature is not available as a default. Amazon wouldn't disable it just to make people's lives harder.

In a master/slave replication it can be dangerous to use stored procedures and triggers that modify data (as in perform queries other than SELECT).

Please have a read below before disabling this restriction in a master/slave setup, which Amazon RDS is when you use Multi-AZ (and you should, for production at least).

http://dev.mysql.com/doc/refman/5.6/en/stored-programs-logging.html


And the problem may not be evident until much later (e.g. when trying to do a point in time recovery)!
D
Deepti Kohli

I followed the above but it did not work for me. I spent almost a day to figure out why it is not working and now I know why. I am listing down steps that I followed to make it work.

Created mysql parameters group using aws web console (make sure that it should have same family as the default parameter group. Earlier, I had created a parameter group but it had different family and so it did not work. This is critical step.

Using aws web console change value of log_bin_trust_function_creators to 1

Apply new parameter group. This is another critical step

rds-modify-db-instance –I $AWS_ACCESS_KEY –S $AWS_SECRET_KEY –region $EC2_REGION \ –db-instance-identifier $DB_INSTANCE \
–db-parameter-group-name $DB_GROUPNAME \
–apply-immediately

You need RDSCli from - http://s3.amazonaws.com/rds-downloads/RDSCli.zip

Then verify if parameter group is associated with your db instance

rds-describe-db-instances \
–I $AWS_ACCESS_KEY \
–S $AWS_SECRET_KEY \
–region $EC2_REGION

And then reboot before you try creating trigger

rds-reboot-db-instance \
–I $AWS_ACCESS_KEY \
–S $AWS_SECRET_KEY \
–region $EC2_REGION  \
–db-instance-identifier $DB_INSTANCE

Remember to set below environment variable before you try above commands.

export AWS_ACCESS_KEY=’*****’
export AWS_SECRET_KEY=’*****’
export EC2_REGION=’region’
export AWS_RDS_BIN=”$AWS_RDS_HOME/bin”
export PATH=$PATH:$AWS_RDS_BIN
export JAVA_HOME=c:/jdk1.6_25 (in most cases this is already set)

Thanks to http://blog.iprofs.nl/2013/03/20/rds-database-triggers-for-mysql/ for full details.


L
Loourr

AWS lays out how to enable functions and triggers in this post

Create a DB parameter group for your MySQL instance: Sign in to the AWS Management Console and open the Amazon RDS console. In the navigation pane, choose Parameter Groups. Choose Create Parameter Group. The Create Parameter Group window appears. For Parameter Group Family, choose the parameter group family. For Group Name, type the name of the new DB parameter group. For Description, type a description for the new DB parameter group. Choose Create. Important After you create a DB parameter group, you should wait at least 5 minutes before creating your first DB instance that uses that DB parameter group.

For more information about creating a DB parameter group, see Working with DB Parameter Groups - Creating a DB Parameter Group.

Modify the newly created parameter group and set the following parameter: In the navigation pane, choose Parameter Groups. The available DB parameter groups appear in a list. In the list, select the parameter group you want to modify. Choose Edit Parameters and set the following parameter to the specified value: log_bin_trust_function_creators = 1 Choose Save Changes. Important After you modify a DB parameter group, you should wait at least 5 minutes before creating your first DB instance that uses that DB parameter group.

For information about modifiying a DB parameter group, see Working with DB Parameter Groups - Modifying Parameters in a DB Parameter Group.

Associate your RDS DB instance with the new or modified DB parameter group: In the navigation pane, choose Instances. Select the DB instance you want to associate with a DB parameter group. On the Instance Actions menu, choose Modify. In the Modify DB Instance dialog box, under Database Options, choose the parameter group you want to associate with the DB instance. Changing this setting does not result in an outage. The parameter group name changes immediately, but the actual parameter changes are not applied until you reboot the instance without failover. Apply changes by rebooting the instance.


c
cesaregb

For me, it worked as the @foxybagga's answer suggest, but I needed to update the generated sql's dump (from mysqlworkbench) to have CURRENT_USER as the DEFINER

ie:

DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=CURRENT_USER*/ /*!50003 TRIGGER `sod_db`.`date`
BEFORE INSERT ON `sod_db`.`CashOut`
FOR EACH ROW
BEGIN
SET NEW.created = NOW();
END */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;

I hope this helps someone is having the same problem.