BLOB (Binary Large Object) Files and RDBMS
Total Page:16
File Type:pdf, Size:1020Kb
BLOB (Binary Large Object) Files and RDBMS It turns out that simply adding a binary file to a database is a controversial topic as a quick google search on “inserting image into mysql” will reveal. The issue is that binary image files are inherently unstructured data and for security reasons and efficiency, it can be detrimental to the database to allow long binary large objects (e.g. image files) to be inserted into the database. The alternative is to keep unstructured file data in a parallel file system and store only name space locating information for the unstructured data (the path and file name). This however can also lead to headaches such as forgetting to back up the file system as well as the database, having inconsistencies between the database and the file system unless an application is used to maintain both carefully. Many IT organizations, including ERAU PRClab IT, may prevent BLOBs from being added to the DB or dumped. E.g. This means that file load and dumping is limited to a specific file system location: So, at ERAU, we have no viable option to test loading BLOBs at this time. IT also does not allow any reads or writes from /var/lib/mysql-files/ directory, so the file system has disallowed us to put any files here to load, but we can save files there form an SQL command line, but this is not of much help. Numerous security related, performance and general server configuration system variables can be found in the MySQL documentation - https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html Research this a bit more, we see we are stuck based on the documentation for secure_file_priv - https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_secure_file_priv Apparently we previously had this set to NULL, which allowed for loading and dumping of files from anywhere to anywhere on the PRClab system. The point of BLOB insertion is for you to do research on the Oracle SQL manual pages and to do your best to “figure it out” and document what you’re able to do and what does not work for you. However, let me share how I was able to insert BLOBs so you can give it a try with a little more help in case you are stuck and would like to see if you can get further. There are some interesting issues with types used, limitations of the CLI compared to Adminer, etc. that I just had to figure out myself by reading MySQL manual pages. It’s all documented on the Oracle MySQL site, but takes a bit of experimenting to get it to work – there are 3 key aspects noted below: I don’t want to answer the question for you, but here’s some tips: 1) Use LONGBLOB – note that MySQL has several BLOB and CLOB types - http://dev.mysql.com/doc/refman/5.0/en/blob.html (this way you won’t be as limited by size constraints) 2) Create your table on the MySQL command line and include LONGBLOB with an attribute like “jpg” for JPEG, “png” for PNG, etc. – works best when you match the attribute in the table to the commonly used file extension attributes - http://dev.mysql.com/doc/refman/5.1/en/create-table.html, so that when you view, the file extension matches the column attribute 3) You should be able to insert a BLOB into a table with commands like this (as well as using “New Item” in Adminer once the table is created) – mysql> use myblobs; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +-------------------+ | Tables_in_myblobs | +-------------------+ | animals | +-------------------+ 1 row in set (0.00 sec) mysql> describe animals; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(32) | NO | | NULL | | | ppm | longblob | YES | | NULL | | | jpg | longblob | YES | | NULL | | | png | longblob | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> insert into animals values('Cow', NULL, load_file('example-images/Cow.jpg'), NULL); Query OK, 1 row affected (0.01 sec) mysql> select name from animals; +-------------------+ | name | +-------------------+ | Baby Musk Ox | | Moose | | Shiba Inu - Hoshi | | Arctic Swan | | Sled Dogs | | Cow | +-------------------+ 6 rows in set (0.00 sec) For some reason, load_file does not seem to work on the CLI (likely because FILE has to be a granted privilege and it’s not) – it shows no errors, but looking at PRClab Adminer, I see a NULL for all 3 of my image types: However, with a properly created table, the PHP Adminer interface allows you to browse and load a file of the right type with no issue, just using “edit” – e.g. After browsing and selecting Cow.jpg, now the Cow image is in the BLOB: And I can view it from Adminer as well (beware of select including the BLOB columns in the CLI – you get a screenload of binary data back – yikes!). If I add FILE privilege to your account, I suspect you can enter BLOBs via a file upload, but image loading and viewing is much more convenient via Adminer anyway. Here’s a few more I’ve done: Here’s what I got assuming table was created with the proper type: And when I access the data for photo, I get a file that I can save or view with an appropriate tool (e.g. I use Irfanview for images): I loaded a PPM format image file, so I should have made the attribute name PPM, because the attribute is appended as the file extension, but Irfanview cleverly handles this: .