1. Introduction
CSV (Comma Separated Values) is a widely used format for data export, migration, and backup. MySQL comes with built-in support for exporting data in CSV format, which makes data management and analysis more efficient. In this article, we’ll explain in detail how to export data as CSV using MySQL, highlight the differences between versions, discuss how to handle error messages, and cover important security considerations.
Execution Environment
This article is based on MySQL 8.0, but it also covers differences when using MySQL 5.x. Since behavior and settings can vary between versions, make sure to follow the correct procedure for the version you’re working with.
2. Basic Steps to Export CSV in MySQL
To export data in CSV format with MySQL, you can use the SELECT INTO OUTFILE
command. This is the standard method for saving query results into a CSV file.
2.1 Basic Syntax
SELECT * FROM table_name INTO OUTFILE '/path/to/file.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
2.2 Command Details
SELECT *
: Selects all data from the table. Specify column names if you only want to export certain columns.INTO OUTFILE
: Saves the query results to a file at the given path. The path must be an absolute path.FIELDS TERMINATED BY ','
: Defines the comma as the delimiter between columns.ENCLOSED BY '"'
: Wraps each field in double quotes. This ensures proper handling when data contains commas or line breaks.LINES TERMINATED BY '\n'
: Defines a newline as the line terminator. On Windows,'\r\n'
may be used instead.

3. Version-Specific Differences
3.1 Differences Between MySQL 5.x and 8.x
There are several important differences between MySQL 5.x and 8.x, particularly related to encoding and file output. Key points include:
- Encoding Handling:
- In MySQL 5.x, the default
utf8
encoding only supports up to 3-byte characters, which means some special characters and emojis may not be handled correctly. Usingutf8mb4
is recommended, but support is limited in 5.x. - In MySQL 8.x,
utf8mb4
is the default encoding, fully supporting emojis and all multibyte characters. - Enhanced
secure_file_priv
: - In MySQL 8.x, file writing is more strictly controlled by
secure_file_priv
. Attempting to write outside permitted directories will trigger an error. - While 5.x also supports this, restrictions are looser, so additional configuration may be required.
3.2 CSV Export Performance
MySQL 8.x offers improved performance, especially when exporting large datasets to CSV. Although CSV export works in 5.x as well, the optimizations in 8.x provide faster and more efficient output.
4. Key Considerations for CSV Export
4.1 File Write Permissions and secure_file_priv
The secure_file_priv
setting restricts which directories MySQL can access for file operations. To check its configuration, run:
SHOW VARIABLES LIKE 'secure_file_priv';
If you attempt to write outside the permitted directory, you will see an error message.
4.2 Encoding Issues
When exporting data containing multibyte or special characters (such as Japanese text or emojis), encoding settings are crucial. Using utf8mb4
ensures proper output. While MySQL 5.x often defaults to utf8
, upgrading to 8.x makes encoding issues much easier to avoid.

5. Common Error Messages and Solutions
5.1 secure_file_priv
Error
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement.
This occurs when trying to write to a directory not permitted by secure_file_priv
. Export to an allowed directory or adjust the setting.
5.2 Permission Denied Error
ERROR 13 (HY000): Can't get stat of '/path/to/file.csv' (Errcode: 13 - Permission denied)
This indicates insufficient write permissions. Set proper permissions using:
sudo chmod 755 /path/to/directory
Security Note: Avoid chmod 777
, as giving write access to all users is a security risk. Always use the minimum required permissions.
6. Additional Security Considerations
6.1 Managing File Permissions
When exporting CSV files, always apply the least privilege principle. On public servers, granting excessive permissions creates security risks. Use restricted permissions such as chmod 755
, ensuring only admins or specific users can access the files.
6.2 Using secure_file_priv
secure_file_priv
restricts MySQL’s file read/write operations to designated directories, reducing risks of data leaks and unauthorized access. This setting is managed in MySQL’s configuration file (my.cnf
or my.ini
) and should be explicitly configured for better security.

7. Conclusion
Exporting CSV files with MySQL is highly useful for data migration and backup, but differences in functionality and performance exist between versions. MySQL 8.x brings better optimization and stronger security, including improvements in encoding and directory restrictions for CSV output.
Meanwhile, MySQL 5.x requires careful handling of encoding and secure_file_priv
settings. Using utf8mb4
and applying proper security measures ensures reliable CSV export.
By configuring file permissions correctly and leveraging secure_file_priv
, you can minimize risks of data leaks or unauthorized access. On public servers, always apply minimal permissions (e.g., chmod 755
) to ensure only authorized users can access the files.
7.1 Key Best Practices
- Understand version differences: Be aware of the differences between MySQL 5.x and 8.x, especially in encoding and file export behavior.
- Apply proper permissions: Avoid excessive permissions. Instead of
chmod 777
, use restricted permissions such aschmod 755
. - Use
secure_file_priv
: Configuresecure_file_priv
to restrict MySQL’s file access, reducing security risks. - Check encoding: For CSV exports with multibyte characters or emojis, always use
utf8mb4
.
By following these practices, you can use MySQL’s CSV export feature safely and efficiently.