Skip to content

Using SELECT INTO OUTFILE in MySQL

The SELECT INTO OUTFILE statement in MySQL allows you to write the results of a query directly to a file. You can also customize column and row delimiters to format the output.

  • Writes query results to a file on the server.
  • Allows custom column and row terminators for formatting.
  • Unmanaged hosting packages: Fully supported since you have control over MySQL privileges and configuration.
  • Shared or reseller hosting: Not supported. The FILE privilege is required, which is usually disabled for security reasons.

If your hosting package does not allow SELECT INTO OUTFILE, you can still save query results to a file using the command line:

echo "sql_query;" | mysql --user=mysql_username --password=mysql_password mysql_database > /home/username/query.txt
  • sql_query: The SQL statement you want to run (e.g., SELECT * FROM employees;).
  • mysql_username: MySQL username for the database.
  • mysql_password: Password for the MySQL user.
  • mysql_database: Name of the database to access.
  • username: Your hosting account username.

You can change the file path or query to suit your needs.

Official documentation:
https://mariadb.com/kb/en/select-into-outfile