Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Taking regular backups is only half the battle; knowing how to restore them under pressure during an outage or when setting up a fresh development environment is what separates junior coders from professional database engineers. In this tutorial, you will learn the standard techniques for importing and restoring SQL scripts.
The fastest and most common way to import a .sql dump file is using shell input redirection (<):
If your backup is compressed (.gz), you can decompress and pipe it directly into MySQL without extracting the full file to disk first:
SOURCEIf you are already logged into the interactive mysql> prompt, use the built-in SOURCE command (or shorthand \.):
/) or escaped backslashes (\\) in file paths inside the SOURCE command! Writing C:\Users can trigger escape character interpretation errors.For visual database management:
.sql file.When importing huge database dumps (gigabytes of data), standard execution can be slow because every inserted row checks foreign keys and recalculates secondary indexes. You can speed up imports by wrapping the session:
.sql file does not contain an explicit USE db_name; statement at the top, running mysql -u root -p < file.sql will abort with Error 1046: No database selected. Always specify the database name on the command line!max_allowed_packet on Large Inserts: If the backup contains giant batch insert statements, ensure the target server's max_allowed_packet is set large enough to receive the payload.backup.sql into a database named school_db?mysql> prompt executes an external SQL script file?C:/data/backup.sql) inside the SOURCE command?FOREIGN_KEY_CHECKS during large database restorations?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Database Backups with mysqldump | Exporting & Importing CSV Data |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.