How to Import CSV into MySQL: 4 Easy Methods (Step-by-Step Guide)
Need to import CSV into MySQL? Whether you’re migrating data from Excel, loading product catalogs, or feeding analytics into your database, this guide covers four proven methods that work for beginners and pros alike.
I’ll show you how to use phpMyAdmin (easiest), MySQL Workbench (visual), LOAD DATA INFILE (fastest for large files), and the command line (automation). By the end, you’ll be able to import any CSV file into MySQL in minutes.
What You’ll Learn
- Method 1: phpMyAdmin – Best for Beginners
- Method 2: MySQL Workbench – Visual Import Wizard
- Method 3: LOAD DATA INFILE – Fastest for Large Files
- Method 4: Command Line (mysqlimport) – For Automation
- Method Comparison Table
- Common Import Errors & Solutions
- Frequently Asked Questions
Before You Begin: Prepare Your CSV File
Proper preparation prevents import problems. Follow these three steps before any import:
- Save as UTF-8 – Open your CSV in Notepad++ or VS Code and save with UTF-8 encoding (without BOM). This prevents character corruption.
- Check headers – Ensure the first row contains column names that match your MySQL table (or let the import create the table). Avoid spaces or special characters in headers.
- Handle delimiters – Standard comma is fine, but if your data contains commas, use a pipe
|or tab as delimiter.
Now let’s dive into the four methods.
Method 1: How to Import CSV into MySQL Using phpMyAdmin (Easiest)
If you use cPanel or any shared hosting, phpMyAdmin is probably already installed. It’s the most beginner-friendly way to import CSV into MySQL.
Step-by-Step:
- Log into phpMyAdmin (usually via cPanel or your hosting control panel).
- Select your target database from the left sidebar.
- Click on the Import tab at the top.
- Under File to import, click Choose File and select your CSV file.
- In the Format dropdown, select CSV (not SQL).
- Scroll to Format-Specific Options:
- Columns separated with:
,(or your delimiter) - Columns enclosed with:
" - Columns escaped with:
\ - Lines terminated with:
auto - Skip this number of rows (for header):
1(if first row has column names)
- Columns separated with:
- Check “Use the first row as column names” if applicable.
- Click Import at the bottom.
That’s it! phpMyAdmin will create the table (if it doesn’t exist) or append data to an existing table. This method works perfectly for CSV files up to about 50MB (depending on server limits).
Pro tip: If your CSV is larger than 50MB, increase upload_max_filesize and post_max_size in your php.ini, or use one of the other methods below.
Method 2: MySQL Workbench – Visual Import Wizard
MySQL Workbench is the official GUI tool for MySQL. It includes a powerful Table Data Import Wizard that handles CSV, JSON, and other formats.
Step-by-Step:
- Open MySQL Workbench and connect to your database instance.
- In the Navigator panel (left side), right-click on your target schema/database.
- Select Table Data Import Wizard.
- Browse and select your CSV file, then click Next.
- Choose an existing table or let the wizard create a new one. If creating new, you can adjust column names and data types.
- Map the CSV columns to table columns. The wizard auto-detects data types, but you can override them.
- Click Next – the wizard shows a preview of the import.
- Click Next again to start the import. Progress bar shows each row being inserted.
- After completion, click Finish.
MySQL Workbench is ideal for files up to 100-200MB. It gives you full control over data type mapping and handles errors gracefully.
If you need to import a CSV into an existing table, make sure the column order in the CSV matches the table’s column order, or use the mapping feature to align them.
Method 3: LOAD DATA INFILE – Fastest for Large Files
For CSV files larger than 200MB (or even gigabytes), the LOAD DATA INFILE command is the fastest method to import CSV into MySQL. It runs directly on the MySQL server and can import millions of rows in seconds.
Basic Syntax:
LOAD DATA INFILE '/var/lib/mysql-files/data.csv'
INTO TABLE your_table
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
Important notes:
- The file path must be accessible to the MySQL server. By default, MySQL restricts to the
secure-file-privdirectory (e.g.,/var/lib/mysql-files/on Linux,C:\ProgramData\MySQL\MySQL Server\Uploads\on Windows). - If you get “The MySQL server is running with the –secure-file-priv option”, check your directory with
SHOW VARIABLES LIKE 'secure_file_priv'; - Use
IGNORE 1 ROWSto skip the header row.
Advanced Example with Data Transformation:
LOAD DATA INFILE '/var/lib/mysql-files/sales.csv'
INTO TABLE sales
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(@order_date, customer_name, amount)
SET order_date = STR_TO_DATE(@order_date, '%m/%d/%Y'),
amount = REPLACE(amount, '$', '');
This example converts a date string to MySQL date format and removes dollar signs from a numeric column – all during the import. You can add any MySQL expression in the SET clause.
Performance tip: Disable indexes and foreign key checks before running LOAD DATA INFILE on very large files:
SET FOREIGN_KEY_CHECKS = 0;
ALTER TABLE your_table DISABLE KEYS;
-- Run LOAD DATA INFILE here
ALTER TABLE your_table ENABLE KEYS;
SET FOREIGN_KEY_CHECKS = 1;
Method 4: Command Line (mysqlimport) – For Automation
If you need to import CSV into MySQL regularly (e.g., daily cron job), the mysqlimport command-line utility is your friend. It’s essentially a wrapper around LOAD DATA INFILE.
Basic Syntax:
mysqlimport --ignore-lines=1 \
--fields-terminated-by=',' \
--fields-enclosed-by='"' \
--lines-terminated-by='\n' \
--user=root --password=yourpass \
your_database /path/to/file.csv
Important: The CSV filename must match the table name. For example, users.csv will import into the users table. The file extension is ignored – the base name becomes the table name.
You can add this command to a shell script and schedule it with cron (Linux) or Task Scheduler (Windows). Perfect for automated ETL pipelines.
For CSV files with a different name than the table, use the --use-thread or simply rename the file before import.
Method Comparison: Which One Should You Use?
| Method | Best For | File Size | Ease of Use | Automation? |
|---|---|---|---|---|
| phpMyAdmin | Beginners, shared hosting | < 50MB | Very easy (GUI) | No |
| MySQL Workbench | Visual mapping, medium files | < 200MB | Easy (wizard) | No |
| LOAD DATA INFILE | Large files, speed | Any size | Moderate (SQL) | Yes |
| mysqlimport | Recurring automation | Any size | Moderate (command line) | Yes |
Common CSV Import Errors & How to Fix Them
| Error Message | Likely Cause | Solution |
|---|---|---|
Error 1290: The MySQL server is running with --secure-file-priv | File not in allowed directory | Move CSV to the directory shown by SHOW VARIABLES LIKE 'secure_file_priv'; or disable secure-file-priv (not recommended for production). |
Error 1366: Incorrect string value | Encoding mismatch (UTF-8 vs Latin1) | Save CSV as UTF-8 without BOM. Set table/column charset to utf8mb4. Use CHARACTER SET utf8mb4 in LOAD DATA INFILE. |
Error 1262: Row was truncated | Column count mismatch or delimiter issue | Check CSV delimiter (use a text editor to view raw). Ensure number of columns matches table. |
Error 1062: Duplicate entry for primary key | CSV contains duplicate primary key values | Use IGNORE keyword: LOAD DATA INFILE ... IGNORE INTO TABLE or clean duplicates beforehand. |
| phpMyAdmin import hangs or times out | File too large or server limits | Split CSV into smaller chunks (e.g., 10,000 rows per file) or use LOAD DATA INFILE. |
Frequently Asked Questions (People Also Ask)
How to import CSV into MySQL with headers?
In phpMyAdmin, check “Use the first row as column names”. In LOAD DATA INFILE, add IGNORE 1 ROWS. In MySQL Workbench, the wizard auto-detects headers.
What is the fastest way to import large CSV into MySQL?
LOAD DATA INFILE is the fastest method, often 10-100x faster than INSERT statements. For a 1GB CSV, it typically takes under a minute on decent hardware.
Can I import CSV into MySQL without phpMyAdmin?
Absolutely. Use MySQL Workbench, LOAD DATA INFILE, or command-line mysqlimport. All are covered in this guide.
How to handle commas inside CSV fields when importing to MySQL?
Enclose fields containing commas in double quotes. Then specify ENCLOSED BY '"' in LOAD DATA INFILE. phpMyAdmin and Workbench handle this automatically.
How to import CSV into MySQL from remote server or cloud storage?
First download the CSV to the MySQL server’s allowed directory, then run LOAD DATA INFILE. For automation, use tools like Skyvia (cloud ETL) or write a script that downloads and imports.
Pro Tips for CSV to MySQL Imports
- Use staging tables: Import into a temporary table first, validate/clean data, then insert into the final table. This prevents corrupting production data.
- Index after import: For huge imports, drop indexes before LOAD DATA INFILE, then recreate them after. This can cut import time by 50% or more.
- Monitor progress: For very large imports, use
SHOW PROCESSLIST;to see if the import is still running. - Use
REPLACEorIGNORE: AddREPLACEkeyword to overwrite duplicate keys, orIGNOREto skip them.
Internal & External Resources
Internal Links (from your site):
- If you prefer a general guide, start with CSV to SQL: Ultimate Pillar Guide.
- For SQL Server users, see How to Import CSV into SQL Server.
- Looking for a CSV to SQL converter tool? We compared the top 5.
External High-Authority References:
- MySQL Official Documentation: LOAD DATA INFILE
- MySQL Official: mysqlimport Utility
- Stack Overflow – Import CSV into MySQL (community solutions)
- W3Schools: SQL Bulk Insert (general concept)
Conclusion
Now you have four battle-tested methods to import CSV into MySQL. Start with phpMyAdmin if you’re a beginner or on shared hosting. Move to MySQL Workbench for more control. For large files or automation, use LOAD DATA INFILE or mysqlimport.
Remember: Always back up your database before running any import on production data. Test with a small sample first. And if you run into errors, refer to our troubleshooting table above – it covers 95% of common issues.
If this guide helped you, bookmark it and share with your team. Have a specific CSV import problem not covered? Leave a comment below – I reply within 24 hours.
Ready to import your first CSV? Pick a method and get started now!
Pingback: CSV to SQL Masterclass: Complete Guide for 2025 (9-in-1 Tutorial Bundle) - JSON Path Finder Tool