How to Import CSV into MySQL: 4 Easy Methods (2025 Step-by-Step)

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

Before You Begin: Prepare Your CSV File

Proper preparation prevents import problems. Follow these three steps before any import:

  1. Save as UTF-8 – Open your CSV in Notepad++ or VS Code and save with UTF-8 encoding (without BOM). This prevents character corruption.
  2. 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.
  3. 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:

  1. Log into phpMyAdmin (usually via cPanel or your hosting control panel).
  2. Select your target database from the left sidebar.
  3. Click on the Import tab at the top.
  4. Under File to import, click Choose File and select your CSV file.
  5. In the Format dropdown, select CSV (not SQL).
  6. 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)
  7. Check “Use the first row as column names” if applicable.
  8. 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:

  1. Open MySQL Workbench and connect to your database instance.
  2. In the Navigator panel (left side), right-click on your target schema/database.
  3. Select Table Data Import Wizard.
  4. Browse and select your CSV file, then click Next.
  5. Choose an existing table or let the wizard create a new one. If creating new, you can adjust column names and data types.
  6. Map the CSV columns to table columns. The wizard auto-detects data types, but you can override them.
  7. Click Next – the wizard shows a preview of the import.
  8. Click Next again to start the import. Progress bar shows each row being inserted.
  9. 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-priv directory (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 ROWS to 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?

MethodBest ForFile SizeEase of UseAutomation?
phpMyAdminBeginners, shared hosting< 50MBVery easy (GUI)No
MySQL WorkbenchVisual mapping, medium files< 200MBEasy (wizard)No
LOAD DATA INFILELarge files, speedAny sizeModerate (SQL)Yes
mysqlimportRecurring automationAny sizeModerate (command line)Yes

Common CSV Import Errors & How to Fix Them

Error MessageLikely CauseSolution
Error 1290: The MySQL server is running with --secure-file-privFile not in allowed directoryMove 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 valueEncoding 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 truncatedColumn count mismatch or delimiter issueCheck CSV delimiter (use a text editor to view raw). Ensure number of columns matches table.
Error 1062: Duplicate entry for primary keyCSV contains duplicate primary key valuesUse IGNORE keyword: LOAD DATA INFILE ... IGNORE INTO TABLE or clean duplicates beforehand.
phpMyAdmin import hangs or times outFile too large or server limitsSplit 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 REPLACE or IGNORE: Add REPLACE keyword to overwrite duplicate keys, or IGNORE to skip them.

Internal Links (from your site):

External High-Authority References:

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!

1 thought on “How to Import CSV into MySQL: 4 Easy Methods (2025 Step-by-Step)”

  1. Pingback: CSV to SQL Masterclass: Complete Guide for 2025 (9-in-1 Tutorial Bundle) - JSON Path Finder Tool

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top