Importing and Exporting data in MySQL
By: Troy Campano
(2005-01-27)
Filed under Articles > Databases > MySQL

Introduction
In my spare time I like to take photos...lots of photos. I put together a little PHP/MySQL app to host all the photos over a TroyGeek.com (http://www.troygeek.com/). Usually I do my PHP development on my Windows computer and then upload the code to my UNIX web host. I also run a copy of MySQL on my Windows computer and before I start my development I like to get a copy of what my 'production' database looks like and load it into my Mysql database on my Windows machine. This article will describe how to do this using the mysqldump and mysql command line utilities to import and export data to/from a MySQL database.

Setting Your Path
First what I like to do is to put the mysql and mysqldump programs in my command line path so I don't need to use the full path when calling the them. To do this on Windows XP, right click your 'My Computer' icon and go to properties. Under the Advanced tab you should see a button for 'Environment Variables'. Click that button and a new window should pop up showing two menu lists. The top menu is for 'user' environment variables, meaning only the user currently logged on to Windows can use these environment variables and the second menu list is for 'System' environment variables which are variables made available to anyone on the computer.

Under the System environment variables, look for a variable called 'Path'. Highlight that variable and click the 'Edit Variable' button. At the very of end the 'Variable Value' field add
;c:\mysql\bin assuming that c:\mysql is where you have MySQL installed. Note the semi-colon at the beginning...that seperates this statement from the one before it. Now you should be able to call the mysqladmin and mysql tools from a command prompt.

Getting the Data Out With Mysqldump
The main utility used to get data out of mysql is called mysqldump. It's a command line utility that works the same way on Windows, UNIX, Linux, etc. and is fairly easy to use. In the following example the mysqldump utility will take in your username, password, computer name, and dump the DDL and data from your database to a text file. Open up a command prompt (for Windows, go to Start > Run > and type in 'cmd'). Now type in the below statement.
mysqldump -u myUsername -p --host=localhost myDatabase > someTextFile.sql
If you hadn't added c:\mysql\bin to your path you would need to run this command:
c:\mysql\bin\mysqldump -u myUsername -p --host=localhost myDatabase > someTextFile.sql

...assuming c:\mysql is where you have installed MySQL.

In the example above the username to the database was
myUsername, the computer that the MySQL database runs on goes after --host (in this case we used localhost though it could be your unix server name) , the database name is myDatabase and we sent all the data we get to a file called sometextFile.sql. If you were to open up the someTextFile.sql file you should see all the CREATE and INSERT statements needed to rebuild your database. You can find more options with the mysqldump command here in the MySQL Manual (http://dev.mysql.com/doc/mysql/en/mysqldump.html)

Putting the Data Back
Now that you've gotten the data out, how do you get it back in? You can actually use the mysql command line utility you would normally use to run queries with. Make sure that any tables you want to create do not already exist. To create the tables and load data back into mysql you would run this command:

mysql -u myUsername -p myDatabase < someTextFile.sql
...where someTextFile.sql is the export you took in the previous example. Basically what this does is opens a connnection to mysql and then runs all the commands that are in the .sql file.

And that's about all you need to do to take exports/imports in MySQL!
As always, if you have trouble, please post your comments on this web page.

~ Troy Campano ~



Reader's Comments - Add Comment