How to Backup and Restore (Export and Import) MySQL Databases Tutorial

phpMyAdmin can be used to export or backup MySQL databases easily. However, if the database size is very big, it probably won’t be a good idea. phpMyAdmin allows users to save database dump as file or display on screen, which involves exporting SQL statements from the server, and transmitting the data across slower network connection or Internet to user’s computer. This process slow the exporting process, increase database locking time and thus MySQL unavailability, slow the server and may simply crash the Apache HTTPD server if too many incoming web connections hogging the system’s resources.

The better way to backup and export MySQL database is by doing the task locally on the server, so that the tables’ data can be instantly dumped on the local disk without delay. Thus export speed will be faster and reduce the time MySQL database or table is locked for accessing. This tutorial is the guide on how to backup (export) and restore (import) MySQL database(s) on the database server itself by using the mysqldump and mysql utilities. There are basically two methods to backup MySQL, one is by copying all table files (*.frm, *.MYD, and *.MYI files) or by using mysqlhotcopy utility, but it only works for MyISAM tables. Below tutorial will concentrate on mysqldump which works for both MyISAM and InnoDB tables.

How to Export or Backup or Dump A MySQL Database

To export a MySQL database into a dump file, simply type the following command syntax in the shell. You can use Telnet or SSH to remotely login to the machine if you don’t have access to the physical box.

mysqldump -u username -ppassword database_name > dump.sql

Replace username with a valid MySQL user ID, password with the valid password for the user (IMPORTANT: no space after -p and the password, else mysqldump will prompt you for password yet will treat the password as database name, so the backup will fail) and database_name with the actual name of the database you want to export. Finally, you can put whatever name you like for the output SQL dump file, here been dump.sql.

The while data, tables, structures and database of database_name will be backed up into a SQL text file named dump.sql with the above command.

How to Export A MySQL Database Structures Only

If you no longer need the data inside the database’s tables (unlikely), simply add –no-data switch to export only the tables’ structures. For example, the syntax is:

mysqldump -u username -ppassword –no-data database_name > dump.sql

How to Backup Only Data of a MySQL Database

If you only want the data to be backed up, use –no-create-info option. With this setting, the dump will not re-create the database, tables, fields, and other structures when importing. Use this only if you pretty sure that you have a duplicate databases with same structure, where you only need to refresh the data.

mysqldump -u username -ppassword –no-create-info database_name > dump.sql

How to Dump Several MySQL Databases into Text File

–databases option allows you to specify more than 1 database. Example syntax:

mysqldump -u username -ppassword –databases db_name1 [db_name2 ...] > dump.sql

How to Dump All Databases in MySQL Server

To dump all databases, use the –all-databases option, and no databases’ name need to be specified anymore.

mysqldump -u username -ppassword –all-databases > dump.sql

How to Online Backup InnoDB Tables

Backup the database inevitable cause MySQL server unavailable to applications because when exporting, all tables acquired a global read lock using FLUSH TABLES WITH READ LOCK at the beginning of the dump until finish. So although READ statements can proceed, all INSERT, UPDATE and DELETE statements will have to queue due to locked tables, as if MySQL is down or stalled. If you’re using InnoDB, –single-transaction is the way to minimize this locking time duration to almost non-existent as if performing an online backup. It works by reading the binary log coordinates as soon as the lock has been acquired, and lock is then immediately released.

Syntax:

mysqldump -u username -ppassword –all-databases –single-transaction > dump.sql

How to Restore and Import MySQL Database

You can restore from phpMyAdmin, using Import tab. For faster way, upload the dump file to the MySQL server, and use the following command to import the databases back into the MySQL server.

mysql -u username -ppassword database_name < dump.sql

The import and export of MySQL database not only is important to recover the data when disaster strikes, but also provides an easy way to migrate or move to another server, such as when switching web hosting providers. However, do note that one common problem – character set encoding. Newer release of mysqldump uses UTF8 as its default charset if nothing is specified, while older versions (older than 4.1 typically) use Latin1 as default characterset. If you database charset is Latin1 and dump in UTF8 collation, the data may ends up become simply rubbish, garbled, or unreadable (frequently happen with Wordpress blog). If this case, use –default-character-set=charset_name option to specify the character set or convert the database to UTF8.


14 Responses to “How to Backup and Restore (Export and Import) MySQL Databases Tutorial”

  1. farooq
    March 20th, 2009 23:12
    14

    Its a great article..

    It really helped me alot

  2. Spurious
    February 16th, 2009 06:10
    13

    It’s –all-databases, not -all-databases.

  3. Th
    January 5th, 2009 06:45
    12

    Thanks alot :)

  4. aharown07
    December 3rd, 2008 02:35
    11

    Great article! But how do I do this on my pc under xampp? All I seem to be able to achieve are syntax errors.

  5. How to export and import MySql database | WebGuru’s Blog about Digital Life
    September 24th, 2008 23:36
    10

    [...] http://www.mydigitallife.info/2007/07/21/how-to-backup-and-restore-export-and-import-mysql-databases... Tags: Database, Export, Import, MySql [...]

  6. danielix
    September 4th, 2008 17:24
    9

    ciao sto cercando di effettuare il backup in locale su vista e iis 7.0

    eseguo questo file php

    db.sql

    ?>

    ma mi restituisce il seguente errore

    Parse error: syntax error, unexpected T_DEC in C:\inetpub\wwwroot\back.php on line 2

    qualche consiglio?

    grazie, daniele.

  7. Importar datos de MySQL de latin1 a UTF8 « Yvoictra Blog
    July 21st, 2008 07:57
    8

    [...] Enlace [...]

  8. tarot
    May 27th, 2008 23:45
    7

    Good post, but what about backing up the mysql users them selves?

  9. peanut
    April 14th, 2008 16:31
    6

    Good post. But you can add some advanced tips to it, to make it better. Goog luck!

  10. Mohammod Nizam Uddin
    February 16th, 2008 02:46
    5

    thanks alot .. you saved my time and money both .. hat off to you .

  11. tc
    February 6th, 2008 03:43
    4

    mysqldump worked.

    But ‘mysql’ to import data did not. The error is:
    ERROR at line 1: Unknown command ‘\m’

    any ideas?

  12. ferensick
    February 4th, 2008 13:17
    3

    mysql -u root -p < all-databases.sql
    done. :-)

  13. ferensick
    February 4th, 2008 12:48
    2

    It shows how to backup all-databases… but how to restore all of them at once from the same file? … I tried restoring the same way but receive a msg: unknown database: all-databases (I’ve done it before but I forget, it’s been a while… I’ll keep looking around)

  14. johnrobin
    January 26th, 2008 01:32
    1

    Nice post.. I usually just copy the files.. for temporary.. and get all mysql queries for permanent backup..

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to comments feature has been disabled. To receive notification of latest comments posted, subscribe to My Digital Life Comments RSS feed or register to receive new comments in daily email digest.
Custom Search

New Articles

Incoming Search Terms for the Article

mysqldump restore - mysqldump import - mysqldump utf8 - mysqldump encoding - mysql export database - mysqldump tutorial - mysql export - mysqldump syntax - mysql export utf8 - mysql export syntax - mysqldump recover - mySQL import dump - mysql import utf8 - mysql import charset - mysql import backup - import mysqldump - mysql export charset - phpmyadmin export encoding - mysqldump slow - mysql import syntax - phpmyadmin export utf8 - mysqldump example - mysqldump restore windows - mysql export dump - export mysql - phpMyAdmin export charset - mysql import all databases - mirakagi tutorial - restore mysqldump - mysql export encoding - all - phpmyadmin export tutorial - mysql import encoding - mysql restore utf8 - mysql import - mysql import slow - phpmyadmin export utf-8 - export mysql database - import mysql database - mysql import collation - mysql import database - mysql import all-databases - import mysql - mysqldump utf-8 - mysqldump syntax import - mysqldump restore example - mysql export table - mysqldump utf - restore data from mysqldump - import mysql backup - mysql export utf-8 - mysql import database dump - mysqldump import all databases - import mysql dump - mysql export innodb - mysql restore charset - mysqldump import export - mysqldump restore data - mysqldump restore table - mysql export import - import database dump - mysql import character set - restore mysqldump windows - mysql restore - phpmyadmin export - mysql import dump charset - import mysqldump file - mysqldump export table - phpmyadmin export latin1 - restore mysql dump ssh - mysql restore all databases - mysql export tutorial - phpmyadmin export character set - mysql import mysqldump - mysqldump examples -