You may frequently encounter the need to move databases – whether it is migrating a site, restoring a backup, or setting up a local development environment. While tools like phpMyAdmin or cPanel are great for smaller databases, they fail when trying to import databases that would have grown to a massive size over time. This is due to process timeouts, upload size limits, or memory limits — more so when you are moving to a shared server where you do not have permission to alter these limits.
That is where the command line comes in. Using the command line is the most efficient way to handle large database imports, as it avoids the limitations of GUI tools like phpMyAdmin, which can struggle with memory or timeout issues. This guide walks you through the process on both Windows and Linux systems, with practical tips for optimizing performance and troubleshooting common issues.
Before starting, ensure you have the following:
MySQL or MariaDB installed: Verify that the database server is running on your system.
SQL dump file: This is the .sql
file containing the database exported from source server and that you want to import. Make sure it is copied / uploaded to your destination server (using ftp) and you know its location.
Command-line access: On Windows, use Command Prompt or PowerShell; on Linux, use a terminal.
Database credentials: You will need the username and password for a database user on the destination MySQL/MariaDB server, with sufficient privileges (e.g., root
) to create and import into databases.
Sufficient disk space: Ensure there is enough space on destination server for the database and temporary files.
mysql
client: This is the command-line utility used to interact with MySQL/MariaDB servers.
Redirection (<
): This operator is used to feed the content of a file as input to a command.
Before importing, check the integrity of your .sql
file to avoid errors during the process:
Check file size: Ensure the file isn’t corrupted or truncated. On Linux, use ls -lh
to view the file size; on Windows, check the file properties.
Inspect the file: Open the file in a text editor (like VS Code or Notepad++) or use head
(Linux) or type | more
(Windows) to confirm it contains valid SQL statements.
You need an empty database to import the dump file into. You can create this database from your cPanel or plesk panel, and assign it to the database username with full privileges.
Alternatively, you can do this from the command line. Follow these steps:
Log in to MySQL/MariaDB:
mysql -u user_name -p
Replace user_name
with your MySQL/MariaDB username (e.g., root
). Enter your password when prompted.
Create a new database:
CREATE DATABASE IF NOT EXISTS my_database;
Replace my_database
with your desired database name.
Exit the MySQL prompt:
EXIT;
The import process differs slightly between Windows and Linux due to command-line syntax and file path conventions.
Linux environments are often the go-to for web development, and the command line is an integral part of that workflow.
Navigate to the directory containing the SQL file:
While not strictly necessary, it is good practice to cd
into the directory where your .sql
file is located. This simplifies the command.
cd /path/to/your/sqlfile
Import the SQL file:
Now, for the main event – importing the .sql
file:
mysql -u user_name -p my_database < database_dump.sql
Replace user_name
with your MySQL/MariaDB username, my_database
with the target database name, and database_dump.sql
with the name of your copied/uploaded SQL file. You will be prompted for the password.
Let us break down this command:
mysql
: Invokes the MySQL command-line client.
-u user_name
: Specifies the username to connect to the database.
-p
: Prompts you for the password. Do not put your password directly after -p
in production scripts, as it can be a security risk and expose your password in your shell history.
my_database
: The name of the database you want to import into. This database must already exist.
< database_dump.sql
: This is the crucial part. The <
symbol redirects the content of database_dump.sql
as input to the mysql
command.
Example:
If your username is root
, your database is named my_wordpress_site
, and your SQL file is backup_20250703.sql
, the command would be:
mysql -u root -p my_wordpress_site < backup_20250703.sql
After pressing Enter, you will be prompted for your password. The import process will then begin. There won't be much output unless there are errors.
Optional: Optimize for large files:
If the file is very large (e.g., >1GB), use the --force
option to continue past minor errors:
mysql -u user_name -p --force my_database < database_dump.sql
Importing on Windows is very similar to Linux, but you will be using Command Prompt or PowerShell.
Open Command Prompt or PowerShell:
Press Win + R
, type cmd
or powershell
, and hit Enter.
Navigate to the MySQL/MariaDB bin directory:
The mysql
client is located in the bin
directory of your MySQL or MariaDB installation. You will need to navigate there or add it to your system's PATH environment variable. For simplicity, we will navigate there.
Common Paths:
MySQL: C:\Program Files\MySQL\MySQL Server X.Y\bin
(where X.Y is your version number)
MariaDB: C:\Program Files\MariaDB X.Y\bin
(where X.Y is your version number)
Import the SQL file:
Now, import your .sql
file. You will need to provide the full path to your SQL file if you are not in its directory.
mysql -u user_name -p my_database < "C:\path\to\your\sql\file\database_dump.sql"
Use backslashes (\
) for file paths in Windows. Replace user_name
, my_database
, and the file path as needed. Enter the password when prompted. Once entered, the import will begin.
Example:
If your username is root
, your database is named my_local_dev
, and your SQL file is D:\backups\my_project_data.sql
, the command would be:
mysql -u root -p my_local_dev < "D:\backups\my_project_data.sql"
Optional: Optimize for large files:
Similar to Linux, use the --force
option for large files:
mysql -u user_name -p --force my_database < C:\path\to\your\database_dump.sql
Large imports can take time, and there is no progress bar by default. To monitor progress:
Check database size: Periodically query the database size to see it grow:
SELECT table_schema AS "Database",
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)"
FROM information_schema.tables
WHERE table_schema = 'my_database';
Use pv
(Linux): If available, use the pv
(pipe viewer) tool to show progress:
pv database_dump.sql | mysql -u user_name -p my_database
Split large files: For extremely large files, consider splitting the SQL file into smaller chunks using a tool like split
(Linux) or a text editor, then import each chunk sequentially.
To speed up the import process, especially for large databases:
Disable foreign key checks (temporary):
Add this to the top of your .sql
file or run it before importing:
SET FOREIGN_KEY_CHECKS = 0;
Re-enable after import:
SET FOREIGN_KEY_CHECKS = 1;
Increase buffer sizes: Edit your MySQL/MariaDB configuration file (my.cnf
on Linux, my.ini
on Windows) to increase innodb_buffer_pool_size
and innodb_log_file_size
for better performance. Restart the server after changes.
Use source
for smaller files: If the file is manageable, you can log into the MySQL prompt and run:
USE my_database;
SOURCE /path/to/database_dump.sql;
Note: This may be slower for very large files.
Large Files and Time: Importing very large databases can take a significant amount of time. Be patient. There might be no visual feedback until the process is complete or an error occurs.
Error Messages: If an error occurs, the command line will usually display an error message. Common issues include:
Incorrect path to SQL file: Double-check the file path.
Incorrect database name: Ensure the database exists and the name is spelled correctly.
"Unknown database" error: Confirm the database was created before importing.
"Access denied" error: Verify your username and password. Ensure the user has sufficient privileges for the database to create tables, insert data, etc.
Memory or timeout issues: For extremely large inserts, you might encounter an error related to max_allowed_packet
. You may need to temporarily increase this value in your my.cnf
(Linux) or my.ini
(Windows) file and restart your MySQL/MariaDB server. Or, you may consider splitting the file.
Locate your configuration file (often /etc/mysql/my.cnf
or /etc/my.cnf
on Linux, and C:\ProgramData\MySQL\MySQL Server X.Y\my.ini
or similar on Windows).
Add or modify the max_allowed_packet
line under the [mysqld]
section:
[mysqld]
max_allowed_packet = 128M # Or a higher value like 256M
Restart your MySQL/MariaDB service.
Output Redirection for Errors: To capture potential errors into a file for later review, you can redirect standard error (2>
) to a file:
mysql -u user_name -p my_database < database_dump.sql 2> import_errors.log
Character set issues: Ensure the .sql
file’s character set matches the database’s (e.g., utf8mb4
). Specify the character set if needed:
mysql -u user_name -p --default-character-set=utf8mb4 my_database < database_dump.sql
Compressed Files (.gz, .zip): If your database dump is compressed (e.g., .sql.gz
), you will need to decompress it first or pipe it directly.
Linux (gunzip):
gunzip < database_dump.sql.gz | mysql -u user_name -p my_database
For .zip
files, you would typically unzip them manually first.
After the import completes, verify the data:
Check table structure:
USE my_database;
SHOW TABLES;
Query sample data:
Run a simple SELECT
query to ensure data is present:
SELECT * FROM some_table LIMIT 10;
Check for errors: Review the terminal output or MySQL error logs for any issues during import.
Importing large MySQL or MariaDB databases via the command line is a powerful method, offering speed and reliability over GUI tools. By following the steps outlined above, you can efficiently handle large .sql
files on both Windows and Linux systems. Optimize performance with configuration tweaks, monitor progress, and always verify the import to ensure data integrity. With these techniques, you will be well-equipped to manage even the most massive databases with confidence.
How to move your Email accounts from one hosting provider to another without losing any mails?
How to resolve the issue of receiving same email message multiple times when using Outlook?
Self Referential Data Structure in C - create a singly linked list
Mosquito Demystified - interesting facts about mosquitoes
Elements of the C Language - Identifiers, Keywords, Data types and Data objects
How to pass Structure as a parameter to a function in C?
Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of premier engineering colleges & management institutes, and IT professionals.
Rajeev has founded Computer Solutions & Web Services Worldwide. He has hands-on experience of building variety of websites and business applications, that include - SaaS based erp & e-commerce systems, and cloud deployed operations management software for health-care, manufacturing and other industries.