Abbey Workshop

MySQL: Exporting, Backup and Restore

This note briefly covers how to export a database out of MySQL and back in. This is useful for moving databases between test environments.

Exporting a Database

To export a database, use the mysqldump utility normally located in your mysql/bin directory . For example, to export all the tables and data for a database named guestdb:

mysqldump guestdb > guestdb.txt

This will create a text file containing all the commands necessary to recreate all the tables and data found in guestdb. However, what if I want to export only one table? To do this the command is modified as follows assuming guestTbl is the table to be exported.:

mysqldump guestdb guestTbl > guestdb.txt

One important point. The data created in the txt file assumes that no table exists on your destination server. The --add-drop-table option adds code to drop a table if it exists in the new database when the file is imported. Also, not login is used in this example. In many cases, you need to log in. To be able to perform the command. So a complete example of the command line looks like this.

mysqldump -p --user=username --add-drop-table guestdb guestTbl
Enter password: ********

The -p option tells MySQL to prompt for a password. The --user option sets your username, and --add-drop-table adds the code necessary to drop a table if it exists during import. The output from the command looks like this:

guestdb.txt

# MySQL dump 8.11
#
# Host: localhost Database: guestdb
#--------------------------------------------------------
# Server version 3.23.28-gamma

#
# Table structure for table 'guestTbl'
#

DROP TABLE IF EXISTS guestTbl;
CREATE TABLE guestTbl (
msgID int(11) NOT NULL auto_increment,
name varchar(30),
email varchar(30),
msgFrom varchar(30),
msgDate timestamp(14),
msgBody text,
PRIMARY KEY (msgID)
);

#
# Dumping data for table 'guestTbl'
#

INSERT INTO guestTbl VALUES (1,'Test User1','testuser@test.com','Smallville USA'
,20010101182157,'This is teh body for message 1.');
INSERT INTO guestTbl VALUES (2,'Test User2','testuser2@test.com','Smallville USA
',20010101182352,'This is teh body for message 2.');
INSERT INTO guestTbl VALUES (3,'Test User3','testuser3@test.com','Smalltown USA'
,20010101182451,'This is teh body for message 3.');
INSERT INTO guestTbl VALUES (4,'Test User4','testuser4@test.com','Smallcity USA'
,20010101182519,'This is teh body for message 4.');

Importing the Database

With the data in a text file, its time to import the data back into MySQL. This can be done by passing the commands contained in the text file into the MySQL client. For example:

mysql -p --user=username < guestdb.txt

This passes all the commands in the file into the mysql client just like you were typing them in.

Note: While developing this tip, it was noted that the command created were missing a use statement. You made need to add a use database; statement to the text file to make the command work.

Backing Up and Restoring a MySQL Database

To make a database backup using the MySQL command-line utilities, execute the following command:

$ mysqldump -u username -p --opt dbname > dump.sql

If the MySQL programs are not in your path, you will need to manually specify the location of the mysqldump program:

$ /usr/local/mysql/bin/mysqldump -u username -p --opt dbname > dump.sql

Download the dump.sql and keep it in a safe place (CDR, Zip disk, etc).

If you need to restore your database, you can do so like this:

$ mysql -u username -p --database=dbname < dump.sql

For more information, see the following books

Google
  Web abbeyworkshop.com   

Copyright © Abbey Workshop 2006

Music Only Search
The Black MP3 Album
The MP3 Album Lyrics
Wprld Music Plus
Mp3 Songs Blog
Music Search Store
First Music Search
Mega MP3 Blog
E MP3 BLog
My MP3 Diary
Fast MP3 Finder
My Blog MP3
Mp3 Songs Mart
My MP3 Songs Album
Top Music Group
Mp3 Tune Records
Song Album Guide
Music DataDite
MP3 Buying Guide
Top MP3 Downloads
MP3 Tune Festival
FAST MP3 Downloads
MP3 Mart Online
Upper MP3
Premium MP3 Site
Music Data online
Fine MP3 Audio
Cheap Music Albums
Fine mp3 downloads
World MP3 Music Records
Mp3 Music Data Direct
The Fine Mp3
MP3 Files World
Home Music Store
DatMP3
Fat MP3 Audio
MP3 ZigZag
MP3 Songs Direct
MP3 Tune World
E MP3 Log
Boom MP3 Music
Speedy MP3
Top 20 MP3 music
Deep MP3 World
Deep MP3 Download
Best-20 MP3
Top Twenty MP3
Deep MP3
Big MP3 Guide
Deep MP3 Audio
Mp3 R.O.B.
MP3-Pop Online
Greate-20-MP3
MP3 Appear
3T-MP3