MySQL Import Database Command Line in Linux Step-by-Step Guide

Avatar
T

Tariq Mansoor

Database & Performance Engineer•
7772Views
5mRead
0Reactions

The MySQL import database command-line method comes down to one MySQL command that reads a .sql dump file and loads it into a database. It is the standard way to restore a backup, move a site to a new server, or copy production data into a local environment. This guide covers the whole process: creating the target database, preparing a dump file, running the import, verifying the result, handling large files, and fixing common errors.

The commands work on Debian, Ubuntu, Fedora, CentOS, and similar distributions. They also work with MariaDB, where mysql is usually an alias for mariadb.

Before You Start, make sure you have:

  • Terminal access, either directly or over SSH.

  • The MySQL client installed. Check with mysql --version.

  • A MySQL user with permission to create tables in the target database.

  • Enough free disk space, which you can check with df -h. A database often takes more space than its dump file.

MySQL Import Database Command Line: Step by Step

1. Accessing the Command Line: Before diving into the import process, ensure you have access to the command line interface in your Linux distribution. You can use terminal emulators like GNOME Terminal, Konsole, or any other shell terminal available in your Linux system.

2. Preparing Your MySQL Database: Before importing a MySQL database, it's crucial to have a database created where you intend to import your data. You can create a new database using the following command:

mysql -u your_username -p
CREATE DATABASE your_database_name;

3. Preparing the Database Dump File: To import a MySQL database, you'll typically use a database dump file, which contains the SQL statements needed to recreate the database's structure and populate it with data. If you already have a dump file ready, proceed to the next step. Otherwise, create one using the following command:

mysqldump -u your_username -p your_database_name > database_dump.sql

4. Importing the MySQL Database: Once you have your database dump file prepared, you can import it into MySQL using the mysql command along with the<operator to specify the file to be imported. Execute the following command:

mysql -u your_username -p your_database_name < database_dump.sql

Replace your_username with your MySQL username, your_database_name with the name of the database you want to import into, and database_dump.sql with the name of your dump file.

5. Verifying the Import: After executing the import command, it's essential to verify whether the import was successful. You can do this by accessing the MySQL client and checking if the tables and data from your database dump are present:

mysql -u your_username -p your_database_name
SHOW TABLES;

6. Handling Large Database Imports: Importing large MySQL databases can sometimes be resource-intensive and time-consuming. In such cases, consider using tools like pv (pipe viewer) to monitor the progress of the import process:

pv database_dump.sql | mysql -u your_username -p your_database_name

7. Optimizing Database Import Performance: To enhance the import performance, you can disable foreign key checks and enable bulk insert mode before importing the database dump:

mysql -u your_username -p your_database_name --force --disable-keys < database_dump.sql

Common Mistakes to Avoid

  • Importing into the wrong database. Double-check the name, especially on servers with several similar ones.

  • Importing over live data without a backup. Because of DROP TABLE, existing tables will be overwritten.

  • Mixing up>and<. >writes to a file and < reads from one. Swapping them can overwrite your dump file.

  • Typing the password on the command line. It stays in your shell history and is visible to other users on the machine.

  • Ignoring character sets. If accented letters or emoji look garbled afterward, the dump and the database may use different character sets. Using utf8mb4 for both avoids most problems.

  • Conclusion

    The MySQL import database command-line process is short: create the target database, get or make a dump file, run mysql ... < dump.sql, and confirm the result by comparing tables and row counts. For big files, add pv or run the import in tmux. Keep passwords off the command line, and back up any database you are about to overwrite.

    Frequently Asked Questions

    How do I create a MySQL dump file?

    Run mysqldump -u your_username -p your_database_name > database_dump.sql. For InnoDB databases, add --single-transaction to avoid locking tables during the export.

    Does the database have to exist before I import?

    Yes, unless the dump was created with mysqldump --databases, which adds CREATE DATABASE and USE statements to the file. With a regular dump, create the database first.

    How can I import a large MySQL database faster?

    Use a regular mysqldump file, which already delays index building and uses multi-row inserts. For trusted data, you can disable foreign key and unique checks for the session. A server with enough memory and fast disk helps too. Use pv if you only want to watch progress.

    Can I import into a remote MySQL server?

    Yes. Add-h remote_host (and-P port if it isn't 3306). The remote server must allow your user to connect from your machine.

    How do I know the import worked?

    Run SHOW TABLES; compare row counts with SELECT COUNT(*) against the original, and check a few important records.

    Connect with KoderSolution 🚀

    Master modern software engineering, scalable architectures, and advanced backend systems.

    What's your setup?What's your production setup for this scenario? Have you encountered similar architectural bottlenecks or edge-case gotchas?
    Tariq Mansoor
    T
    Written by•Database & Performance Engineer

    Tariq Mansoor

    Spends an unhealthy amount of time in EXPLAIN ANALYZE. Fixes slow N+1 queries, indexes table scans, and tunes PostgreSQL/MySQL for high-throughput apps.

    React to this article

    Recommended Articles

    View all articles