illustration

sql创建函数过程中的报错

  • 操作系统:Linux Ubuntu 18.04 LTS
  • mysql: 8.0.23

1. 需要修改语句结束符号:DELIMITER

最开始sql代码:

1
2
3
4
5
6
CREATE DEFINER=`root`@`%` FUNCTION adddeposit()
RETURNS INTEGER
BEGIN
INSERT INTO ACCOUNT016 VALUES ('A-100lsaj','Pownal',777);
RETURN 0;
END;

报错:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘END’ at line 1

在函数结束前用DELIMITER //将默认的";"改为"//"

1
2
3
4
5
6
7
8
DELIMITER //
CREATE DEFINER=`root`@`%` FUNCTION adddeposit()
RETURNS INTEGER
BEGIN
INSERT INTO ACCOUNT016 VALUES ('A-100lsaj','Pownal',777);
RETURN 0;
END;
//

2. DETERMINISTIC, NO SQL 和 READS SQL DATA问题

报错:

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)

参考Mysql在导入时遇到DETERMINISTIC, NO SQL 和 READS SQL DATA问题得以解决

mysql下执行

1
set global log_bin_trust_function_creators=TRUE

然后在配置文件my.cnf最后追加一行,防止以后还需要修改

1
su -c "echo log-bin-trust-function-creators=1 >> /etc/mysql/mysql.conf.d/mysqld.cnf" root

ps:配置文件的位置需要自己去找,还有一个看上去似的目录/etc/mysql/conf.d/mysql.cnf,看上去也像是配置文件但却是不对的,要加到[mysqld]后,否则数据库无法打开

3. definer报错

报错:

The user specified as a definer (‘root’@’%’) does not exist

将%改为localhost或者远程登录的ip地址,如果仍然有错,那说明权限出了问题,要给对应用户正确的host权限

1
2
3
4
5
6
7
8
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION adddeposit()
RETURNS INTEGER
BEGIN
INSERT INTO ACCOUNT016 VALUES ('A-100lsaj','Pownal',777);
RETURN 0;
END;
//