Loading Data from an SQL Dump FileThe previous section showed you how to backup your databases. Let’s see how to restore them from those backups.
To load the structures and data in a dump file, you can tell the MySQL monitor to read in the SQL commands from the file:
mysql>
SOURCE dumpfile.sqlAlternatively, you can simply run the MySQL monitor in batch mode and execute the
instructions in the dump file mysql mysql_options < dumpfile.sqlWe don’t recommend this approach, as it’s a little less portable than the
SOURCE
com- mand;
more importantly, it doesn’t show you any error and warning messages as the
SQL statements are processed.
If the backup file doesn’t have CREATE DATABASE
and
USE
statements, you’ll need to type these into the monitor before
you read in the dump file, or add them to the dump file if you want to run the monitor in batch mode. A good step prior to carrying out a restore operation is to inspect the backup file with a text editor. Once you’ve inspected the file, you can decide whether you need to drop and recreate databases, use databases whether you need to take any other steps prior to a restore operation. Of course, you can use the mysqldump options to control what’s written to the dump file when it’s created.
You previously backed up the
music database to the file music.sql. The way you did this didn’t include any CREATE DATABASE
and
USE
statements in the dump file, so you need to use the monitor to enter these yourself.
Start
the monitor as the root user mysql --user=root --password=the_mysql_root_passwordNow, drop the existing music database:
mysql>
DROP DATABASE music;and create anew (empty) database with the same name:
mysql>
CREATE DATABASE music;Then select the music database as the active database:
mysql>
USE music;Now you can restore
the data by reading in the music.sql dump file:
mysql>
SOURCE music.sql;If your
music.sql file isn’t in the current directory, you should specify the full path. If you used our earlier suggestions in Dumping a Database as SQL Statements this
Share with your friends: