Monday, May 21, 2012

Sonar analysis and mysql max_allowed_packet size



Recently I had the problem where sonar was failing my CI plans with the following error:


"Packet for query is too large (1363557 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable."


I wasn't sure whether this was the sonar server i.e. the web frontend or the mysql server that I was using for the backend database, however off to google and a couple of checks later outlines it being a mysql server with the best example being:


http://dev.mysql.com/doc/refman//5.5/en/set-statement.html


It's also worth checking first if this variable is actually able to be changed at runtime:


http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_max_allowed_packet


So from the above two links I was able then to go off to the mysql server and set the value:


First of all check that the current value is what is expected:


mysql> show variables like "max_allowed%";


| Variable_name                   | Value       |
| max_allowed_packet          | 1048576   |


1 row in set (0.00 sec)


Good, our variable is as expected from the error we received in the first place. Now from the first link above set the value to 16M:


mysql> SET GLOBAL max_allowed_packet=16*1024*1024;
Query OK, 0 rows affected (0.00 sec)


and verify:


mysql> show variables like "max_allowed%";


| Variable_name                  | Value         |
| max_allowed_packet        | 1048576    |


1 row in set (0.00 sec)




Eh, hold on - that's not right, seems like the multiplier isn't working, so off to the calculator to work it out ourselves and try again:


mysql> SET GLOBAL max_allowed_packet=16777216;
Query OK, 0 rows affected (0.00 sec)


mysql> show variables like "max_allowed%";


| Variable_name                | Value    |
| max_allowed_packet      | 16777216 |


1 row in set (0.00 sec)


Nice!


Since we changed this just in memory we need go to our my.cnf file to make sure the change is persisted at server restart:


my.cnf
[mysqld]
max_allowed_packet=16M


You should now not get the error on the sonar upload.

4 comments:

  1. Hello,

    I was getting the same error, and tried your solution. I was running the query using "sonar" user since I didn't know the password for root. and now I am getting following error

    ERROR 1227 (42000): Access denied; you need the SUPER privilege for this operation

    I don't know the root password for the mysql database on sonar. Any help will be appreciated.

    ReplyDelete
    Replies
    1. Hey,

      Typically the sonar database will be set up to allow a sonar user to write metrics to the database, however the above is setting global permissions on the database so therefore the sonar user will not have these privileges - this is a good thing!

      I'm afraid you'll either need the root password or access to the my.cnf file in order to changes this, you could ask your friendly neighborhood sonar database admin to change this for you if you ask nicely :)

      Delete
  2. Thanks for your reply. Changed the my.cnf file and added the max_allowed_packet variable. Happy is ending... :)

    ReplyDelete
  3. I had the same problem with the sonar log saying "Packet for query is too large (4396742 > 4194304)" with a configuration at 64M (max_allowed_packet=64M in my.cnf)....
    Don't forget to look at sonar side and the mysql JDBC connector. I tried to configure it through sonar.cnf (parameter on top of the jdbc URL) and it worked !
    sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?maxAllowedPacket=67108864&....

    ReplyDelete