MYSQL 創(chuàng)建函數(shù)出錯的解決方案
在使用MySQL數(shù)據(jù)庫時,有時會遇到MySQL函數(shù)不能創(chuàng)建的情況。下面就教您一個解決MySQL函數(shù)不能創(chuàng)建問題的方法,供您借鑒參考。
案例一:
目前在項目中,執(zhí)行創(chuàng)建mysql的函數(shù)出錯,
mysql 創(chuàng)建函數(shù)出錯信息如下:
Error Code: 1227. Access denied; you need (at least one of) the SUPER privilege(s) for this operation
首先檢查創(chuàng)建函數(shù)的功能是否開啟,檢查是否開啟創(chuàng)建功能的SQL如下:
-- 查看是否開啟創(chuàng)建函數(shù)的功能 show variables like '%func%'; -- 開啟創(chuàng)建函數(shù)的功能 set global log_bin_trust_function_creators = 1;
執(zhí)行完SQL之后發(fā)現(xiàn)已經(jīng)開啟了,隨檢查自己的SQL是否寫錯(因為SQL是別人給的,在別人環(huán)境沒問題,在自己的環(huán)境就有可能)。
突然發(fā)現(xiàn)了確實是SQL出現(xiàn)問題,由于他創(chuàng)建的SQL有指定用戶,所以導(dǎo)致出現(xiàn)問題,以下是他的SQL:
DROP FUNCTION IF EXISTS `nextval`; DELIMITER ;; CREATE DEFINER=`devop`@`%` FUNCTION `nextval`(`seq_name` VARCHAR(50)) RETURNS varchar(20) CHARSET utf8 BEGIN DECLARE seq_max BIGINT(20); UPDATE sequenceconftable SET `max` = `max` + NEXT WHERE NAME = seq_name; SELECT `max` INTO seq_max FROM sequenceconftable WHERE NAME = seq_name ; RETURN seq_max; END ;; DELIMITER ;
由于CREATE_FUNCTION規(guī)范,可以發(fā)現(xiàn)就是DEFINER這個參數(shù)是可以指定數(shù)據(jù)庫用戶的,但是自己的庫卻不是這個用戶,所以導(dǎo)致問題。
目前問題已經(jīng)解決。
-EOF-
案例二:
在MySQL創(chuàng)建用戶自定義函數(shù)時,報以下錯誤:
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)
這是因為有一個安全參數(shù)沒有開啟,log_bin_trust_function_creators 默認為0,是不允許function的同步的,開啟這個參數(shù),就可以創(chuàng)建成功了。
mysql> show variables like '%fun%'; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | log_bin_trust_function_creators | ON | +---------------------------------+-------+ 1 row in set (0.00 sec) mysql> set global log_bin_trust_function_creators=1; Query OK, 0 rows affected (0.00 sec) mysql> show variables like '%fun%'; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | log_bin_trust_function_creators | ON | +---------------------------------+-------+ 1 row in set (0.00 sec)
如果是在有master上開啟了該參數(shù),記得在slave端也要開啟這個參數(shù)(salve需要stop后再重新start),否則在master上創(chuàng)建函數(shù)會導(dǎo)致replaction中斷。
案例三:
Error Code : 1418
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) (0 ms taken)
分析:
根據(jù)系統(tǒng)提示,導(dǎo)致該錯誤的原因可能是一個安全設(shè)置方面的配置,查手冊log_bin_trust_function_creators參數(shù)缺省0,是不允許function的同步的,一般我們在配置repliaction的時候,都忘記關(guān)注這個參數(shù),這樣在master更新funtion后,slave就會報告錯誤,然后slave stoped。
處理過程:
登陸mysql數(shù)據(jù)庫
> set global log_bin_trust_function_creators = 1; > start slave;
跟蹤mysql的啟動日志,slave正常運行,問題解決。
相關(guān)文章
MySQL處理重復(fù)數(shù)據(jù)的學(xué)習筆記
在本篇文章里小編給大家分享的是一篇關(guān)于MySQL處理重復(fù)數(shù)據(jù)的學(xué)習筆記,需要的朋友們可以參考下。2020-03-03