Exporting and Importing a MySQL Database


At some juncture you will be faced with the task of moving an existing website from one server to another. It’s an easy enough task to make a backup and transfer your website’s public_html directory to a new server, but all of your content lives in a MySQL database, and you will need to bring it with you if you want to keep your site intact.There are a couple of options at your disposal for this task. One is to use the web-based interface PHPMyAdmin, which you will find in the database section of Cpanel if you are a Cpanel user. The other is to ssh into the servers and use the mysql command if you’re on a unix server.

For most people the easiest way to do this is to use PHPMyAdmin on both ends. It’s self explanatory, just choose the database you need to export and save it to your local computer as a file. Or, use ssh in and use mysqldump at the command line:

mysqldump -u database_username -p name_of_database > foo.sql

Which will prompt you for the database user’s pasword, and then dump the database to foo.sql.

Depending on how large your website is, we can approach the task of importing it to the new site in one of two ways.

First, before you import the old database to your new box, create a database and a database user and password for the new server in Cpanel, just like you had to do on the old server. Make sure you associate the database user you create with the new database, and make sure you give that user all permissions.

If you have a small MySQL database, <100megs or so depending on your server’s PHPMyAdmin settings, you can use PHPMyAdmin to import the database. I recently moved my site via PHPMyAdmin, and it went without a hitch, but the largest database I had was just over 110 megs. I moved my friend’s website to my server last week, and his database was over 300 megs and required everything to be done from the command line. Moving his site went even smoother than moving mine, but I understand that some of you may find the command line intimidating and will prefer to use PHPMyAdmin.

If your website’s database is larger, chances are that PHPMyAdmin will timeout after the database is partially imported. Don’t panic. I think that using the command line is just as easy or easier, and is less cryptic than using the PHPMyAdmin interface.

You will need to upload the SQL dump to your new server. Once there, close out your ftp program and fire up a terminal. SSH into your new server and switch to the directory you uploaded your SQL dump to. A simple mysql command in your shell will get you up and running:

mysql -u database_username -p name_of_database < foo.sql

This will bring up a password prompt. Just enter the password of the database user as you created it in Cpanel, and the database will be imported.

Now it’s time to kick the tires and make sure everything is ok after the move. Remember, if there is a problem, you can always make a fresh dump of the database and import it again. Maybe you used PHPMyAdmin and checked a box you shouldn’t have, maybe your new database’s database user wasn’t given all permissions. Whatever the issue, it’s nothing that can’t be fixed.

Becoming familiar with exporting and importing MySQL databases isn’t rocket science, and I guarantee you it’s a skill you will use again someday. Take your time, be sure of what you’re doing before you start hammering the keyboard, and your move will painless.

Bookmark & Share

Related Posts

About the Author

author photo

Michael was a bass player in a hardcore punk band in the 80's and spent the 90's building and riding custom Harleys. As strange a combination as it may seem, Mike also has some coder and sysadmin in his history as well. At 42 Mike's now a husband and dad, and works as a Corrections Officer in a maximum security lockdown unit by day, and is admin at AV Enthusiast and contributor to Connected Internet when time allows. Mike is also passionate about food and travel.

See All Posts by This Author

There Are 3 Responses So Far. »

  1. #1

    Nice tutorial.

    Once you master this, the next step to do is to script the export and then rename foo.sql with date info to indicate which backup this is, like foo-YYYY-MM-DD.sql. Finally, schedule the script to run on a daily/weekly/monthly/whatever basis.

    That’s what I do with the http://ww2db.com database, which is among one of my larger and most critical databases. I’d hate to lose any data from this database that I’ve been carefully building up for the past several years!

  2. #2

    Hi!

    Have a look at http://www.mysqldumper.de/en/
    I love this script ;-)

  3. #3

    ssh everytime was easier for me
    here are some other useful comands:
    1.making dump as in the article
    mysqldump -u database_username -p name_of_database > foo.sql
    2. making archive (if base is large)
    tar zcf BASE.sql.tar.gz BASE.sql
    3. getting this archive from the other server
    wget http://blah…blah…/BASE.sql.tar.gz
    4. getting base from archive
    tar zxf BASE.sql.tar.gz
    5. importing to base
    mysql -u USER -p mp3 <BASE.sql

    hope it will be usefull to somebody

Subscribe without commenting

Post a Response


Comment Policy: Any comments are permitted only because the site owner is letting you post, and any comments could be removed for any reason at the absolute discretion of the site owner.
  • Subscribe Now!

    subscribe to my feed subscribe to my feed
    1 - What is RSS?
    2 - Or, signup & receive updates by email!
  • www.checkcost.co.uk

    Read Reviews & Compare

    Computer

    Laptops

    Printer

    MP3 Players

    Compare Prices

  • Polls

    Which Mobile OS Will Be The Winner in 2009?






    View Results

    Loading ... Loading ...
  • RSS Windows 7 News

    • An error has occurred; the feed is probably down. Try again later.
  • Meta