In MySQL 8.0, even with binlog_format set to "ROW" or "MIXED" I seem to be unable to create a non-deterministic function. e.g:
DELIMITER //
CREATE FUNCTION nonDeterministicFunc()
RETURNS CHAR(4)
BEGIN
RETURN LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
END
//
DELIMITER ;
gives the error:
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)
I'm aware that during replication, if binlog_format is set to STATEMENT then the slave DB will re-call the function and get a different result, but I thought when set to "ROW" or "MIXED" it would replicate the row changes not the function calls and so non-deterministic functions were fine?
The comment from MySQL support Margaret Fisher seems to say this is a feature:
https://bugs.mysql.com/bug.php?id=101480#c511192
"This ensures that if the binary logging format changes after the function is created, it will still work."
I understand the danger that someone changes the replication format later.... but surely that is an extreme corner case and that adding non-deterministic functions should to be allowed when replication is currently set to ROW or MIXED?
Or am I completely missing the obvious?
Any session may override the binlog_format, so the MySQL Server cannot rely on the global setting to ensure that the function is safe. If the function's code performs an INSERT/UPDATE/DELETE, and the session has been changed to binlog_format=STATEMENT, it would cause a different change on the replica than on the source.
I recommend you add the NO SQL option to your function definition to satisfy the error check.
Our App is developed with the following requirements
Front end: HTML and Google apps script
Back end: Google Cloud SQL,MySQL VER 5.6.21, engine= InnoDB
We are using Stored Procedures to access CLOUD SQL.
This stored procedure will be called via Google Apps Script.
Example Stored Procedure and the process of execution via Google Apps Script as below...
Step 1: Store procedure will create a user and giving grant access for store procedures, tables,triggers, views
DROP PROCEDURE IF EXISTS SP_TEST;
CREATE PROCEDURE SP_TEST(OUT SUCCESS_MESSAGE TEXT)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SET SUCCESS_MESSAGE=0;
END;
START TRANSACTION;
SET AUTOCOMMIT = 0;
DROP USER TESTUSER;
CREATE USER 'TESTUSER'#'%' IDENTIFIED BY 'TESTUSER';
GRANT EXECUTE ON PROCEDURE PROC_NAME1 TO 'TESTUSER'#'%';
GRANT EXECUTE ON PROCEDURE PROC_NAME2 TO 'TESTUSER'#'%';
SET SUCCESS_MESSAGE=1;
COMMIT;
END;
Step 2: script used in Google apps script to call store procedure/access sql table
try
{
//OPEN CONNECTION
var conn=Jdbc.getCloudSqlConnection("jdbc:google:rdbms://" + DB_INSTANCE + "/" + DB_SCHEMA, DB_USER, DB_PASSWORD);
conn.setAutoCommit(false);
//CALL STORE PROCEDURE
var create_stmt = conn.createStatement();
create_stmt.execute("CALL SP_TEST (#SUCCESS_MESSAGE)”);
create_stmt.close();
//SELECT RESULT RETURNED FROM STORE PROCEDURE
var stmt_rolecrinsrtflag = conn.createStatement()
var flag_rolecrinsrtselect="SELECT #SUCCESS_MESSAGE";
var flag_rolecrinsrtrs=stmt_rolecrinsrtflag.executeQuery(flag_rolecrinsrtselect);
if(flag_rolecrinsrtrs.next())
var flag_rolecrinsrtinsert=flag_rolecrinsrtrs.getString("#SUCCESS_MESSAGE");
flag_rolecrinsrtrs.close();
stmt_rolecrinsrtflag.close();
conn.commit();
//CLOSE CONNECTION
conn.close();
return flag_rolecrinsrtinsert;
}
catch(err)
{
//to do rollback,if any exception..
conn.rollback();
}
We have more than 200 sp’s and 98 tables.
All the stored procedure and all tables will be loaded in Google Cloud Sql via root like user only.
After calling the Store Procedure via Google apps script , user will be created in user table and given stored procedure will be granted access for the created user.
But Sometimes we are getting the following issues when we call any store procedure/ accessing the sql table via Google Apps Script . Initially it worked well, but recently, only we are facing these issues.
Error 1:"INVALID CONNECTION ID" is thrown and the following things are
observed
TESTUSER wiped in the mysql.user table as below
Execute permission revoked for the PROC_NAME1,PROC_NAME2.
Some Store procedure which is created already has been automatically
dropped such as PROC_NAME2
cant able to proceed with the remaining execution of Apps Script
Error 2: "THIS CONNECTION IS CLOSED"
We cant able to proceed the remaining execution..
In script side if any issue/exception occurs , we need to proceed the execution, i.e., we need to drop the temp table created during the STORE PROCEDURE execution process.
Since the above issues occurs, we can't able to proceed the script execution further.
And also when we execute the store procedure, sometime we are getting issue like
Error 3: "Incorrect key file for table './mysql/procs_priv.MYI'; try to repair it"
After this issue, cant able to execute or run any Store Procedure/
access Google cloud Sql.
Pls refer the below link..
Having Result of sample Stored Procedure that is given above-Before Issue and After Issue...
https://docs.google.com/a/ssomens.com/document/d/1N5_-O2UfescCmWe9IorteUZUh7gtgkF9-RwubvF4MhM/edit
The 'jdbc:google:rdbms' way to connect is deprecated and the preferred way to connect to Cloud SQL from an external application is to use the IP connectivity via the 'jdbc:mysql' (docs).
This looks like common connection timeout due to instance inactivity (remeber that Cloud SQL has two billing plans and most users choose the per-use plan which implies that instance is not always up and running).
As a general rule-of-thumb, it's always advisable to implement some kind of exponential backoff to cope with occasional connection errors like this one, which might happen from time to time due to the already mentioned inactivity timeouts, Cloud SQL instance restarts, and possibly other unforeseen reasons.
Please read "How should I manage connections?" section [1] of the public documentation.
[1] https://cloud.google.com/sql/faq#connections
I have created an AWS RDS instance, I have created my master user with master password, and it is working/connecting fine.
But when I am going to create a function on that instance, it shows me the following error:
ERROR 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_creator variable).
In my instance the variable log_bin_trust_function_creators shows OFF, and if I try to change the variable using SET GLOBAL log_bin_trust_function_creators = 1;, it gives me another error "Error Code: 1227. Access denied; you need (at least one of) the SUPER privilege(s) for this operation"
Set log_bin_trust_function_creators = 1 for Parameter group of the RDS instance.
Note: Default Parameter-Group is not editable. Create a new Parameter-Group and assign it to the RDS instance by modifying it from UI (AWS Admin Console) OR maybe using commands
DELIMITER $$
CREATE DEFINER=`DB_USERNAME_HERE`#`%` FUNCTION `GetDistance`(coordinate1 VARCHAR(120), coordinate2 VARCHAR(120)) RETURNS decimal(12,8)
READS SQL DATA
BEGIN
DECLARE distance DECIMAL(12,8);
/*Business logic goes here for the function*/
RETURN distance;
END $$
DELIMITER ;
Here, you have to replace DB_USERNAME_HERE with you RDS database username and function names according to you need.
Important thing is: DEFINER=`DB_USERNAME_HERE`#`%`
This was the problem I was facing after setting log_bin_trust_function_creators = 1 in parameter group. And it worked like a charm.
A better way is to apply your own parameter group, with log_bin_trust_function_creators set to true. (its false by default)
This happens when you try to create a procedure/function/view with a DEFINER that is not the current user.
To solve this remove or update the DEFINER clause.
I'm using MySQL 5.5 (x64) and MySQL Workbench 5.2 deployed locally on a Windows 7 workstation for development purposes. I used MySQL Workbench to build a schema with the following function definition:
CREATE FUNCTION `db`.`get_public_name` (GPN_entID INT) RETURNS VARCHAR(64)
DETERMINISTIC
BEGIN
DECLARE GPN_pubName VARCHAR(64);
SELECT public_name INTO GPN_pubName
FROM entity WHERE id_entity=GPN_entID LIMIT 1;
RETURN GPN_pubName;
END
I then attempt to "Forward Engineer" the schema to the database with the following options specified:
DROP Objects Before Each Create Object
Generate DROP SCHEMA
Add SHOW WARNINGS After Every DDL Statement
GENERATE INSERT Statements for Tables
After this, MySQL Workbench attempts to publish to the server:
CREATE FUNCTION `db`.`get_public_name` (GPN_entID INT) RETURNS VARCHAR(64)
DETERMINISTIC
BEGIN
DECLARE GPN_pubName VARCHAR(64);
SELECT public_name FROM entity WHERE id_entity = GPN_entID;
RETURN GPN_pubName;
END
This results in the following error:
Executing SQL script in server
ERROR: Error 1415: Not allowed to return a result set from a function
Upon closer examination, I noticed the "INTO" and "LIMIT" clauses of the SELECT statement have been removed from the original function definition. This looks like it might be a cached version of the function, but I have tried everything I can think of (short of uninstalling and reinstalling MySQL Workbench) to flush any such cache to reload the correct version, but to no avail.
So, why is this change happening and how do I prevent it from happening?
Try changing to this:
SELECT public_name FROM entity WHERE id_entity = GPN_entID LIMIT 1 INTO GPN_pubName;
I'm embarrassed; if it wasn't for the fact this may be useful to others, I'd just go ahead and delete this question to hide my shame.
It turns out I created two functions with the same name and MySQL Workbench happily let me do so. I didn't notice that was the case until I started going through the stored routines with a more careful eye. I was editing one, but the other one (which had the error) was never changed. Since publishing each function involved dropping any earlier version from the database, I probably wouldn't have noticed this until things weren't working properly.
My current process for debugging stored procedures is very simple. I create a table called "debug" where I insert variable values from the stored procedure as it runs. This allows me to see the value of any variable at a given point in the script, but is there a better way to debug MySQL stored procedures?
The following debug_msg procedure can be called to simply output a debug message to the console:
DELIMITER $$
DROP PROCEDURE IF EXISTS `debug_msg`$$
DROP PROCEDURE IF EXISTS `test_procedure`$$
CREATE PROCEDURE debug_msg(enabled INTEGER, msg VARCHAR(255))
BEGIN
IF enabled THEN
select concat('** ', msg) AS '** DEBUG:';
END IF;
END $$
CREATE PROCEDURE test_procedure(arg1 INTEGER, arg2 INTEGER)
BEGIN
SET #enabled = TRUE;
call debug_msg(#enabled, 'my first debug message');
call debug_msg(#enabled, (select concat_ws('','arg1:', arg1)));
call debug_msg(TRUE, 'This message always shows up');
call debug_msg(FALSE, 'This message will never show up');
END $$
DELIMITER ;
Then run the test like this:
CALL test_procedure(1,2)
It will result in the following output:
** DEBUG:
** my first debug message
** DEBUG:
** arg1:1
** DEBUG:
** This message always shows up
I do something very similar to you.
I'll usually include a DEBUG param that defaults to false and I can set to true at run time. Then wrap the debug statements into an "If DEBUG" block.
I also use a logging table with many of my jobs so that I can review processes and timing. My Debug code gets output there as well. I include the calling param name, a brief description, row counts affected (if appropriate), a comments field and a time stamp.
Good debugging tools is one of the sad failings of all SQL platforms.
How to debug a MySQL stored procedure.
Poor mans debugger:
Create a table called logtable with two columns, id INT and log VARCHAR(255).
Make the id column autoincrement.
Use this procedure:
delimiter //
DROP PROCEDURE `log_msg`//
CREATE PROCEDURE `log_msg`(msg VARCHAR(255))
BEGIN
insert into logtable select 0, msg;
END
Put this code anywhere you want to log a message to the table.
call log_msg(concat('myvar is: ', myvar, ' and myvar2 is: ', myvar2));
It's a nice quick and dirty little logger to figure out what is going on.
Yes, there is a specialized tools for this kind of thing - MySQL Debugger.
There are GUI tools for debugging stored procedures / functions and scripts in MySQL. A decent tool that dbForge Studio for MySQL, has rich functionality and stability.
Debugger for mysql was good but its not free. This is what i use now:
DELIMITER GO$
DROP PROCEDURE IF EXISTS resetLog
GO$
Create Procedure resetLog()
BEGIN
create table if not exists log (ts timestamp default current_timestamp, msg varchar(2048)) engine = myisam;
truncate table log;
END;
GO$
DROP PROCEDURE IF EXISTS doLog
GO$
Create Procedure doLog(in logMsg nvarchar(2048))
BEGIN
insert into log (msg) values(logMsg);
END;
GO$
Usage in stored procedure:
call dolog(concat_ws(': ','#simple_term_taxonomy_id', #simple_term_taxonomy_id));
usage of stored procedure:
call resetLog ();
call stored_proc();
select * from log;
Another way is presented here
http://gilfster.blogspot.co.at/2006/03/debugging-stored-procedures-in-mysql.html
with custom debug mySql procedures and logging tables.
You can also just place a simple select in your code and see if it is executed.
SELECT 'Message Text' AS `Title`;
I got this idea from
http://forums.mysql.com/read.php?99,78155,78225#msg-78225
Also somebody created a template for custom debug procedures on GitHub.
See here
http://www.bluegecko.net/mysql/debugging-stored-procedures/
https://github.com/CaptTofu/Stored-procedure-debugging-routines
Was mentioned here
How to catch any exception in triggers and store procedures for mysql?
I'm late to the party, but brought more beer:
http://ocelot.ca/blog/blog/2015/03/02/the-ocelotgui-debugger/
and
https://github.com/ocelot-inc/ocelotgui
I tried, and it seems pretty stable, supporting Breakpoints and Variable inspection.
It's not a complete suite (just 4,1 Mb) but helped me a lot!
How it works:
It integrates with your mysql client (I'm using Ubuntu 14.04), and after you execute:
$install
$setup yourFunctionName
It installs a new database at your server, that control the debugging process. So:
$debug yourFunctionName('yourParameter')
will give you a chance to step by step walk your code, and "refreshing" your variables you can better view what is going on inside your code.
Important Tip: while debugging, maybe you will change (re-create the procedure). After a re-creation, execute: $exit and $setup before a new $debug
This is an alternative to "insert" and "log" methods.
Your code remains free of additional "debug" instructions.
Screenshot:
I just simply place select statements in key areas of the stored procedure to check on current status of data sets, and then comment them out (--select...) or remove them before production.
MySQL Connector/Net 6.6 has a feature to Debug Stored Procedures and Functions
Installing the Debugger
To enable the stored procedure debugger:
For Connector/Net 6.6: Install Connector/Net 6.6 and choose the Complete option.
For Connector/Net 6.7 and later: Install the product MySQL for Visual Studio, to which the stored procedure debugger belongs.
Starting the Debugger
To start the debugger, follow these steps:
Choose a connection in the Visual Studio Server Explorer.
Expand the Stored Procedures folder. Only stored procedures can be debugged directly. To debug a user-defined function, create a stored
procedure that calls the function.
Click on a stored procedure node, then right-click and from the context menu choose Debug Routine.
MySql Connector/NET also includes a stored procedure debugger integrated in visual studio as of version 6.6,
You can get the installer and the source here:
http://dev.mysql.com/downloads/connector/net/
Some documentation / screenshots:
https://dev.mysql.com/doc/visual-studio/en/visual-studio-debugger.html
You can follow the annoucements here:
http://forums.mysql.com/read.php?38,561817,561817#msg-561817
UPDATE: The MySql for Visual Studio was split from Connector/NET into a separate product, you can pick it (including the debugger) from here https://dev.mysql.com/downloads/windows/visualstudio/1.2.html (still free & open source).
DISCLAIMER: I was the developer who authored the Stored procedures debugger engine for MySQL for Visual Studio product.
The first and stable debugger for MySQL is in dbForge Studio for MySQL
I had use two different tools to debug procedures and functions:
dbForge - many functional mysql GUI.
MyDebugger - specialized tool for debugging ... handy tool for debugging.vote http://tinyurl.com/voteimg
MySQL user defined variable (shared in session) could be used as logging output:
DELIMITER ;;
CREATE PROCEDURE Foo(tableName VARCHAR(128))
BEGIN
SET #stmt = CONCAT('SELECT * FROM ', tableName);
PREPARE pStmt FROM #stmt;
EXECUTE pStmt;
DEALLOCATE PREPARE pStmt;
-- uncomment after debugging to cleanup
-- SET #stmt = null;
END;;
DELIMITER ;
call Foo('foo');
select #stmt;
will output:
SELECT * FROM foo
Toad mysql. There is a freeware version
http://www.quest.com/toad-for-mysql/
Answer corresponding to this by #Brad Parks
Not sure about the MySQL version, but mine was 5.6, hence a little bit tweaking works:
I created a function debug_msg which is function (not procedure) and returns text(no character limit) and then call the function as SELECT debug_msg(params) AS my_res_set, code as below:
CREATE DEFINER=`root`#`localhost` FUNCTION `debug_msg`(`enabled` INT(11), `msg` TEXT) RETURNS text CHARSET latin1
READS SQL DATA
BEGIN
IF enabled=1 THEN
return concat('** DEBUG:', "** ", msg);
END IF;
END
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `proc_func_call`(
IN RegionID VARCHAR(20),
IN RepCurrency INT(11),
IN MGID INT(11),
IN VNC VARCHAR(255)
)
BEGIN
SET #enabled = TRUE;
SET #mainQuery = "SELECT * FROM Users u";
SELECT `debug_msg`(#enabled, #mainQuery) AS `debug_msg1`;
SET #lastQuery = CONCAT(#mainQuery, " WHERE u.age>30);
SELECT `debug_msg`(#enabled, #lastQuery) AS `debug_msg2`;
END $$
DELIMITER