{"id":27504,"date":"2025-04-22T08:34:22","date_gmt":"2025-04-22T08:34:22","guid":{"rendered":"http:\/\/www.withdata.com\/blog\/?p=27504"},"modified":"2025-10-17T03:45:20","modified_gmt":"2025-10-17T03:45:20","slug":"mysql-blob","status":"publish","type":"post","link":"https:\/\/www.withdata.com\/blog\/blob\/mysql-blob.html","title":{"rendered":"MySQL BLOB"},"content":{"rendered":"<p>In MySQL, the BLOB (Binary Large Object) data types are used to store large amounts of binary data, such as images, audio, and video files.<\/p>\n<h2>MySQL provides four different BLOB data types, each with different maximum storage capacities:<\/h2>\n<p>TINYBLOB: Can store up to 255 bytes of binary data.<br \/>\nBLOB: Can store up to 65,535 bytes (64 KB) of binary data.<br \/>\nMEDIUMBLOB: Can store up to 16,777,215 bytes (16 MB) of binary data.<br \/>\nLONGBLOB: Can store up to 4,294,967,295 bytes (4 GB) of binary data.<\/p>\n<h3>Creating a Table with a BLOB Column<\/h3>\n<p>The following SQL code creates a table named media_files with a BLOB column to store binary data related to media files:<br \/>\n<code><br \/>\nCREATE TABLE media_files (<br \/>\nid INT AUTO_INCREMENT PRIMARY KEY,<br \/>\nfile_name VARCHAR(255),<br \/>\nfile_content BLOB<br \/>\n);<br \/>\n<\/code><\/p>\n<h3>Inserting Binary Data into a BLOB Column<\/h3>\n<p>You can insert binary data into a BLOB column in different ways. One common method is using programming languages. Here is an example using Python and the mysql-connector-python library:<\/p>\n<pre>import mysql.connector\r\nimport os\r\n\r\n# Connect to the MySQL database\r\nmydb = mysql.connector.connect(\r\n    host=\"localhost\",\r\n    user=\"your_username\",\r\n    password=\"your_password\",\r\n    database=\"your_database\"\r\n)\r\n\r\nmycursor = mydb.cursor()\r\n\r\n# Read the binary file\r\nfile_path = 'example.jpg'\r\nwith open(file_path, 'rb') as file:\r\n    binary_data = file.read()\r\n\r\n# Insert the binary data into the table\r\nsql = \"INSERT INTO media_files (file_name, file_content) VALUES (%s, %s)\"\r\nval = (os.path.basename(file_path), binary_data)\r\nmycursor.execute(sql, val)\r\n\r\nmydb.commit()\r\n\r\nprint(mycursor.rowcount, \"record inserted.\")\r\n\r\n<\/pre>\n<h3>Querying Binary Data from a BLOB Column<\/h3>\n<p>You can query the binary data stored in a BLOB column and save it to a file. Here is a Python example:<\/p>\n<pre>import mysql.connector\r\n\r\n# Connect to the MySQL database\r\nmydb = mysql.connector.connect(\r\n    host=\"localhost\",\r\n    user=\"your_username\",\r\n    password=\"your_password\",\r\n    database=\"your_database\"\r\n)\r\n\r\nmycursor = mydb.cursor()\r\n\r\n# Query the binary data\r\nsql = \"SELECT file_content FROM media_files WHERE id = 1\"\r\nmycursor.execute(sql)\r\n\r\nresult = mycursor.fetchone()\r\n\r\nif result:\r\n    binary_data = result[0]\r\n    # Save the binary data to a file\r\n    with open('retrieved.jpg', 'wb') as file:\r\n        file.write(binary_data)\r\n    print(\"File retrieved successfully.\")\r\n\r\n<\/pre>\n<p><strong>Storage<\/strong>: Storing large BLOB data can significantly increase the size of your database. You may need to manage your storage space carefully. An alternative approach is to store the files on the file system and keep only the file paths in the database.<br \/>\n<strong>Performance<\/strong>: Inserting and retrieving large BLOB data can be slow, especially if there are many concurrent operations. You may need to optimize your database configuration, such as adjusting buffer sizes and using appropriate storage engines.<\/p>\n<h2>Here are some common use cases for storing binary data using the MySQL BLOB data type:<\/h2>\n<h3>1. Storing Images<\/h3>\n<p><a href=\"https:\/\/www.withdata.com\/blog\/blob\/store-images-in-mysql-blob.html\">Images can be stored as MySQL BLOB data<\/a>.<\/p>\n<h3>2. Audio and Video Storage<\/h3>\n<p>You can store the media (<a href=\"https:\/\/www.withdata.com\/blog\/blob\/store-audio-files-in-mysql-blob.html\">Audio<\/a> and <a href=\"https:\/\/www.withdata.com\/blog\/blob\/store-video-files-in-mysql-blob.html\">Video<\/a>) files MySQL BLOB data.<\/p>\n<h3>3. Document Storage<\/h3>\n<p>You can store various types of documents such as <a href=\"https:\/\/www.withdata.com\/blog\/blob\/store-pdf-files-in-mysql-blob.html\">PDFs<\/a>, <a href=\"https:\/\/www.withdata.com\/blog\/blob\/store-word-documents-in-mysql-blob.html\">Word documents<\/a>, and <a href=\"https:\/\/www.withdata.com\/blog\/blob\/store-excel-spreadsheets-in-mysql-blob.html\">Excel spreadsheets<\/a> MySQL BLOB data.<\/p>\n<p>In summary, MySQL&#8217;s BLOB data types provide a way to handle large binary objects, but you need to be aware of the storage and performance implications when using them.<\/p>\n<p>See also: <a href=\"\/tools\/mysql\/blob\">Some tools for MySQL BLOB<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MySQL, the BLOB (Binary Large Object) data types are used to store large amounts of binary data, such as images, audio, and video files. MySQL provides four different BLOB data types, each with different maximum storage capacities: TINYBLOB: Can store up to 255 bytes of binary data. BLOB: Can store up to 65,535 bytes [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4524],"tags":[97,1323],"class_list":["post-27504","post","type-post","status-publish","format-standard","hentry","category-blob","tag-blob","tag-mysql"],"_links":{"self":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/27504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/comments?post=27504"}],"version-history":[{"count":6,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/27504\/revisions"}],"predecessor-version":[{"id":40209,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/27504\/revisions\/40209"}],"wp:attachment":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/media?parent=27504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/categories?post=27504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/tags?post=27504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}