Using the mysqldump tool in Mysql June 19, 2010Posted by Tournas Dimitrios in Mysql.
This tutorial will cover using the mysqldump executable for backing up databases. If you are an average user of MySQL, and are not interested in administering a MySQL server, then this tutorial might be useless to you.The mysqldump tool is a backup program that is distributed with every MySQL download. If you aren’t familiar with the bin directory within your MySQL distro, then it’s time to start playing around with the tools inside it.
According to MySQL AB, a man named Igor Romanenko was the original author of the mysqldump tool. As with all software, especially open source, it has been hacked, smacked, slapped around and rewritten 17 times since then (those aren’t actual statistics). When using the mysqldump tool, it produces SQL statements to create table structure, populate tables, or both.I will assume you are using Linux command line to practice using this tool, but the same principles apply to Windows. Please do not play with production data if this is your first time using this application. You can screw things up if you aren’t careful.The first step is to get into the MySQL bin directory. If your installation of MySQL is in /usr/local/mysql (Linux installation), then let’s get there by typing the following:
Now that we are inside our bin directory, let’s list the files within and see what we’ve got.
Now you should see the mysqldump tool along with a bunch of other stuff that will eventually get covered in other tutorials. Although we are only going to cover the commonly used and mostly basic options, let?s look at all that can be used with mysqldump.The command to backup an entire database is simple:
mysqldump database_name > filename_to_create.sql
Keep in mind that you will likely be required to use the ?p (password) and/or ?u (username) to backup a database. That user must have permissions to access that particular database. For instance, if we needed to backup the mysql database, and were using the root user, we would type the following:
mysqldump mysql > mysql_backup.sql ?u root ?p
At this point, MySQL will prompt us to input a password for the user root. After doing so, the backup process will begin, and a new file filled with all of the SQL creations and inserts for the mysql database will be available. Pretty handy, huh? What if we need to backup ALL databases? This is actually just as simple!
With the list of options provided with the link above, one of the option was ?–all-databases?. This will dump all databases into the file you command. Let?s try it!
mysqldump ?all-databases > alldbs.sql ?u root ?p
This will dump all databases into an SQL file called alldbs.sql. Now that we have finally backed up some databases, what do we need to do to restore them?
This can actually be a little tricky at first. You won?t be using mysqldump to do this, but rather mysql itself. There are two possible ways to do this. The first is to restore the data directly (using brute force). I don?t like this way, and if the SQL is formatted perfectly, then you could run into some issues. At all costs, here is the format:
mysql db_name < file.sql
Again, permissions will be a factor here. Here is my favorite method:
mysql ?e ?source backup_file.sql? db_name
That?s it! That is way too easy.Before closing this tutorial, I want to show you how to copy databases and their data from one server to another. This is extremely handy if you have a development, test, and production area serving different databases:
mysqldump ?opt db_name | mysql ?host=remote_host -C db_name
The mysqldump tool is just too cool! If you have any questions, feel free comment me.