How to Export MySQL to CSV: Step-by-Step Guide


Building your pipeline or Using Airbyte
Airbyte is the only open source solution empowering data teams to meet all their growing custom business demands in the new AI era.
- Inconsistent and inaccurate data
- Laborious and expensive
- Brittle and inflexible
- Reliable and accurate
- Extensible and scalable for all your needs
- Deployed and governed your way
Start syncing with Airbyte in 3 easy steps within 10 minutes



Take a virtual tour
Demo video of Airbyte Cloud
Demo video of AI Connector Builder
What sets Airbyte Apart
Modern GenAI Workflows
Move Large Volumes, Fast
An Extensible Open-Source Standard
Full Control & Security
Fully Featured & Integrated
Enterprise Support with SLAs
What our users say


"The intake layer of Datadog’s self-serve analytics platform is largely built on Airbyte.Airbyte’s ease of use and extensibility allowed any team in the company to push their data into the platform - without assistance from the data team!"


“Airbyte helped us accelerate our progress by years, compared to our competitors. We don’t need to worry about connectors and focus on creating value for our users instead of building infrastructure. That’s priceless. The time and energy saved allows us to disrupt and grow faster.”


“We chose Airbyte for its ease of use, its pricing scalability and its absence of vendor lock-in. Having a lean team makes them our top criteria. The value of being able to scale and execute at a high level by maximizing resources is immense”
First, you need to connect to your MySQL server using the command-line interface or a MySQL client.
```
mysql -u username -p
```
Enter your password when prompted.
Choose the database you want to export data from.
```
USE your_database_name;
```
Write the SQL query to select the data you want to export. For example:
```
SELECT * FROM your_table_name;
```
Use the MySQL `INTO OUTFILE` clause to export the query results directly to a CSV file:
```
SELECT * FROM your_table_name
INTO OUTFILE '/path/to/output_file.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
```
Note: Make sure the MySQL server has write permissions to the specified output directory.
If you want to include column headers in your CSV, you can use a UNION query:
```
(SELECT 'column1', 'column2', 'column3')
UNION
(SELECT column1, column2, column3 FROM your_table_name)
INTO OUTFILE '/path/to/output_file.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
```
If your data contains special characters or line breaks, you may need to adjust the FIELDS and LINES clauses:
```
SELECT * FROM your_table_name
INTO OUTFILE '/path/to/output_file.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n';
```
Alternatively, you can export data directly from the command line without entering the MySQL prompt:
```
mysql -u username -p -e "SELECT * FROM your_database.your_table" -B | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > output_file.csv
```
Once the export is complete, verify that the CSV file has been created and contains the expected data.
For very large datasets, you may need to split the export into multiple files or use the `mysqldump` utility:
```
mysqldump -u username -p --tab=/path/to/output_directory your_database your_table
```
This will create a .sql file with the table structure and a .txt file with the data.
Depending on your needs, you may want to perform some post-processing on the CSV file, such as removing unnecessary quotes or adjusting date formats.
Remember to always be cautious when exporting sensitive data and ensure you have the necessary permissions to read from the database and write to the output directory.
This process allows you to export MySQL data to CSV without relying on third-party tools, using only built-in MySQL and command-line utilities.
FAQs
What is ETL?
ETL, an acronym for Extract, Transform, Load, is a vital data integration process. It involves extracting data from diverse sources, transforming it into a usable format, and loading it into a database, data warehouse or data lake. This process enables meaningful data analysis, enhancing business intelligence.
MySQL is an SQL (Structured Query Language)-based open-source database management system. An application with many uses, it offers a variety of products, from free MySQL downloads of the most recent iteration to support packages with full service support at the enterprise level. The MySQL server, while most often used as a web database, also supports e-commerce and data warehousing applications and more.
MySQL provides access to a wide range of data types, including:
1. Numeric data types: These include integers, decimals, and floating-point numbers.
2. String data types: These include character strings, binary strings, and text strings.
3. Date and time data types: These include date, time, datetime, and timestamp.
4. Boolean data types: These include true/false or yes/no values.
5. Spatial data types: These include points, lines, polygons, and other geometric shapes.
6. Large object data types: These include binary large objects (BLOBs) and character large objects (CLOBs).
7. Collection data types: These include arrays, sets, and maps.
8. User-defined data types: These are custom data types created by the user.
Overall, MySQL's API provides access to a wide range of data types, making it a versatile tool for managing and manipulating data in a variety of applications.
What is ELT?
ELT, standing for Extract, Load, Transform, is a modern take on the traditional ETL data integration process. In ELT, data is first extracted from various sources, loaded directly into a data warehouse, and then transformed. This approach enhances data processing speed, analytical flexibility and autonomy.
Difference between ETL and ELT?
ETL and ELT are critical data integration strategies with key differences. ETL (Extract, Transform, Load) transforms data before loading, ideal for structured data. In contrast, ELT (Extract, Load, Transform) loads data before transformation, perfect for processing large, diverse data sets in modern data warehouses. ELT is becoming the new standard as it offers a lot more flexibility and autonomy to data analysts.
What should you do next?
Hope you enjoyed the reading. Here are the 3 ways we can help you in your data journey: