HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

mysqldump doesn't restore database. why not?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
whymysqldumpdatabasedoesnnotrestore

Problem

Why doesn't my restore operation work?

I created a dumpfile for a database using:

mysqldump -u root -p databasename > /home/databasename_bkup.sql


I then opened the dumfile and confirmed that it contains CREATE TABLE statements for each of the tables in the database. So I dropped the database and re-created an empty database of the same name before running the following restore command:

mysqldump -u root -p databasename < /home/databasename_bkup.sql


This restore command resulted in the following printing in the terminal:

-- MySQL dump 10.13  Distrib 5.6.23, for Linux (x86_64)
--
-- Host: localhost    Database: databasename
-- ------------------------------------------------------
-- Server version   5.6.23

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2015-03-05 17:56:29


But then when I log into mysql to check the contents of the database, I found out that the database is empty, as follows:

```
mysql> use databasename;
Database changed
mysql>

Solution

You have to use the mysql client to reload

mysql -u root -p -Ddatabasename < /home/databasename_bkup.sql


Another way to reload would be

mysql -u root -p -Ddatabasename


then from the MySQL prompt, do this

mysql> source /home/databasename_bkup.sql


If you would like the mysqldump to drop and recreate the database for you, create the dump like this:

mysqldump -u root -p --add-drop-database -B databasename > /home/databasename_bkup.sql


Then, running the script

mysql -u root -p < /home/databasename_bkup.sql


does the DROP DATABASE command for you.

Code Snippets

mysql -u root -p -Ddatabasename < /home/databasename_bkup.sql
mysql -u root -p -Ddatabasename
mysql> source /home/databasename_bkup.sql
mysqldump -u root -p --add-drop-database -B databasename > /home/databasename_bkup.sql
mysql -u root -p < /home/databasename_bkup.sql

Context

StackExchange Database Administrators Q#94552, answer score: 28

Revisions (0)

No revisions yet.