Home arrow MySQL arrow Page 2 - Backing Up Your MySQL Databases With MySQLDump
MYSQL

Backing Up Your MySQL Databases With MySQLDump


MySQL includes several ways to backup your important data. In this article Mitchell shows us how to backup our databases using the mysqldump utility that comes with MySQL. He looks at several examples including how to backup your database to a file, another server, and even a compressed gzip file.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 126
January 30, 2002
TABLE OF CONTENTS:
  1. · Backing Up Your MySQL Databases With MySQLDump
  2. · The mysqldump utility
  3. · More examples

print this article
SEARCH DEVARTICLES

Backing Up Your MySQL Databases With MySQLDump - The mysqldump utility
(Page 2 of 3 )

What is mysqldump?

The mysqldump utility is a console driven executable that allows us to specify a host of options to backup a database to an external resource such as a file, or even a completely different MySQL server running on the other side of the world!

I'm using the word backup rather loosely here, because MySQL doesn't actually backup our data per se. Rather, it creates a set of "CREATE TABLE" and "INSERT INTO" commands that can be executed against a MySQL server to re-create our database(s).

The mysqldump utility can be found in c:\mysql\bin on Windows operating systems, and in the /usr/local/bin directory on Unix/Linux systems where MySQL is installed. The mysqldump utility accepts several command-line arguments that you can use to change the way your databases are backed up.

In its simplest form, the mysqldump utility can be used like this:

mysqldump –-user [user name] –-password=[password] [database name] > [dump file]

Let's take a look at each of the arguments that can be passed to the mysqldump utility, as shown above:
  • --user [user name]: The --user flag followed by a valid MySQL username tells MySQL the username of the account that we want to use to perform the database dump. MySQL user accounts are stored in the "user" table of the "mysql" database. You can view a list of users and their permissions for your MySQL server by using the following code at the MySQL console application:

    use mysql;

    select * from user;
  • --password=[password]: The password for the user account mentioned above.
  • [database name]: The name of the database that we would like the mysqldump utility to backup. Instead of specifying one single database name, we could use either --databases or --all-databases to backup every single database on our MySQL server.
  • > [dump file]: If you're familiar with DOS and batch files, then you will know that the ">" symbol specifies that we are directing output to a stream, port, or file. For the mysqldump utility, we prepend a ">" to the filename we would like our database to be backed up to. If no path is specified for the file, then it will be created in the current directory.
Now that we're versed with the basic arguments that can be passed to the mysqldump utility, let's take a look at five different ways to use the mysqldump utility to backup our databases.

Example 1:

mysqldump –-user admin –-password=password mydatabase > sql.dump

In the example above, we're specifying that MySQL should check the grants for the user account of the "admin" user with a password of "password". I'm running MySQL on Windows 2000, and these are the default credentials for the admin user account. I have chosen to backup the database named "mydatabase" into the file sql.dump.

If you're not sure what the names of your databases are, then use the following command at the MySQL console application to list them:

show databases;

On my system, MySQL responded with this:

The list of database that exist on my MySQL server

Here's a snippet of my database dump using the mysqldump utility as described in the example above:

#

# Table structure for table 'tbl_contactemails'

#

CREATE TABLE tbl_contactemails (

pk_ceId int(11) NOT NULL auto_increment,

ceEmail varchar(250) NOT NULL default '',

ceType int(11) default NULL,

PRIMARY KEY (pk_ceId),

UNIQUE KEY id (pk_ceId)

) TYPE=MyISAM;

#

# Dumping data for table 'tbl_contactemails'

#

INSERT INTO tbl_contactemails VALUES (18,'mitchell@devarticles.com',1);

INSERT INTO tbl_contactemails VALUES (17,'mitchell_harper@hotmail.com',1);

INSERT INTO tbl_contactemails VALUES (16,'mytch@dingoblue.net.au',1);


As you can see, the mysqldump command has taken the design of my tbl_contactemails table (which exists as part of the "mydatabase" database that I chose to back up) and turned it into a CREATE TABLE query, which (when imported back into MySQL) will re-create the tbl_contactemails table if it needs to.

Also notice the three INSERT INTO statements, which add rows of data to the tbl_contactemails table? I had three email addresses in my contact emails table, and when this data is restored, MySQL will execute these insert commands directly against my tbl_contactemails database to add the rows back into the table.
blog comments powered by Disqus
MYSQL ARTICLES

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 – More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials