Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store MySQL warnings in database?

I'd like to store warnings caused by some SQL statements in the database. E.g. after

mysql> select 1/0;
+------+
| 1/0  |
+------+
| NULL |
+------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+---------------+
| Level | Code | Message       |
+-------+------+---------------+
| Error | 1365 | Division by 0 |
+-------+------+---------------+
1 row in set (0.00 sec)

I'd like to save what SHOW WARNINGS says to some persistent table, to be able to analyze it in future.

Let's assume I know when there is something to log - I can check if @@warning_count > 0.

like image 578
ssobczak Avatar asked Apr 15 '10 16:04

ssobczak


People also ask

How do I show MySQL warnings?

The mysql client also has a number of options related to warnings. The \W command will show warnings after every statement, while \w will disable this. Starting the client with the --show-warnings option will show warnings after every statement.

What is warning in MySQL?

SHOW WARNINGS is a diagnostic statement that displays information about the conditions (errors, warnings, and notes) resulting from executing a statement in the current session.

What are ACLs in MySQL?

MySQL uses security based on Access Control Lists (ACLs) for all connections, queries, and other operations that users can attempt to perform. There is also support for SSL-encrypted connections between MySQL clients and servers.

How do I ignore MySQL warnings?

To suppress warnings, set SQL_NOTES=0.


1 Answers

You can accomplish this within a stored procedure by analyzing the diagnostics. For example, the following handler for warnings could be declared by the following

  DECLARE CONTINUE HANDLER FOR SQLWARNING 
  BEGIN 
    GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE, 
         @errno = MYSQL_ERRNO, @message = MESSAGE_TEXT
    ;

    /* select the following into your debugging table */
    SELECT @errno, @sqlstate, @message;
  END;

More information about DIAGNOSTICS can be found here: https://dev.mysql.com/doc/refman/5.6/en/get-diagnostics.html

like image 135
Napoli Avatar answered Oct 01 '22 19:10

Napoli