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)
log_bin=ON)log_bin_trust_function_creators = OFF,阻擋不確定的函數建立
-- 客戶端執行
SET GLOBAL log_bin_trust_function_creators = 1;
-- 永久生效:加入 my.cnf [mysqld]
log_bin_trust_function_creators = 1
風險:允許建立可能導致主從不一致的非確定性函數 (如使用 UUID(), RAND())。
CREATE FUNCTION my_func() RETURNS INT
DETERMINISTIC -- 相同輸入必得相同輸出
NO SQL -- 不包含 SQL 語句
-- 或 READS SQL DATA -- 只讀資料
-- 或 MODIFIES SQL DATA -- 修改資料
BEGIN
RETURN 1;
END;
| 關鍵字 | 含義 | Binlog 記錄 |
|---|---|---|
| DETERMINISTIC | 確定性:相同輸入→相同輸出 | 安全,記錄函數呼叫 |
| NOT DETERMINISTIC | 非確定性 (預設) | 需 log_bin_trust_function_creators=1 |
| NO SQL | 無 SQL 語句 | 安全 |
| READS SQL DATA | 含 SELECT | 安全 |
| MODIFIES SQL DATA | 含 INSERT/UPDATE/DELETE | 需 log_bin_trust_function_creators=1 |
SHOW VARIABLES LIKE 'log_bin_trust_function_creators';
-- +---------------------------------+-------+
-- | Variable_name | Value |
-- +---------------------------------+-------+
-- | log_bin_trust_function_creators | ON |
-- +---------------------------------+-------+
DETERMINISTIC / READS SQL DATASHOW CREATE FUNCTION 確認特性宣告