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.
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:
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:
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.
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:
On my system, MySQL responded with this:
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)
# Dumping data for table 'tbl_contactemails'
INSERT INTO tbl_contactemails VALUES (18,'firstname.lastname@example.org',1);
INSERT INTO tbl_contactemails VALUES (17,'email@example.com',1);
INSERT INTO tbl_contactemails VALUES (16,'firstname.lastname@example.org',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.