Guides

Step-by-step practical fixes for everyday technical problems.
Database Guides

How to Import SQL File into MySQL

Restore a backup or move data safely by importing a SQL file into the correct MySQL database.

Before you start

Confirm the exact database name before running the import.
Check whether the target database is empty or already contains tables.
If the existing data matters, take a backup before importing over it.

Import into an existing database

If the target database already exists, the standard import command is straightforward.

Command
mysql -u username -p database_name < backup.sql
What this does

It reads the SQL file and executes its statements inside the target database.

Create the database first if needed

If the database does not exist yet, create it before importing unless the SQL file already contains a database creation step.

SQL
CREATE DATABASE database_name;
Common mistake

Running the import before the database exists causes an avoidable failure.

Import after creating the database

Once the database exists, run the import command against that database.

Command
mysql -u username -p database_name < backup.sql
Practical flow

Create the database first, then import. This is the most predictable workflow when restoring to a fresh environment.

Import and save an error log

If the file is large or the environment is unfamiliar, capturing errors can make troubleshooting much easier.

Command
mysql -u username -p database_name < backup.sql 2> import_error.log
Why this helps

If the import fails, you have a file containing the error messages instead of relying only on terminal output.

Check whether the tables were restored

Do not stop at the import command. Verify that tables now exist in the target database.

SQL
SHOW TABLES;
Expected result

You should see the tables that were supposed to be restored by the SQL file.

Check whether the data is there too

If the tables exist but you want to confirm the data imported correctly, run a quick row count on an important table.

SQL
SELECT COUNT(*) AS cnt
FROM table_name;
Why this matters

An import can appear to work while still leaving you with missing or partial data if errors occurred partway through.

Common failure situations

Database does not exist

Create the target database first, or confirm whether the SQL file is supposed to create it for you.

Table already exists

The import may fail if the SQL file tries to create tables that are already present in the target database.

Permission errors

The MySQL account may not have the privileges needed to create tables, insert rows, or run the statements inside the file.

Charset or collation issues

If the source and destination environments differ, text data or table definitions may create import warnings or failures.

Common mistakes

Importing into the wrong database

Double-check the target database name before pressing Enter.

Skipping backup before import

If the target database already contains important data, back it up first.

Assuming success without verification

Check tables and row counts instead of assuming the import finished perfectly.

Ignoring import errors

If there are errors, read them immediately before doing more database work on top of a broken restore.

About this guide

This guide shows how to import a SQL file into MySQL safely in real-world situations such as restoring a backup, moving a database to another server, or applying an exported table dump. It focuses on the practical commands, common failure points, and quick checks you should do before and after import.

How to follow this guide

  1. Confirm the target database name before running the import.
  2. Create the database first if it does not already exist.
  3. Run the mysql import command with the correct SQL file.
  4. Watch for permission, charset, or duplicate object errors during import.
  5. Verify that tables and data were actually restored after the import finishes.

Why use this method?

Importing a SQL file sounds simple, but it is easy to import into the wrong database, overwrite existing objects, or assume success without checking the result. A clear import workflow prevents data mistakes and saves time when restoring or migrating databases.

Frequently Asked Questions

What command is used to import a SQL file into MySQL?

The most common method is the mysql command with input redirection, pointing the SQL file into the target database.

Do I need to create the database before importing?

Usually yes if the database does not already exist and the SQL file does not create it for you.

What if the SQL file contains tables that already exist?

The import may fail with table already exists errors unless the file is written to drop or replace objects first.

How do I know the import worked correctly?

Check the tables, row counts, and any error output instead of assuming success just because the command finished.