How to fix MySqlException-“Packets larger than max_allowed_packet are not allowed.” while inserting BLOB data in MySQL database using C#

Introduction

In my previous articles I explained How to save images in MySQL database using C# in Windows Forms application and How to retrieve images from MySQL database and display as a slide show in Windows Forms application using C#. While inserting images more than 1 MB in MySQL BLOB column from C#, I got an error- “Packets larger than max_allowed_packet are not allowed”.  In this article I will discuss its causes and solution

Cause

By default the largest packet that can be sent to the MySQL server is 1 MB. If you try to insert more than 1 MB of data, you will get a MySqlException with message “Packets larger than max_allowed_packet are not allowed.” and ErrorCode -2147467259

Solution

  1. Open “my.ini” file configuration file from MySQL Server folder. In my system it is located in “C:MySQLMySQL Server 5.1”.
  2. Add max_allowed_packet=5M below the line containing [mysqld] if it is not already there. Where M represents megabytes
    [mysqld]
    max_allowed_packet=5M
  3. Restart MySQL service. You can use MySQL Administrator to restart MySql. You can start or stop MySQL service using “Service Control” tab in MySQL Administrator window.

2 thoughts on “How to fix MySqlException-“Packets larger than max_allowed_packet are not allowed.” while inserting BLOB data in MySQL database using C#

Leave a Reply

Your email address will not be published. Required fields are marked *