Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print debugging info from stored procedure in MySQL

Is there a way in MySQL to print debugging messages to stdout, temptable or logfile? Something like:

  • print in SQLServer
  • DBMS_OUTPUT.PUT_LINE in Oracle
like image 734
Peanut Avatar asked Jul 23 '10 01:07

Peanut


People also ask

How do I print a stored procedure value in MySQL?

Option 1: Put this in your procedure to print 'comment' to stdout when it runs. SELECT 'Comment'; Option 2: Put this in your procedure to print a variable with it to stdout: declare myvar INT default 0; SET myvar = 5; SELECT concat('myvar is ', myvar);

How do I debug a stored procedure?

To debugging SP, go to database->Programmability->Stored Procedures-> right click the procedure you want to debug->select Debug Procedure.


2 Answers

Option 1: Put this in your procedure to print 'comment' to stdout when it runs.

SELECT 'Comment'; 

Option 2: Put this in your procedure to print a variable with it to stdout:

declare myvar INT default 0; SET myvar = 5; SELECT concat('myvar is ', myvar); 

This prints myvar is 5 to stdout when the procedure runs.

Option 3, Create a table with one text column called tmptable, and push messages to it:

declare myvar INT default 0; SET myvar = 5; insert into tmptable select concat('myvar is ', myvar); 

You could put the above in a stored procedure, so all you would have to write is this:

CALL log(concat('the value is', myvar)); 

Which saves a few keystrokes.

Option 4, Log messages to file

select "penguin" as log into outfile '/tmp/result.txt'; 

There is very heavy restrictions on this command. You can only write the outfile to areas on disk that give the 'others' group create and write permissions. It should work saving it out to /tmp directory.

Also once you write the outfile, you can't overwrite it. This is to prevent crackers from rooting your box just because they have SQL injected your website and can run arbitrary commands in MySQL.

like image 50
Patrick Avatar answered Sep 29 '22 11:09

Patrick


Quick way to print something is:

select '** Place your mesage here' AS '** DEBUG:'; 
like image 33
Roberto Rodriguez Avatar answered Sep 29 '22 10:09

Roberto Rodriguez