Pages

Wednesday, January 29, 2014

MySQL stored routine error, This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration

When I try to create MySQL Stored Routine like function, then I got  "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)". This error message will appear when try to create Stored Procedure and Function in MySQL Server which had Enabled Binary Log.

The Binary Log

The binary log contains events that describe database changes such as table creation operations or changes to the table data. It also contains events for statements that potentially could have made changes The binary log also contains information about how long each statement took that updated data. The binary log has two important purposes: 

1) For replication, the binary log on a master replication server provides a record of the data changes to be sent to slave servers. The master server sends the events contained in its binary log to its slaves, which execute those events to make the same data changes that were made on the master.

2) Certain data recovery operations require use of the binary log. After a backup has been restored, the events in the binary log that were recorded after the backup was made are re-executed. These events bring databases up to date from the point of the backup. 

Running a server with binary logging enabled makes performance slightly slower. However, the benefits of the binary log in enabling you to set up replication and for restoring operations generally outweigh this minor performance decrement.  

Binary Logging of Stored Programs

 The binary log contains information about SQL statements that modify database contents. This information is stored in the form of events that describe the modifications. The binary log has two important purposes: 

For replication, the binary log is used on master replication servers as a record of the statements to be sent to slave servers. The master server sends the events contained in its binary log to its slaves, which execute those events to make the same data changes that were made on the master.

Certain data recovery operations require use of the binary log. After a backup file has been restored, the events in the binary log that were recorded after the backup was made are re-executed. These events bring databases up to date from the point of the backup.

However, there are certain binary logging issues that apply with respect to stored programs (stored procedures and functions, and triggers):
  • Logging occurs at the statement level. In some cases, it is possible that a statement will affect different sets of rows on a master and a slave.
  • Replicated statements executed on a slave are processed by the slave SQL thread, which has full privileges. It is possible for a procedure to follow different execution paths on master and slave servers, so a user can write a routine containing a dangerous statement that will execute only on the slave where it is processed by a thread that has full privileges.
  • If a stored program that modifies data is nondeterministic, it is not repeatable. This can result in different data on a master and slave, or cause restored data to differ from the original data.
The current conditions on the use of stored functions in MySQL  can be summarized as follows. These conditions do not apply to stored procedures and they do not apply unless binary logging is enabled. 
 To create or alter a stored function, you must have the SUPER privilege, in addition to the CREATE ROUTINE or ALTER ROUTINE privilege that is normally required.

When you create a stored function, you must declare either that it is deterministic or that it does not modify data. Otherwise, it may be unsafe for data recovery or replication.
By default, for a CREATE FUNCTION statement to be accepted, at least one of DETERMINISTIC, NO SQL, or READS SQL DATA must be specified explicitly. Otherwise, this error occurs

 We can avoid this error by creating a stored routines (stored procedures and functions, and triggers) with DETERMINISTIC like

CREATE FUNCTION f1(i INT)
RETURNS INT
DETERMINISTIC
READS SQL DATA
BEGIN
  RETURN i;
END;

MySQL does not check that a function declared DETERMINISTIC is free of statements that produce nondeterministic results.  

OR to relax the preceding conditions on function creation, set the global log_bin_trust_function_creators system variable to 1. By default, this variable has a value of 0, but you can change it like this: 

mysql> SET GLOBAL log_bin_trust_function_creators = 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.

No comments:

Post a Comment