MySQL Err 1418 的原因分析及解决方法
admin
2023-04-15 08:02:25
0

MySQL的有个参数log_bin_trust_function_creators,官方文档对这个参数的介绍、解释如下所示:

MySQL Err 1418  的原因分析及解决方法

This variable applies when binary logging is enabled. It controls whether stored function creators can be trusted not to create stored functions that will cause unsafe events to be written to the binary log. If set to 0 (the default), users are not permitted to create or alter stored functions unless they have the SUPER privilege in addition to the CREATE ROUTINE or ALTER ROUTINE privilege. A setting of 0 also enforces the restriction that a function must be declared with the DETERMINISTIC characteristic, or with the READS SQL DATA or NO SQL characteristic. If the variable is set to 1, MySQL does not enforce these restrictions on stored function creation. This variable also applies to trigger creation. See Section 23.7, “Binary Logging of Stored Programs”.

简单介绍一下,当二进制日志启用后,这个变量就会启用。它控制是否可以信任存储函数创建者,不会创建写入二进制日志引起不安全事件的存储函数。如果设置为0(默认值),用户不得创建或修改存储函数,除非它们具有除CREATE ROUTINE或ALTER ROUTINE特权之外的SUPER权限。 设置为0还强制使用DETERMINISTIC特性或READS SQL DATA或NO SQL特性声明函数的限制。 如果变量设置为1,MySQL不会对创建存储函数实施这些限制。 此变量也适用于触发器的创建。 请参见第23.7节“Binary Logging of Stored Programs”。

下面我们测试一下,当开启二进制日志后,如果变量log_bin_trust_function_creators为OFF,那么创建或修改存储函数就会报“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 Err 1418  的原因分析及解决方法
在调用存储函数时,也会遇到这个错误,如下测试所示:
MySQL Err 1418  的原因分析及解决方法
MySQL Err 1418  的原因分析及解决方法

那么为什么MySQL有这样的限制呢? 因为二进制日志的一个重要功能是用于主从复制,而存储函数有可能导致主从的数据不一致。所以当开启二进制日志后,参数log_bin_trust_function_creators就会生效,限制存储函数的创建、修改、调用。那么此时如何解决这个问题呢?官方文档介绍如下,具体可以参考23.7 Binary Logging of Stored Programs

If you do not want to require function creators to have the SUPER privilege (for example, if all users with the CREATE ROUTINE privilege on your system are experienced application developers), set the global log_bin_trust_function_creators system variable to 1. You can also set this variable by using the --log-bin-trust-function-creators=1 option when starting the server. If binary logging is not enabled, log_bin_trust_function_creators does not apply. SUPER is not required for function creation unless, as described previously, the DEFINER value in the function definition requires it.
If a function that performs updates is nondeterministic, it is not repeatable. This can have two undesirable effects:

· It will make a slave different from the master.

· Restored data will be different from the original data.

To deal with these problems, MySQL enforces the following requirement: On a master server, creation and alteration of a function is refused unless you declare the function to be deterministic or to not modify data. Two sets of function characteristics apply here:

· The DETERMINISTIC and NOT DETERMINISTIC characteristics indicate whether a function always produces the same result for given inputs. The default is NOT DETERMINISTIC if neither characteristic is given. To declare that a function is deterministic, you must specify DETERMINISTIC explicitly.

· The CONTAINS SQL, NO SQL, READS SQL DATA, and MODIFIES SQL DATA characteristics provide information about whether the function reads or writes data. Either NO SQL or READS SQL DATA indicates that a function does not change data, but you must specify one of these explicitly because the default is CONTAINS SQL if no characteristic is given.

1: 如果数据库没有使用主从复制,那么就可以将参数log_bin_trust_function_creators设置为1。

mysql> set global log_bin_trust_function_creators=1;

这个动态设置的方式会在服务重启后失效,所以我们还必须在my.cnf中设置,加上log_bin_trust_function_creators=1,这样就会永久生效。

2:明确指明函数的类型,如果我们开启了二进制日志, 那么我们就必须为我们的function指定一个参数。其中下面几种参数类型里面,只有 DETERMINISTIC, NO SQL 和 READS SQL DATA 被支持。这样一来相当于明确的告知MySQL服务器这个函数不会修改数据。

1 DETERMINISTIC 不确定的

2 NO SQL 没有SQl语句,当然也不会修改数据

3 READS SQL DATA 只是读取数据,当然也不会修改数据

4 MODIFIES SQL DATA 要修改数据

5 CONTAINS SQL 包含了SQL语句
MySQL Err 1418  的原因分析及解决方法
MySQL Err 1418  的原因分析及解决方法

相关内容

热门资讯

今日重磅消息“欢乐众娱到底是不... 网上科普关于“欢乐众娱有没有挂”话题很是火热,小编也是针对欢乐众娱作*弊开挂的方法以及开挂对应的知识...
玩家攻略科普“皇豪众娱牛牛真的... 网上科普关于“皇豪众娱牛牛有没有挂”话题很是火热,小编也是针对皇豪众娱牛牛作*弊开挂的方法以及开挂对...
【第一财经】“宝宝浙江游戏怎么... 家人们!今天小编来为大家解答宝宝浙江游戏透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里...
重磅消息“天府红桃3怎么装挂?... 重磅消息“天府红桃3怎么装挂?”(透视曝光猫腻)您好,天府红桃3这个游戏其实有挂的,确实是有挂的,需...
【今日要闻】“微壳字牌究竟有挂... 有 亲,根据资深记者爆料微壳字牌是可以开挂的,确实有挂(咨询软件无需打开...
终于明白“相约福建麻将可以开挂... 终于明白“相约福建麻将可以开挂吗?”(确实真的有挂)您好,相约福建麻将这个游戏其实有挂的,确实是有挂...
【今日要闻】“土豪赢三张怎么开... 网上科普关于“土豪赢三张有没有挂”话题很是火热,小编也是针对土豪赢三张作*弊开挂的方法以及开挂对应的...
终于明白“暗宝到底是不是挂?”... 有 亲,根据资深记者爆料暗宝是可以开挂的,确实有挂(咨询软件无需打开直接...
今日重大消息“哥哥打大A开挂器... 您好:哥哥打大A这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款游...
终于明白“麻友圈2挪来挪去怎么... 网上科普关于“麻友圈2挪来挪去有没有挂”话题很是火热,小编也是针对麻友圈2挪来挪去作*弊开挂的方法以...