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) 時,MySQL 要求創建的函數/觸發器/事件必須聲明確定性特徵,以確保主從複寫一致性:
DETERMINISTIC:相同輸入必然產生相同輸出NO SQL:不包含 SQL 語句READS SQL DATA:僅讀取資料不修改MODIFIES SQL DATA:修改資料(預設,不允許)未聲明時預視為 MODIFIES SQL DATA,為安全起見禁止創建。
SET GLOBAL log_bin_trust_function_creators = 1;
-- 永久生效寫入 my.cnf
[mysqld]
log_bin_trust_function_creators = 1
允許創建未聲明特徵的函數,由開發者自行保證確定性。主從環境若函數非確定性會導致資料不一致。
CREATE FUNCTION my_func(p INT) RETURNS INT
DETERMINISTIC
NO SQL
RETURN p * 2;
DETERMINISTIC NO SQL 或 DETERMINISTIC READS SQL DATADETERMINISTIC READS SQL DATA
SET SESSION log_bin_trust_function_creators = 1;
CREATE FUNCTION ...;
SET SESSION log_bin_trust_function_creators = 0;
SHOW VARIABLES LIKE 'log_bin_trust_function_creators';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| log_bin_trust_function_creators | ON |
+---------------------------------+-------+
SET GLOBAL log_bin_trust_function_creators = 0;
log_bin_trust_function_creators=1,代碼審查時強制要求聲明 DETERMINISTIC 等特徵SET SESSION ... = 1 導入,完成後關閉SHOW CREATE FUNCTION 確認特徵聲明完整ERROR 1418 是 Binlog 安全機制。SET GLOBAL log_bin_trust_function_creators=1 秒解,但最佳實踐是正確聲明 DETERMINISTIC/NO SQL/READS SQL DATA。主從一致性由開發者守護,MySQL 僅提供護欄。