The first step is using MySQL with BLOB
data
is to configure the server. To start, create a table that can be
accessed. File tables often have four columns: an
AUTO_INCREMENT
column of appropriate size
(UNSIGNED SMALLINT
) to serve as a primary key
to identify the file, a VARCHAR
column that
stores the file name, an UNSIGNED MEDIUMINT
column that stores the size of the file, and a
MEDIUMBLOB
column that stores the file
itself. For this example, use the following table definition:
CREATE TABLE file(
file_id SMALLINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
file_name VARCHAR(64) NOT NULL,
file_size MEDIUMINT UNSIGNED NOT NULL,
file MEDIUMBLOB NOT NULL);
After creating a table, you might need to modify the
max_allowed_packet
system variable. This
variable determines how large of a packet (that is, a single
row) can be sent to the MySQL server. By default, the server
only accepts a maximum size of 1MB from the client application.
If you intend to exceed 1MB in your file transfers, increase
this number.
The max_allowed_packet
option can be modified
using the MySQL Workbench Server Administration
screen. Adjust the Maximum permitted option in the
Data / Memory size section of the
Networking tab to an appropriate setting. After adjusting the
value, click the button and restart
the server using the Startup / Shutdown
screen of MySQL Workbench. You can also adjust this value directly
in the my.cnf
file (add a line that reads
max_allowed_packet=
),
or use the xx
MSET
max_allowed_packet=
syntax from within MySQL.
xx
M;
Try to be conservative when setting
max_allowed_packet
, as transfers of BLOB data
can take some time to complete. Try to set a value that will be
adequate for your intended use and increase the value if
necessary.