How to export and import a .sql file from command line with options?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
For MySQL and compatible servers, command-line export and import usually means mysqldump to create a logical backup and mysql to restore it. The useful part is not the basic syntax but choosing the right options for schema-only dumps, data-only dumps, consistent backups, and safer restores.
Export With mysqldump
The basic export pattern is:
This prompts for the password and writes the dump to backup.sql.
A few very common variations are:
These options mean:
- '
--no-dataexports only tables, indexes, and other schema definitions' - '
--no-create-infoexports only row data' - '
--routinesincludes stored procedures and functions' - '
--triggersincludes triggers'
Use The Right Options For Consistent Backups
For InnoDB-heavy databases, --single-transaction is a common and useful option:
It creates a consistent snapshot without locking every table the way older dump patterns often did.
If you are dumping multiple databases:
Or everything:
Those commands are more powerful, so use them carefully and make sure the target restore environment is appropriate.
Import With The mysql Client
To restore a dump into an existing database:
If the database does not exist yet, create it first:
This redirection pattern is easy to automate in deployment or disaster-recovery workflows.
Export And Import Specific Tables
You do not always need a full database dump. Specific tables can be targeted:
That file can later be restored the same way:
This is useful for moving a subset of the schema, reproducing a bug, or restoring a small part of an environment.
Security And Operational Details
Use -p by itself so the client prompts for the password. Avoid putting the password directly on the command line because it may end up in shell history or process listings.
If you run this often, store non-sensitive defaults in an option file such as ~/.my.cnf, or rely on a secure secret-management workflow instead of hard-coding credentials in scripts.
It is also worth compressing large dump files:
That is often a practical improvement for large logical backups.
Common Pitfalls
One common mistake is forgetting that mysqldump creates a logical SQL export, not a physical snapshot. Very large databases may need a different backup strategy.
Another issue is restoring into the wrong database name because the command line was copied from another environment.
A third problem is assuming schema.sql includes data even when --no-data was used, or assuming a dump contains routines and triggers when those options were omitted.
Finally, users often paste passwords directly after -p on shared systems. It works, but it is weaker operational hygiene than using the interactive prompt.
Summary
- Use
mysqldumpto export logical SQL backups andmysqlto import them. - '
--no-datagives schema only, and--no-create-infogives data only.' - '
--single-transactionis a strong default for consistent InnoDB dumps.' - Restore with
mysql database_name < file.sqlafter creating the target database if needed. - Avoid exposing passwords on the command line and compress large dumps when practical.

