Skip to content

MySQL - Database Import

MySQL - Importing a Database from a SQL File

Section titled “MySQL - Importing a Database from a SQL File”

Importing a database involves restoring its structure and data from a SQL script file (often called a ‘dump’ file). This process is essential for migrating databases, restoring from backups, or setting up development environments. This tutorial covers the primary methods for importing data into MySQL.

### Prerequisites
* **A SQL Dump File**: You need a `.sql` file that contains the `CREATE TABLE` and `INSERT INTO` statements (or other valid SQL commands) to recreate the database. These are typically generated by tools like `mysqldump`.
* **A Target Database**: You need an existing, often empty, database to import the data into.
* **Permissions**: The MySQL user performing the import needs sufficient privileges (`CREATE`, `INSERT`, `DROP`, etc.) on the target database.
Section titled “Method 1: Using the mysql Command-Line Tool (Recommended)”

The most common and efficient way to import a SQL file is by using the mysql command-line client to redirect the file’s content as input. This method is fast and works well for files of any size.

mysql -u [username] -p [database_name] < [path_to_your_sql_file.sql]
  • -u [username]: Your MySQL username.
  • -p: Prompts you to enter the password securely. Avoid typing your password directly on the command line.
  • [database_name]: The name of the database you want to import the data into. This database must exist first.
  • <: This is the standard input redirection operator in shell environments. It tells the shell to send the content of the file to the mysql client’s standard input.

Let’s assume we have a backup file named tut_backup.sql that we want to import into a new database called testdb.

  1. Log in to MySQL and Create the Target Database
$ mysql -u root -p
Enter password: ********
mysql> CREATE DATABASE testdb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

A Query OK message confirms the database creation. Now, exit the MySQL client.

mysql> exit
Bye
  1. Run the Import Command from Your System’s Terminal
$ mysql -u root -p testdb < tut_backup.sql
Enter password: ********

If the command executes successfully, there will be no output. The absence of error messages indicates success. If there are any syntax errors or permission issues in the SQL file, MySQL will print them to the terminal.

To verify the import, log back into the testdb database and inspect its contents.

$ mysql -u root -p testdb
mysql> SHOW TABLES;

This should list the tables that were created from the tut_backup.sql file. For instance, if the backup contained a customers table:

Tables_in_testdb
customers

You can further verify by querying the data:

mysql> SELECT * FROM customers LIMIT 5;

Method 2: Using the SOURCE Command in the MySQL Client

Section titled “Method 2: Using the SOURCE Command in the MySQL Client”

An alternative method is to use the SOURCE command from within an interactive MySQL session. This approach is functionally similar but can be more convenient if you are already working inside the MySQL client.

SOURCE /path/to/your/sql_file.sql;
  1. Log in to MySQL and select the target database.
$ mysql -u root -p
Enter password: ********
mysql> USE testdb;
  1. Run the SOURCE command. Note that you must provide the full, absolute path to the file, or a path relative to where you started the mysql client.
mysql> SOURCE /home/user/backups/tut_backup.sql;

MySQL will execute the commands from the file and print Query OK for each statement, which can be verbose for large files.

Important Considerations and Troubleshooting

Section titled “Important Considerations and Troubleshooting”
* **Character Sets**: Mismatches between the character set of the SQL file and the target database can cause data corruption (mojibake). It's a best practice to create the target database with the same character set as the source (usually `utf8mb4`).
* **Large Files**: For very large dump files, the command-line redirection method (`<`) is generally more memory-efficient. You may also encounter the `max_allowed_packet` limit. If you see an error like 'MySQL server has gone away', you may need to increase this value in your MySQL configuration file (`my.cnf` or `my.ini`).
* **Security**: Never import SQL files from untrusted sources. They are plain text files that can contain malicious commands like `DROP DATABASE` or other destructive operations.
* **Common Errors**:
* `ERROR 1044 (42000): Access denied for user...`: The user lacks the necessary privileges on the target database.
* `ERROR 1046 (3D000): No database selected`: You forgot to specify the `database_name` in the command line or use `USE database_name;` before `SOURCE`.
* `ERROR 1050 (42S01): Table '...' already exists`: You are importing into a database that already contains tables with the same names. Ensure the target database is empty or the SQL file contains `DROP TABLE IF EXISTS` statements.