How to import and export MySQL database into an SQL file

Or Gzip for the matter. Here’s the command to run in your UNIX-based server to import or export via an SQL file, this is useful for performing backup and restoring of a MySQL database. (I wrote a similar import and export guide for PostgreSQL.) The mysqldump utility performs just that:

Exporting using mysqldump:

[code lang=”bash”]mysqldump -u[Username] -p[Password] [Database] > output.sql[/code]

For example, my username is ‘kahwee’, my password being ‘secret’ and database being ‘justrealized_db’, I would run the following to export my database to a SQL file:

[code lang=”bash”]mysqldump -ukahwee -psecret justrealized_db > output.sql[/code]

And to Gzip:

[code lang=”bash”]mysqldump -u[Username] -p[Password] [Database] | gzip > output.sql.gz[/code]

Importing using mysql:

To import back, we can use the mysql utility in a similar fashion, note that the > (greater than) has change to a < (lesser than).

[code lang=”bash”]mysql -u[Username] -p[Password] [Database] < output.sql[/code]

For example, my username is ‘kahwee’, my password being ‘secret’ and database being ‘justrealized_db’, I would run the following to import my database:

[code lang=”bash”]mysql -ukahwee -psecret justrealized_db < output.sql[/code]

And to Ungzip:

[code lang=”bash”]gunzip < output.sql.gz | mysql -u[Username] -p[Password] [Database][/code]


Backing up and restoring MySQL databases in Windows

Unfortunately, you can’t use gzip here. So all those commands above with gzip can’t work. The rest, however, still works. However, mysqldump and mysql may not be set in your system environment variables. These are instructions on how to add them for Windows Vista:

Editing system environment variables in Windows Vista.

Click on ‘Edit the system environment variables’, a dialog box will pop up. Click on ‘Environment Variables…’, you should be greeted with the following dialog box:

Editing the path for environment variables

My path looks like this before I add anything:

[code lang=”bash”]%SystemRoot%system32;%SystemRoot%;%SystemRoot%System32Wbem[/code]

Append your MySQL bin directory at the back of what is already there. I use XAMPP (XAMPP lite to be specific) which has its MySQL bin folder located here ‘;C:xampplitemysqlbin’, so I would be appending this:

[code lang=”bash”];C:xampplitemysqlbin[/code]

That’s all I guess, hope it is helpful for you.

How to backup and restore in PostgreSQL

I was fairly impressed with pgAdmin III when I first used it. It seems to be simple to use for anyone who can’t write their own SQL statements. I wasn’t too good and it and each time I execute a change in the database I have a feeling the world’s going to end. But one thing the pgAdmin III doesn’t do well is backup and restore.

Using pgAdmin III for PostgreSQL

(Using pgAdmin III for PostgreSQL. A screenshot.)

0. Objective

In the end it’s best to use the command prompt (or terminal) and in this really beginner tutorial, we’ll do a backup and restore using the command line.

1. Set PATH for PostgreSQL in Windows Vista

This guide assumes you install in ‘C:Program FilesPostgreSQL8.3’, the default installation directory. Continue reading “How to backup and restore in PostgreSQL”