MySQL Backup & Restore Mastery: How to Import Large .SQL Files Without Breaking a Sweat
What is mysqldump?
mysqldump is a command-line tool used to create backups of MySQL databases. It generates a .sql file containing all the SQL commands needed to recreate the database structure and data.
PART 1: RESTORING (IMPORTING) from .SQL FILE
If you already have a .sql file, here's how to restore it:
Step 1: Access the MySQL Command Line
bash
mysql -u root -p
(Replace root with your username and enter your password when prompted)
Step 2: Create a Database (Optional)
If you need to create a new database before importing:
sql
CREATE DATABASE aash_db;
Step 3: Exit MySQL
sql
exit;
Step 4: Import Your .sql File
bash
mysql -u root -p aash_db < posts\ \(1\).sql
Important: If your filename has spaces or special characters, use quotes or escape them:
bash
mysql -u root -p aash_db < "posts (1).sql"
# OR
mysql -u root -p aash_db < posts\ \(1\).sql
Step 5: Verify the Import
bash
mysql -u root -p -e "USE aash_db; SHOW TABLES;"
PART 2: CREATING BACKUPS (EXPORTING) WITH MYSQLDUMP
Basic Export:
bash
mysqldump -u root -p database_name > backup.sql
Export Specific Tables:
bash
mysqldump -u root -p database_name table1 table2 > backup.sql
Export Only Structure (No Data):
bash
mysqldump -u root -p --no-data database_name > structure.sql
Export Only Data (No Structure):
bash
mysqldump -u root -p --no-create-info database_name > data_only.sql
PART 3: COMPLETE EXAMPLE WITH YOUR FILE
Here's a practical example for your specific case:
bash
# Step 1: Create the database (if needed)
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS aash_db;"
# Step 2: Import your SQL file
mysql -u root -p aash_db < "posts (1).sql"
# Step 3: Verify the import
mysql -u root -p -e "USE aash_db; SELECT COUNT(*) FROM posts;"
PART 4: ADDITIONAL USEFUL MYSQLDUMP OPTIONS
For Better Performance (Large Databases):
bash
mysqldump -u root -p --quick --compress --skip-extended-insert database_name > backup.sql
With All Options:
bash
mysqldump -u root -p \
--add-drop-table \
--add-locks \
--create-options \
--disable-keys \
--extended-insert \
--single-transaction \
--quick \
database_name > full_backup.sql
Export to a Specific Location:
bash
mysqldump -u root -p database_name > /path/to/backups/backup_$(date +%Y%m%d).sql
PART 5: COMMON ISSUES AND SOLUTIONS
1. "Access denied" error:
bash
mysqldump -u root -p --password=your_password database_name > backup.sql
2. "File not found" with spaces in filename:
bash
mysqldump -u root -p database_name > "backup file with spaces.sql"
3. Importing a large SQL file:
bash
mysql -u root -p --max_allowed_packet=1G database_name < large_file.sql
4. If you get character set errors:
bash
mysqldump -u root -p --default-character-set=utf8 database_name > backup.sql
QUICK REFERENCE TABLE
|
Operation |
Command |
|
Export database |
mysqldump -u user -p db > file.sql |
|
Export structure only |
mysqldump -u user -p --no-data db > file.sql |
|
Export data only |
mysqldump -u user -p --no-create-info db > file.sql |
|
Import database |
mysql -u user -p db < file.sql |
|
Export specific tables |
mysqldump -u user -p db table1 table2 > file.sql |
Step-by-Step Summary:
1. Open Terminal/Command Prompt
2. Navigate to where your .sql file is located
3. Create the database:
bash
mysql -u root -p -e "CREATE DATABASE aash_db;"
4. Import the file:
bash
mysql -u root -p aash_db < "posts (1).sql"
5. Check the result:
bash
mysql -u root -p -e "USE aash_db; SHOW TABLES;"
That's it! Your posts (1).sql file is now fully imported into your aash_db database.
path is like this: E:\dumpTempFolder\db_backup.sql now tell me what to do...
Perfect! Now I'll give you the exact commands for your specific file path.
STEP-BY-STEP GUIDE FOR YOUR FILE
Step 1: Open Command Prompt or PowerShell
· Press Windows + R, type cmd and press Enter
· OR search for "Command Prompt" in Start Menu
Step 2: Navigate to Your Dump Directory
bash
cd E:\dumpTempFolder
Step 3: Check if File Exists (Optional)
bash
dir db_backup.sql
(This should show your file)
Step 4: Login to MySQL (To Create Database if Needed)
bash
mysql -u root -p
(Enter your MySQL password when prompted)
Step 5: Create Database (if not exists)
sql
CREATE DATABASE IF NOT EXISTS aash_db;
Step 6: Exit MySQL
sql
exit;
Step 7: Import Your SQL File
bash
mysql -u root -p aash_db < db_backup.sql
(Enter your MySQL password when prompted)
COMPLETE COMMANDS (COPY AND PASTE)
Here's everything in one go (run one by one):
bash
# 1. Go to your dump folder
cd E:\dumpTempFolder
# 2. Login to MySQL and create database
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS aash_db;"
# 3. Import your SQL file
mysql -u root -p aash_db < db_backup.sql
IF YOU GET ERRORS
Option A: If MySQL is not in PATH
bash
# For Windows, use full path to mysql
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql" -u root -p aash_db < db_backup.sql
Option B: If password has special characters
bash
mysql -u root -pYourPassword aash_db < db_backup.sql
(No space between -p and password)
Option C: If file is too large (Memory issues)
bash
mysql -u root -p --max_allowed_packet=1G aash_db < db_backup.sql
Option D: Use MySQL Workbench (GUI Method)
1. Open MySQL Workbench
2. Click on your connection
3. Go to Server → Data Import
4. Select Import from Self-Contained File
5. Browse to E:\dumpTempFolder\db_backup.sql
6. Select your database aash_db
7. Click Start Import
VERIFY SUCCESSFUL IMPORT
After importing, check if data was imported:
bash
# Method 1: Quick check
mysql -u root -p -e "USE aash_db; SELECT COUNT(*) FROM posts;"
# Method 2: Full check
mysql -u root -p -e "USE aash_db; SHOW TABLES;"
TROUBLESHOOTING
|
Problem |
Solution |
|
ERROR 1049 (42000): Unknown database |
Create database first: CREATE DATABASE aash_db; |
|
ERROR 1045 (28000): Access denied |
Check your password or username |
|
ERROR 1064 in import |
Your SQL file might have syntax errors; check the last lines |
|
File not found |
Make sure you're in E:\dumpTempFolder\ directory |
|
Import very slow |
Use --compress or import in MySQL Workbench |
QUICK ONE-LINER (if you know your password)
bash
cd E:\dumpTempFolder && mysql -u root -pYourPassword aash_db < db_backup.sql
Replace YourPassword with your actual password (no space after -p)
MY RECOMMENDATION
Since you're on Windows and have a specific path, run these commands in order:
bash
# Step 1
cd E:\dumpTempFolder
# Step 2 (creates database if needed)
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS aash_db;"
# Step 3 (imports your file)
mysql -u root -p aash_db < db_backup.sql
# Step 4 (verify - optional)
mysql -u root -p -e "USE aash_db; SHOW TABLES;"
Note: You'll be prompted for your MySQL password after each -p command.
Still stuck? Let me know:
1. What error message you see
2. What's your MySQL username (usually root)
3. If you have MySQL Workbench installed
E:\dumpTempFolder>mysql -u root -p root dmo_aashd_old < db_backup.sql 'mysql' is not recognized as an internal or external command, operable program or batch file.
Ah! MySQL is not in your system's PATH. Let's fix this step by step.
SOLUTION 1: Use Full Path to MySQL (Quick Fix)
First, find where MySQL is installed:
Step 1: Find MySQL Installation Location
Try these common locations:
bash
# Check if MySQL is installed in Program Files
dir "C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe"
# OR
dir "C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe"
# OR
dir "C:\Program Files (x86)\MySQL\MySQL Server 5.7\bin\mysql.exe"
# OR
dir "C:\xampp\mysql\bin\mysql.exe"
Step 2: Use Full Path to Import
Once you find MySQL location, use the full path. For example:
bash
# If MySQL is in Program Files (most common)
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql" -u root -p dmo_aashd_old < db_backup.sql
# OR if you have XAMPP
"C:\xampp\mysql\bin\mysql" -u root -p dmo_aashd_old < db_backup.sql
# OR if you have WAMP
"C:\wamp\bin\mysql\mysql5.7.31\bin\mysql" -u root -p dmo_aashd_old < db_backup.sql
SOLUTION 2: Add MySQL to PATH (Permanent Fix)
Method A: Quick PATH Addition (Current Session Only)
bash
# For MySQL 8.0 (Adjust version number)
set PATH=%PATH%;C:\Program Files\MySQL\MySQL Server 8.0\bin
# Then try again
mysql -u root -p dmo_aashd_old < db_backup.sql
Method B: Permanent PATH Addition (Recommended)
1. Open System Properties:
o Press Windows + R
o Type sysdm.cpl and press Enter
o Go to Advanced tab
o Click Environment Variables
2. Edit PATH:
o In "System variables" section, find Path and double-click
o Click New
o Add the MySQL bin folder path (e.g., C:\Program Files\MySQL\MySQL Server 8.0\bin)
o Click OK all the way
3. Restart Command Prompt and try again
SOLUTION 3: Use MySQL Workbench (Easiest GUI Method)
1. Download MySQL Workbench if not installed:
o https://dev.mysql.com/downloads/workbench/
2. Steps to import:
o Open MySQL Workbench
o Click your connection (usually "Local instance MySQL80")
o Go to Server → Data Import
o Select Import from Self-Contained File
o Browse to E:\dumpTempFolder\db_backup.sql
o Select Default Target Schema: Choose dmo_aashd_old or click New to create
o Click Start Import
SOLUTION 4: Find MySQL Location Automatically
Option A: Using Where Command
bash
where mysql
(If this shows a path, use that path)
Option B: Using PowerShell
bash
Get-Command mysql | Select-Object Source
Option C: Check Common Locations
bash
dir "C:\Program Files\MySQL\MySQL Server *\bin\mysql.exe" /s
dir "C:\xampp\mysql\bin\mysql.exe"
dir "C:\wamp\bin\mysql\mysql*\bin\mysql.exe"
CORRECTED COMMAND (Using Your Database Name)
Your command had a syntax issue too! The correct format is:
bash
# WRONG (your command)
mysql -u root -p root dmo_aashd_old < db_backup.sql
# ↑ No space between -p and password if you use password
# CORRECT (without password in command)
mysql -u root -p dmo_aashd_old < db_backup.sql
# Then enter password when prompted
# OR (with password - no space!)
mysql -u root -proot dmo_aashd_old < db_backup.sql
COMPLETE COMMAND WITH FULL PATH
Assuming MySQL is in C:\Program Files\MySQL\MySQL Server 8.0\bin\:
bash
cd E:\dumpTempFolder
# Create database first if needed
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql" -u root -p -e "CREATE DATABASE IF NOT EXISTS dmo_aashd_old;"
# Import the file
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql" -u root -p dmo_aashd_old < db_backup.sql
If Still Not Working - Let's Find Your MySQL
Run this command:
bash
dir /s "C:\mysql.exe"
dir /s "D:\mysql.exe"
dir /s "E:\mysql.exe"
OR, look for MySQL in these common folders:
· C:\Program Files\MySQL\
· C:\Program Files (x86)\MySQL\
· C:\xampp\mysql\
· C:\wamp\bin\mysql\
· C:\MYSQL\
· D:\xampp\mysql\
Tell Me What You See
Please check and tell me:
1. Do you have XAMPP installed? (Check for folder C:\xampp)
2. Do you have WAMP installed? (Check for folder C:\wamp)
3. Was MySQL installed standalone? (Check C:\Program Files\MySQL)
4. What error do you get when running this:
bash
dir "C:\Program Files\MySQL" /s | find "mysql.exe"
Once you find the MySQL location, we'll use that path to import your file!
bash
"C:\MAMP\bin\mysql\bin\mysql" -u root -proot -e "CREATE DATABASE IF NOT EXISTS dmo_aashd_old;"
bash
cd E:\dumpTempFolder
"C:\MAMP\bin\mysql\bin\mysql" -u root -proot dmo_aashd_old < db_backup.sql
Note: If your MySQL password is something other than "root", use that password instead.
--database optionbash
"C:\MAMP\bin\mysql\bin\mysql" -u root -proot --database=dmo_aashd_old < db_backup.sql
bash
# 1. Login to MySQL
"C:\MAMP\bin\mysql\bin\mysql" -u root -proot
# 2. Inside MySQL, run this:
USE dmo_aashd_old;
SOURCE E:\dumpTempFolder\db_backup.sql;
source command in one linebash
"C:\MAMP\bin\mysql\bin\mysql" -u root -proot -e "USE dmo_aashd_old; SOURCE E:\dumpTempFolder\db_backup.sql;"
First, check if your database exists:
bash
"C:\MAMP\bin\mysql\bin\mysql" -u root -proot -e "SHOW DATABASES;"
You should see dmo_aashd_old in the list.
If not, create it:
bash
"C:\MAMP\bin\mysql\bin\mysql" -u root -proot -e "CREATE DATABASE dmo_aashd_old;"
Then import:
bash
cd E:\dumpTempFolder
"C:\MAMP\bin\mysql\bin\mysql" -u root -proot dmo_aashd_old < db_backup.sql
bash
# Try without password
"C:\MAMP\bin\mysql\bin\mysql" -u root dmo_aashd_old < db_backup.sql
# Or try blank password (just press Enter)
"C:\MAMP\bin\mysql\bin\mysql" -u root -p dmo_aashd_old < db_backup.sql
# When prompted, just press Enter
bash
# Check your MAMP MySQL password
# For MAMP default password is usually "root" or blank
# Try these:
"C:\MAMP\bin\mysql\bin\mysql" -u root dmo_aashd_old < db_backup.sql
"C:\MAMP\bin\mysql\bin\mysql" -u root -p dmo_aashd_old < db_backup.sql
What worked
This worked:
E:\dumpTempFolder>"C:\MAMP\bin\mysql\bin\mysql" -u root --password=root dmo_aashd_old < posts1.sql
This will also work ( if MySQL is Added to PATH (windows) ):
E:\dumpTempFolder> mysql -u root --password=root dmo_aashd_old < posts1.sql
E:\dumpTempFolder>mysql -u root -p root dmo_aashd_old < db_backup.sql
Connect With Us on Social Media