Import and Export Databases Guide
Introduction
Learn how to backup (export) and restore (import) MySQL databases using phpMyAdmin. Essential skills for data migration, backup strategies, and disaster recovery.
Exporting Databases
Quick Export (Simple Backup)
Steps:
- Open phpMyAdmin
- Select the database from left sidebar
- Click “Export” tab in main panel
- Select “Quick” export method
- Format: SQL (recommended for most cases)
- Click “Go”
- File downloads automatically
Default Settings:
- Format: SQL
- Includes: All tables
- Includes: Structure and data
- File: database_name.sql
Custom Export (Advanced Options)
Steps:
- Select database
- Click “Export” tab
- Select “Custom” export method
- Configure options (see below)
- Click “Go”
Export Options:
Tables to Export
- Select All: Export all tables
- Select Specific: Choose individual tables
- Unselect All: Deselect all then choose
Output Options
- View output as text: Display in browser
- Save output to file: Download file (recommended)
- Compression:
- None
- zipped (recommended for large databases)
- gzipped
Format Options
- SQL: Standard (most compatible)
- CSV: For spreadsheets
- JSON: For modern applications
- XML: For data exchange
- PDF: For documentation
- Other: Various specialized formats
SQL Options (when format is SQL)
Structure:
- Add DROP TABLE / DATABASE
- Add CREATE TABLE / DATABASE
- Add IF NOT EXISTS
- Add AUTO_INCREMENT value
- Enclose export in transaction
- Disable foreign key checks
Data:
- Complete inserts (includes column names)
- Extended inserts (multiple values in one INSERT)
- Max length of created query
- Use hexadecimal for BLOB
Exporting Specific Tables
Steps:
- Select database
- Click “Export” tab
- Select “Custom” method
- Under “Tables”, select specific tables
- Configure other options
- Click “Go”
Exporting Only Structure
Use Case: Create empty database with same structure
Steps:
- Select database
- Export → Custom
- Under “Dump table”:
- Check “structure”
- Uncheck “data”
- Click “Go”
Exporting Only Data
Use Case: Backup data without structure
Steps:
- Select database
- Export → Custom
- Under “Dump table”:
- Uncheck “structure”
- Check “data”
- Click “Go”
Scheduled Exports (Advanced)
For automatic backups, use command-line tools:
# WAMP
cd C:\wamp64\bin\mysql\mysql[version]\bin
mysqldump -u root -p database_name > backup.sql
# XAMPP Windows
cd C:\xampp\mysql\bin
mysqldump -u root -p database_name > backup.sql
# XAMPP macOS/Linux
/opt/lampp/bin/mysqldump -u root -p database_name > backup.sql
Export Best Practices
1. Regular Backups
- Daily: For production databases
- Weekly: For development databases
- Before major changes: Always
2. File Naming Convention
database_name_YYYYMMDD_HHMMSS.sql
Example: mystore_20241115_143000.sql
3. Storage
- Keep multiple backup versions
- Store in different locations
- Test restore periodically
4. Compression
- Use for large databases
- Saves storage space
- Faster transfer
Importing Databases
Quick Import
Steps:
- Open phpMyAdmin
- Select existing database OR create new database
- Click “Import” tab
- Click “Choose File”
- Select .sql file
- Leave settings as default
- Click “Go”
Import with Custom Settings
Steps:
- Select database
- Click “Import” tab
- Choose file
- Configure format and options
- Click “Go”
Import Options:
File Format
- Automatic (recommended)
- SQL
- CSV
- Open Document Spreadsheet
- Other formats
Partial Import
- Allows file uploads up to: (PHP limit shown)
- Number of queries to skip: Resume interrupted imports
- For large files, may need to adjust PHP settings
SQL Options
- SQL compatibility mode
- Do not use AUTO_INCREMENT value
- Ignore INSERT errors
- Partial import: number of queries to skip
Importing Large Databases
Method 1: Split Large SQL Files
Using Terminal:
# Split file into 10MB chunks
split -b 10m large_database.sql chunk_
# Import each chunk
mysql -u root -p database_name < chunk_aa
mysql -u root -p database_name < chunk_ab
# etc.
Method 2: Increase PHP Limits
Edit php.ini:
upload_max_filesize = 256M
post_max_size = 256M
max_execution_time = 600
max_input_time = 600
memory_limit = 512M
Location:
- WAMP:
C:\wamp64\bin\php\php[version]\php.ini - XAMPP:
C:\xampp\php\php.ini
After editing:
- Save file
- Restart Apache
- Retry import
Method 3: Use MySQL Command Line
Windows (WAMP):
cd C:\wamp64\bin\mysql\mysql[version]\bin
mysql -u root -p database_name < C:\path\to\backup.sql
Windows (XAMPP):
cd C:\xampp\mysql\bin
mysql -u root -p database_name < C:\path\to\backup.sql
macOS/Linux:
mysql -u root -p database_name < /path/to/backup.sql
Method 4: BigDump Tool
For very large databases, consider using BigDump PHP script:
- Handles files up to several GB
- Resumes interrupted imports
- No server configuration needed
Creating Database Before Import
Option 1: phpMyAdmin
- Click “Databases” tab
- Enter new database name
- Select collation
- Click “Create”
- Then proceed with import
Option 2: Include in SQL File
CREATE DATABASE IF NOT EXISTS database_name;
USE database_name;
-- rest of SQL statements
Importing CSV Files
Steps:
- Create table with matching structure first
- Select the table
- Click “Import” tab
- Choose CSV file
- Configure CSV options:
- Format: CSV
- Column names in first row
- Field delimiter (usually comma)
- Field enclosure (usually quote)
- Line terminator
- Click “Go”
CSV Import Options:
- Columns separated with:
,or;or tab - Columns enclosed with:
"or' - Columns escaped with:
\ - Lines terminated with:
autoor\nor\r\n - Column names: Check if first row has column names
Common Import/Export Scenarios
Scenario 1: Migrate Database to New Server
Export from old server:
- phpMyAdmin → Select database
- Export → Custom
- Check “Add DROP TABLE”
- Check “Add CREATE TABLE”
- Download .sql file
Import to new server:
- phpMyAdmin → Create new database
- Import → Choose file
- Upload and import
Scenario 2: Backup Before Major Changes
-- Quick backup
-- Export → Quick → Go
-- Make changes
-- If problems occur, import backup
Scenario 3: Copy Database Within Same Server
Method 1: Export/Import
- Export original database
- Create new database
- Import into new database
Method 2: Operations Tab
- Select database
- Operations → Copy database
- Enter new name
- Click “Go”
Scenario 4: Clone Database Structure
Export structure only:
- Export → Custom
- Structure only (no data)
- Download
Import to new database:
- Create new database
- Import structure file
Scenario 5: Transfer Data Only
Export data only:
- Export → Custom
- Data only (no structure)
- Download
Import:
- Tables must already exist
- Import data file
Troubleshooting
Error: File Too Large
Solution:
- Increase PHP limits in php.ini
- Use command line import
- Split file into smaller parts
- Use BigDump tool
Error: Maximum Execution Time Exceeded
Solution:
- Increase
max_execution_timein php.ini - Use command line import
- Import in smaller batches
Error: SQL Syntax Error
Solution:
- Check MySQL version compatibility
- Try SQL compatibility mode
- Review the error line in SQL file
- Ensure correct character encoding
Error: Duplicate Entry
Solution:
- Enable “Add DROP TABLE” on export
- Check for existing data before import
- Use “Ignore INSERT errors” option
- Clear target database first
Error: Foreign Key Constraint
Solution:
- Enable “Disable foreign key checks” on export
- Import in correct order (parent tables first)
- Temporarily disable constraints:
SET FOREIGN_KEY_CHECKS=0; -- import data SET FOREIGN_KEY_CHECKS=1;
Error: Character Encoding Issues
Solution:
- Ensure database collation matches
- Set proper character set on export:
SET NAMES utf8mb4; - Verify file encoding (should be UTF-8)
Partial Import Completed
Solution:
- Note the last successful query number
- Use “Number of queries to skip” option
- Resume from that point
Command Line Import/Export
Export via Command Line
Full database:
mysqldump -u username -p database_name > backup.sql
With compression:
mysqldump -u username -p database_name | gzip > backup.sql.gz
Specific tables:
mysqldump -u username -p database_name table1 table2 > backup.sql
Structure only:
mysqldump -u username -p --no-data database_name > structure.sql
Data only:
mysqldump -u username -p --no-create-info database_name > data.sql
All databases:
mysqldump -u username -p --all-databases > all_databases.sql
Import via Command Line
Basic import:
mysql -u username -p database_name < backup.sql
From compressed file:
gunzip < backup.sql.gz | mysql -u username -p database_name
Create database first:
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS database_name;"
mysql -u root -p database_name < backup.sql
With progress indicator:
pv backup.sql | mysql -u username -p database_name
Automated Backups
Windows Task Scheduler
Create batch file backup.bat:
@echo off
set TIMESTAMP=%date:~-4%%date:~3,2%%date:~0,2%
cd C:\xampp\mysql\bin
mysqldump -u root -pYourPassword database_name > C:\backups\db_%TIMESTAMP%.sql
Schedule with Task Scheduler:
- Open Task Scheduler
- Create Basic Task
- Set trigger (daily, weekly)
- Action: Start a program
- Program: path to backup.bat
Linux Cron Job
Create script backup.sh:
#!/bin/bash
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
mysqldump -u root -pYourPassword database_name > /backups/db_$TIMESTAMP.sql
# Keep only last 7 days
find /backups -name "db_*.sql" -mtime +7 -delete
Add to crontab:
chmod +x backup.sh
crontab -e
# Add line:
0 2 * * * /path/to/backup.sh
Best Practices
Export Best Practices
- Regular Schedule: Daily for production
- Test Exports: Verify files are valid
- Multiple Locations: Store backups in different places
- Compression: Use for large databases
- Documentation: Document backup procedures
Import Best Practices
- Test First: Import to test database first
- Backup Before: Always backup before import
- Check Compatibility: Verify MySQL versions match
- Monitor Progress: Watch for errors
- Verify After: Check data integrity after import
File Management
- Naming: Use consistent naming convention
- Organization: Separate by date/version
- Cleanup: Remove old backups periodically
- Documentation: Keep import/export logs