ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA
in its declaration and binary logging is enabled
(you *might* want to use the less safe log_bin_trust_function_creators variable)
啟用 Binlog (log_bin=ON) 且建立 Function/Trigger/Event 時,MySQL 要求明確宣告函數特性以確保主從複制安全:
DETERMINISTIC:相同輸入必產生相同輸出。NO SQL:不包含 SQL 語句。READS SQL DATA:僅讀取資料不修改。MODIFIES SQL DATA:會修改資料(預設隱含)。未宣告時,MySQL 拒絕建立,防止非確定性函數導致主從資料不一致。
SET GLOBAL log_bin_trust_function_creators = 1;
永久生效請加入 my.cnf:
[mysqld]
log_bin_trust_function_creators = 1
風險:允許建立非確定性函數,可能導致主從資料漂移。**僅限非複制環境或開發測試使用。**
CREATE FUNCTION my_func(p_int INT)
RETURNS INT
DETERMINISTIC
NO SQL
BEGIN
RETURN p_int * 2;
END;
DETERMINISTIC READS SQL DATADETERMINISTIC MODIFIES SQL DATA(需 SUPER 權限)。若 Master 已建立函數、Slave 報錯停止:
-- 在 Slave 執行
STOP SLAVE;
SET GLOBAL log_bin_trust_function_creators = 1;
START SLAVE;
隨後補上正確的函數定義並重啟複制。