Mysql::Import/Export Database using SSH

MySQL databases are not backed up like ordinary files. Even if it were possible, it would not be a good solution, as it could corrupt the database by doing so.

To back up your database, ssh to your account, then use the mysqldump command:

    mysqldump -ppassword DatabaseName > db.sql

(‘db.sql’ is the name of your backup dump file, which you can change to whatever name you choose)

That backs up the whole database in standard SQL text format. Both the structure (like CREATE table command) and data (like INSERT command) statements, can be backed up separately, which is usefull in many cases. Check:

mysqldump --help

for all the command line options.

To restore the dump, just type:

    mysql -p dbname < db.sql

Note that the structure dump won't work if tables already exist. The backup will not change or replace the structure.

For the data dump (the INSERT commands) it will append any new data if it does not yet exist.

If you have a database that is in frequent use, such as one linked to a dynamic web interface being updated by your web site visitors, it is a good idea to make a daily backup of the data.

To transfer a database, create the dump as described above, use ftp to upload it on the second server, and then restore it as above.

Backing up the database is not done through an ftp database directly. You first need to generate an sql dump, and then download this dump.

Use the following mysqldump command:

    mysqldump -ppassword username > somefile.sql

Then use the following command to view the dump:

    view somefile.sql

(exit with: q!)

To put the file back:

  • upload the file
  • edit it with a text editor, adding the below line to the top:
      use databasename (which is the same as your username)
  • save the file and issue the command:
      mysql -ppasswd < somefile.sql

All of the database will be generated, including structure. For more details, use

    mysqldump –help
Advertisements

5 thoughts on “Mysql::Import/Export Database using SSH

  1. Elians says:

    hay, informative article.

    i got guess from this article and used following command sequence on ‘cmd’ (command line interface for windows xp) to import mysql dump back to my database named “test_database”:

    1) cd C:\wamp\bin\mysql\mysql5.0.51a\bin

    2) mysql -u root -p test_database < my_dump_file.sql

    where ‘my_dump_file.sql’ was dump file of my database and i placed at C:\wamp\bin\mysql\mysql5.0.51a\bin\my_dump_file.sql

    depending on the size of your database, 2) may take sometime to execute so be patient

    thanks again

  2. Matt libner says:

    please add few more explained things like: this command
    mysqldump -u myusername -p DatabaseName > db.sql

    should run not on mysql console prompt but on server console prompt (using putty)

    informative, thanks

  3. ghassem yad says:

    hi,i am iranian .i guess you are iranian too. your article helped me a lot ,i could make the sqldump very easy ,i am trying to put my php files and mysql made database on remote server,how should i upload my sqldump file.

    thanks
    ghassem

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s