如果在自行安装的MySQL上无法使用trigger,可以使用下面的方法:
解决方法:
第一步,用root用户登录:mysql -u root -p
第二步,设置参数log_bin_trust_function_creators为1:set global log_bin_trust_function_creators = 1;
在AWS上面的无法使用会得到以下报错:
mysql> create trigger ins_sum before insert on account for each row set @sum = @sum + new.amount;
ERROR 1419 (HY000): You do not have the SUPER privilege and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
解决方案是新建数据库参数组:
-
导航到参数组,新建-填写名称和描述,查找log_bin_trust_function_creators 并设置为‘1’。
-
导航到数据库,选择要与数据库参数组关联的数据库实例;选择操作,然后选择修改;选择要与该数据库实例关联的参数组。
-
重新启动数据库实例。
重新连接数据库后:
mysql> use company;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create trigger ins_sum before insert on account for each row set @sum = @sum + new.amount;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from information_schema.triggers where trigger_name='ins_sum'\G;
*************************** 1. row ***************************
TRIGGER_CATALOG: def
TRIGGER_SCHEMA: company
TRIGGER_NAME: ins_sum
EVENT_MANIPULATION: INSERT
EVENT_OBJECT_CATALOG: def
EVENT_OBJECT_SCHEMA: company
EVENT_OBJECT_TABLE: account
ACTION_ORDER: 1
ACTION_CONDITION: NULL
ACTION_STATEMENT: set @sum = @sum + new.amount
ACTION_ORIENTATION: ROW
ACTION_TIMING: BEFORE
ACTION_REFERENCE_OLD_TABLE: NULL
ACTION_REFERENCE_NEW_TABLE: NULL
ACTION_REFERENCE_OLD_ROW: OLD
ACTION_REFERENCE_NEW_ROW: NEW
CREATED: 2021-04-28 11:26:27.09
SQL_MODE: NO_ENGINE_SUBSTITUTION
DEFINER: admin@%
CHARACTER_SET_CLIENT: utf8mb4
COLLATION_CONNECTION: utf8mb4_0900_ai_ci
DATABASE_COLLATION: utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
ERROR:
No query specified
如上所示,已经可以使用Trigger了。
注意:
- 此参数同时允许了函数和存储过程。
- 创建Trigger时如果收到“ERROR 1419 (HY000): You do not have the SUPER privilege and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable)”,请将 log_bin_trust_post_creators 参数修改为 1 以允许数据库实例上的函数、存储过程和触发器。