MySQL File Read with LOAD_FILE
The MySQL LOAD_FILE() function reads a file from the server filesystem and returns its contents as a string. With FILE privilege and no secure_file_priv restriction, this reads any file the MySQL process user can access. On a typical Linux deployment the MySQL process runs as the mysql OS user which can read world-readable files including most configuration files, application source, and credential stores. This technique requires no shell access and works entirely through SQL queries.
Confirm FILE privilege is present and secure_file_priv is empty before proceeding. See the FILE privilege detection page.
Read Standard System Files
Start with files that are always readable and confirm the technique works:
root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin mysql:x:112:117:MySQL Server,,,:/nonexistent:/bin/false
Read Application Configuration Files
Application config files typically contain database credentials, API keys, and internal service addresses. Target common locations based on what the web server is running:
<?php
define('DB_NAME', 'wordpress');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'SuperSecret123!');
define('DB_HOST', 'localhost');
Read MySQL Configuration and Credential Files
The MySQL configuration file and credential files on the host may contain additional credentials or replication passwords:
Read SSH Keys and Sensitive Home Directory Files
Save Output to a Local File
For large files or automated collection, redirect output directly to a local file:
-s suppresses the column header and -N omits the field names, giving clean file content output suitable for saving directly.
Troubleshoot NULL Returns
If LOAD_FILE returns NULL despite FILE privilege being confirmed, check the following:
A non-empty secure_file_priv restricts reads to a specific directory. A low max_allowed_packet silently truncates or blocks large file reads. If the path is restricted, target files within the allowed path — often /var/lib/mysql-files/ on modern installations. If max_allowed_packet is the issue, increase it in the session: SET GLOBAL max_allowed_packet=1073741824; (requires SUPER or SYSTEM_VARIABLES_ADMIN privilege).
References
-
MySQL — LOAD_FILE function referencedev.mysql.com/doc/refman/8.0/en/string-functions.html#function_load-file (opens in new tab)
LOAD_FILE behavior, NULL conditions, and privilege requirements
-
MySQL — secure_file_priv variabledev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_secure_file_priv (opens in new tab)
Path restriction behavior affecting LOAD_FILE and INTO OUTFILE
Was this helpful?
Your feedback helps improve this page.