Article

Home » Server-side Coding » PHP & MySQL Tutorials » Diary of A Webmaster Part 4 - Backing Up With MySQLDump

About the Author

Mitchell Harper

author_mitchellharper Mitchell is the co-founder and product manager at BigCommerce—SaaS ecommerce software which is used by thousands of businesses to sell online. He can be reached via email at mitch@bigcommerce.com

View all articles by Mitchell Harper...

Diary of A Webmaster Part 4 - Backing Up With MySQLDump

By Mitchell Harper

February 27th, 2002

Reader Rating: 8.5

Page: 1 2 3 Next

MySQL is one of the most popular database management systems for the development of interactive Websites that need to utilize persistent data sources. As with all other popular database management systems, MySQL offers several methods to backup your important data. In this article we'll look at how to backup your databases using the mysqldump utility that comes with MySQL. We'll review several examples using mysqldump, including the backup of your database to a file, another server, and even a compressed gzip file.

I'll assume that you have MySQL installed locally on a Windows, Unix or Linux machine, and that you have administrative privileges on that machine. I'll also assume that you've had at least a small amount of exposure to MySQL and the SQL language syntax.

The mysqldump Utility

What is mysqldump?

The mysqldump utility is a console-driven executable that lets us 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 usually be found in c:\mysql\bin on Windows operating systems, and in the /usr/local/mysql/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 inserting the following code at the MySQL command prompt:

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'll know that the ">" symbol specifies that we're directing output to a stream, port, or file. For the mysqldump utility, we prepend a ">" to the filename to which we would like our database to be backed up. If no path is specified for the file, then it will be created in the current directory.

Now that we're versed in 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.

Method 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've 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 command prompt to list them:

show databases;

On my system, MySQL responded with this:678example1.gif
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.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article