Tutorial On Using Database In Magento 2 Media Files Storage

In the default installation of Magento 2 Open Source, images, compiled CSS files, and JS files of the Magento instance are saved in the file system on the server. Store administrators can store these files in a database on a database server. This method allows you to select between auto-sync and reverse-sync between the web server file system and the database. You can use the default database to store media or create a new one. To use a newly created database as media storage, you must include information about it and its access credentials in the local .xml file.

The database workflow of using database In Magento 2 media files storage below:

  1. Browser requests media: The browser requests the media that is specified in the HTML while opening a page in the store on the browser.
  2. The system looks for media in the file system: The media files will be searched and found in the file system and send to the browser.
  3. The system locates media in database: If the file is not available in the system, a request is sent to the database that is specified in the configuration.
  4. The system locates media in database: A PHP script sends the files from the database to the file system and sent to the browser.

If the server is supported and web server rewrites are enabled for Magento, the PHP script runs only when the requested media files are not in the file system.

If the server is not supported or web server rewrites are disabled for Magento, the PHP script runs even if the requested media files exist in the file system.

To configure a database for media storage, please follow these steps:

On the admin sidebar click Stores, under Settings click Configuration, under Advanced, click System. To apply the configuration at the global level, set Store View to Default Config. Open the Storage Configuration for Media tab and follow these steps:

Storage Configuration for Media (Database)
Storage Configuration for Media (Database)
  • Uncheck the Use system value checkbox
  • Set Media Storage to “Database.”
  • Set Select Media Database to the database you want to use.
  • Click Synchronize, enter the Environment Update Time and click Save Config.

In the next article, we will describe tutorial on using a CDN as a media storage for Magento 2 online store. Keep tracking Magestandard and see you in the next blog post.