MySQL参数log_bin_trust_function_creators介绍

作者:潇湘隐者

出处:http://www.cnblogs.com/kerrycode/p/7641835.html

 

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

log_bin_trust_function_creators

Command-Line Format

--log-bin-trust-function-creators

System Variable

Name

log_bin_trust_function_creators

Variable Scope

Global

Dynamic Variable

Yes

Permitted Values

Type

boolean

Default

FALSE

 

 

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 theDETERMINISTIC 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> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.00 sec)
 
mysql>  show variables like '%log_bin_trust_function_creators%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| log_bin_trust_function_creators | OFF   |
+---------------------------------+-------+
1 row in set (0.00 sec)
 
mysql> 
mysql> DELIMITER //
mysql> CREATE FUNCTION GET_UPPER_NAME(emp_id INT)
    -> RETURNS VARCHAR(12)
    -> BEGIN
    ->   RETURN(SELECT UPPER(NAME) FROM TEST WHERE ID=emp_id);
    -> END
    -> //
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> 
 

在调用存储函数时,也会遇到这个错误,如下测试所示:

  


mysql> DELIMITER ;
mysql> set global log_bin_trust_function_creators=1;
Query OK, 0 rows affected (0.00 sec)
 
mysql> DELIMITER //
mysql> CREATE FUNCTION GET_UPPER_NAME(emp_id INT)
    -> RETURNS VARCHAR(12)
    -> BEGIN
    ->   RETURN(SELECT UPPER(NAME) FROM TEST WHERE ID=emp_id);
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)
 
mysql> SELECT ID,
    ->        GET_UPPER_NAME(ID)
    -> FROM TEST;
    -> //
+------+--------------------+
| ID   | GET_UPPER_NAME(ID) |
+------+--------------------+
|  100 | KERRY              |
|  101 | JIMMY              |
+------+--------------------+
2 rows in set (0.00 sec)
 
mysql> DELIMITER ;
mysql> set global log_bin_trust_function_creators=0;
Query OK, 0 rows affected (0.00 sec)
 
mysql> SELECT ID,
    ->        GET_UPPER_NAME(ID)
    -> FROM TEST;
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> 
 

clip_image001

 

 

那么为什么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> show variables like 'log_bin_trust_function_creators';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| log_bin_trust_function_creators | OFF   |
+---------------------------------+-------+
1 row in set (0.00 sec)
 
mysql> DROP FUNCTION GET_UPPER_NAME;
Query OK, 0 rows affected (0.00 sec)
 
mysql> DELIMITER //
mysql> CREATE FUNCTION GET_UPPER_NAME(emp_id INT)
    -> RETURNS VARCHAR(12)
    -> READS SQL DATA
    -> BEGIN
    ->   RETURN(SELECT UPPER(NAME) FROM TEST WHERE ID=emp_id);
    -> END
    -> //
Query OK, 0 rows affected (0.01 sec)
 
mysql> DELIMITER ;
mysql> SELECT ID,
    ->        GET_UPPER_NAME(ID)
    -> FROM TEST;
+------+--------------------+
| ID   | GET_UPPER_NAME(ID) |
+------+--------------------+
|  100 | KERRY              |
|  101 | JIMMY              |
+------+--------------------+
2 rows in set (0.00 sec)
 

clip_image002

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: log_bin_trust_function_creators 是 MySQL 数据库中的一个系统变量,它用于控制是否信任函数创建者。如果该变量设置为 1,则 MySQL 将信任函数创建者,允许他们创建具有任意安全权限的函数。如果该变量设置为 0,则 MySQL 将不信任函数创建者,只允许他们创建具有较低安全权限的函数。 ### 回答2: log_bin_trust_function_creators是MySQL数据库中的一个变量。它用于决定是否信任函数创建者。默认情况下,该变量的值为0,意味着不信任函数创建者。 在MySQL中,函数可以由权限高的用户(如root用户)创建,并可以被其他用户引用和调用。但是,如果log_bin_trust_function_creators的值为0,则MySQL会对具有含有函数的语句进行复制时进行限制。 当log_bin_trust_function_creators的值为0时,如果具有函数的语句被复制到其他MySQL实例,而这个实例上没有对应的函数,会导致复制错误。这是为了防止潜在的安全问题。 如果要避免复制错误,并且确信函数创建者是可信的,则可以将log_bin_trust_function_creators的值设置为1。这样,MySQL会信任函数创建者,并允许复制具有函数的语句,而无需检查函数的存在。 但是,需要注意的是,将log_bin_trust_function_creators设置为1可能会在复制过程中引入安全风险。因此,在设置为1之前,应该确保函数创建者是受信任的,并且在复制时可以保证函数的正确性。 总之,log_bin_trust_function_creators是MySQL中的一个变量,用于控制是否信任函数创建者,在复制过程中是否允许具有函数的语句。 ### 回答3: log_bin_trust_function_creators是MySQL数据库中的一个系统变量。默认情况下,这个变量的值是OFF。 log_bin_trust_function_creators是用来确定是否信任二进制日志中的用户定义函数创建者的身份。当这个变量的值是OFF时,如果一个用户没有SUPER权限,他所创建的函数在二进制日志中将以一个特殊的格式保存,以保证复制流程的正确性。这种格式可能会导致函数在从二进制日志中恢复时被扩展(递归调用)。 然而,如果将log_bin_trust_function_creators设置为ON,MySQL将信任函数的创建者,并且将函数以其完整的形式(包括函数定义和代码)写入二进制日志。这样做的好处是,复制从库可以直接执行二进制日志中的函数,而不需要重新创建函数。这在一些特殊情况下非常有用,例如,在进行数据库迁移或者将复制流程从一个主库切换到另一个主库时。 但是这也存在一些风险。如果在二进制日志中收到了一个来自未经授权的源的包含有恶意代码的函数,那么恶意代码可能会在从库上执行,从而对数据库和应用程序造成潜在的安全风险。因此,需要谨慎使用并确认二进制日志中的函数来源可靠。 总之,log_bin_trust_function_creators是一个用于控制MySQL二进制日志中用户定义函数行为的系统变量,它可以选择性地信任函数创建者并将函数完整写入到二进制日志中。在使用时需要权衡其带来的便利性和安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值