How can I import a database with MySQL from terminal?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Importing a database using MySQL from the terminal is a fundamental task for database administrators and developers. It allows for efficient data transfer and backup recovery. This guide will walk you through the process with technical explanations and examples, ensuring a solid understanding of the procedure.
Prerequisites
Before proceeding with importing a database, ensure you have:
- MySQL Installed: Verify that MySQL is installed on your system by running
mysql --versionin your terminal. - Database File: A
.sqlfile containing the database backup you intend to import. - Access Credentials: Ensure you have the necessary credentials (username and password) to access the MySQL server.
- Database Ready: The target database should already exist, or you should have permissions to create one.
Steps to Import a Database
Step 1: Open the Terminal
Access your terminal or command-line interface that supports MySQL commands. This environment interacts directly with your MySQL server.
Step 2: Log into MySQL
Authenticate and log into the MySQL server using:
Replace [username] with your MySQL username. After pressing enter, you'll be prompted to enter your password. Upon successful login, you'll enter the MySQL command prompt.
Step 3: Create or Select the Target Database
If the database already exists, you can skip this step. To create a new database, run:
Replace [database_name] with the name you want for the new database. To use this database, execute:
Step 4: Import the SQL File
Exit the MySQL prompt by typing exit or pressing Ctrl + D. To import the .sql file, use the following command in the terminal:
- Replace
[username]with your MySQL username. - Replace
[database_name]with the name of the database you're importing into. - Replace
/path/to/your/databasefile.sqlwith the full path to your.sqlfile.
Example
To illustrate, if your username is admin, your database name is sampleDB, and your file path is /home/user/backup.sql, the command would look like:
When prompted, enter your password. The import process then begins, displaying progress in your terminal.
Common Issues and Troubleshooting
- Access Denied Error: Ensure you have typed your username and password correctly, and verify that your user has the correct privileges.
- Database Doesn't Exist: If you get this error, ensure you have created the database or have permissions to do so before the import.
- File Not Found: Double-check the path to your
.sqlfile for correctness.
Advanced Options
Custom Port Number
If your MySQL server is running on a custom port, specify it using the -P flag:
Specifying a Host
When needing to connect to a remote host, include the -h flag with the host address:
Summary Table
| Action | Command/Instructions |
| Verify MySQL Installation | mysql --version |
| Log into MySQL | mysql -u [username] -p |
| Create Database | CREATE DATABASE [database_name]; |
| Use existing Database | USE [database_name]; |
| Import .sql File | mysql -u [username] -p [database_name] < /path/to/your/file.sql |
| Custom Port Specification | mysql -u [username] -p -P [port] [dbname] < /path/to/file.sql |
| Specify Remote Host | mysql -u [username] -p -h [host] [dbname] < /path/to/file.sql |
Additional Tips
- Backup Regularly: Regular backups are key to disaster recovery. Export databases using the
mysqldumpcommand. - Permissions Management: Regularly review and update MySQL user permissions to secure access.
- Data Validation: After import, verify data integrity by performing consistency checks and comparisons with source data.
By following the steps outlined in this guide, you can reliably import a MySQL database from the terminal, ensuring efficient database management and recovery procedures.
Related reading
- How can I import a jsonb column from a csv file using the COPY command?
- How can I import a large 14 GB MySQL dump file into a new MySQL database?
- How can I import an SQL file using the command line in MySQL?
- How can I import bulk data from a CSV file into DynamoDB?
- How can I import data into mysql database via mysql workbench?
- How can I initialize a MySQL database with schema in a Docker container?
- How can I initialize a MySQL database with schema in a Docker container?
- How can I Insert many rows into a MySQL table and return the new IDs?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.