How do you debug MySQL stored procedures?

后端 未结 16 1175
旧时难觅i
旧时难觅i 2020-11-30 17:44

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. Thi

相关标签:
16条回答
  • 2020-11-30 17:49

    Yes, there is a specialized tools for this kind of thing - MySQL Debugger.
    enter image description here

    0 讨论(0)
  • 2020-11-30 17:50

    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:

    0 讨论(0)
  • 2020-11-30 17:54

    Toad mysql. There is a freeware version http://www.quest.com/toad-for-mysql/

    0 讨论(0)
  • 2020-11-30 17:56

    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.

    0 讨论(0)
  • 2020-11-30 17:56

    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
    
    0 讨论(0)
  • 2020-11-30 17:57

    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;
    
    0 讨论(0)
提交回复
热议问题