Troubleshooting MySQL Dump Import Issues: Why Tables and Data Aren’t Being Imported


3 views

html

I recently encountered a frustrating scenario where mysqldump wasn't importing any tables or data despite showing successful execution. The command output looked perfect, yet the target database remained empty. Here's what I discovered:

The key issue lies in using mysqldump for import operations. Many developers (myself included) mistakenly think the command works both ways:

# This is INCRECT for importing data
mysqldump -uuser -p --database targetdb < dump.sql

For database imports, you should use the MySQL client instead:

# Correct import method
mysql -uuser -p targetdb < dump.sql

When you pipe data into mysqldump:

  1. It reads the SQL file but doesn't execute it
  2. It outputs the same content with some header/footer additions
  3. No error occurs because technically it's "working" - just not as expected

Before importing, always check your dump file contains actual data:

# Check for table creation statements
grep "CREATE TABLE" dump.sql

# Check for INSERT statements
grep "INSERT INTO" dump.sql | head -n 5

Here's the proper end-to-end process:

# 1. Create dump (optional - if you need a fresh export)
mysqldump -uuser -p sourcedb > backup.sql

# 2. Create target database (if needed)
mysql -uuser -p -e "CREATE DATABASE IF NOT EXISTS targetdb"

# 3. Import data
mysql -uuser -p targetdb < backup.sql

# 4. Verify import
mysql -uuser -p -e "SHOW TABLES IN targetdb"

For large databases or special cases:

# Using source command from MySQL prompt
mysql -uuser -p
USE targetdb;
SOURCE /path/to/dump.sql;

# For compressed dumps
zcat dump.sql.gz | mysql -uuser -p targetdb

If you're still facing issues:

  1. Check file permissions on the dump file
  2. Verify the database user has proper privileges
  3. Look for character encoding mismatches
  4. Try importing in smaller chunks

Remember that database operations can be destructive - always test with non-critical data first and maintain backups.


I recently encountered a perplexing situation where mysqldump wasn't importing any tables or data despite showing successful execution. The command:

mysqldump -umydbuser -p --database testimport < database.dump

produced output that looked completely normal, yet the target database remained empty. Here's what I discovered during troubleshooting.

The core issue wasn't with the dump file itself - examining it showed complete CREATE TABLE and INSERT statements. The problem lay in how mysqldump was being invoked for the import operation.

The fundamental mistake was using mysqldump for importing data. While this might seem logical (using the same tool for both export and import), mysqldump is actually only for creating dumps - not for restoring them. The proper tool for importing is the mysql client.

To properly import a mysqldump file, you should use:

mysql -umydbuser -p testimport < database.dump

Key differences from the incorrect approach:

  • Using mysql instead of mysqldump
  • Specifying the database name directly rather than with --database flag

After running the correct import command, verify your data with:

mysql -umydbuser -p -e "SHOW TABLES IN testimport; SELECT COUNT(*) FROM table_name;"

For large databases, consider these more efficient approaches:

# Using pv for progress monitoring
pv database.dump | mysql -umydbuser -p testimport

# For compressed dumps
zcat database.dump.gz | mysql -umydbuser -p testimport
  • Mixing up mysqldump (export) and mysql (import) commands
  • Not specifying the target database name in the mysql command
  • Using --database flag when importing (it's only needed for exports)
  • Not verifying file permissions on the dump file

If you're still facing issues, try these diagnostic steps:

# Check for errors in the MySQL error log
tail -f /var/log/mysql/error.log

# Test with a minimal database first
echo "CREATE TABLE test(id INT); INSERT INTO test VALUES(1);" | mysql -umydbuser -p testimport

For large imports, these options can significantly improve performance:

mysql -umydbuser -p testimport --init-command="SET autocommit=0; SET unique_checks=0; SET foreign_key_checks=0;" < database.dump