How to backup and import a MySQL InnoDB database

Due to the way Innodb tables are structured, you cannot perform a hotback up of this database without using third party tools. Therefore you must take your database offline for a small period of time while you take the backup.
Backing up MySQL
MySQL backups are performed using the common mysqldump tool. This is a command line utility that ships with MySQL and you use at as follows:

% mysqldump –user=user –password=pass –opt DBNAME > dumpfile.sql

You may also need to specify the –host= parameter to force the hostname you are connecting to. This depends largely on how you’ve setup your user security. This will produce a text file with a series of INSERT/DROP/CREATE SQL statements that will recreate the database.

The –opt flag is very important. This is shorthand to pass in many flags at once; –add-drop-table –add-locks –create-options –disable-keys –extended-insert –lock-tables –quick –set-charset. This ensures that your database is in a good state while the backup is performed, including restricting all write access while the backup is in operation. Any locks placed will be automatically removed when this utility finishes.

Restoring a backup
Restoring a backup, or importing from an existing dump file can take a long time depending on the number of indexes and primary keys you have on each table. You can speed this process up dramatically by modifying your original dump file by surrounding it with the following:

SET AUTOCOMMIT = 0;
SET FOREIGN_KEY_CHECKS=0;

.. your dump file ..

SET FOREIGN_KEY_CHECKS = 1;
COMMIT;
SET AUTOCOMMIT = 1;

This turns off all the checks and auto-commits. This is a safe operation to do if you are fully restoring a database since the previous dump has already been validated for legal keys. If however you are importing, or adding to an existing database, then this step is not advisable.

You can then easily import the SQL file into MySQL using:

% mysql –user=user –password=pass DBNAME < dumpfile.sql

or using

mysql> SET FOREIGN_KEY_CHECKS = 0;
mysql> SOURCE dump_file_name
mysql> SET FOREIGN_KEY_CHECKS = 1;
mysql> COMMIT;

Backing up without taking MySQL offline
You cannot perform this with Innodb with the tools supplied with MySQL. However you can get around this limitation by running two MySQL database instances. You setup another MySQL database running on another machine, that is a replication server. Replication features come part of the standard MySQL installation. This replication server can be taken offline for as long as you wish while the backup is performed from the replicated server. Once online, it will resync itself up with any operations that were done while it was offline.

Tags:

3 Responses to “How to backup and import a MySQL InnoDB database”

  1. ZACHARY says:


    PillSpot.org. Canadian Health&Care.No prescription online pharmacy.Special Internet Prices.Best quality drugs. High quality drugs. Buy drugs online

    Buy:VPXL.Cialis Professional.Super Active ED Pack.Levitra.Viagra Soft Tabs.Tramadol.Zithromax.Viagra.Propecia.Cialis.Viagra Super Active+.Viagra Professional.Maxaman.Cialis Super Active+.Viagra Super Force.Soma.Cialis Soft Tabs….

  2. CHARLES says:


    Medicamentspot.com. Canadian Health&Care.Best quality drugs.No prescription online pharmacy.Special Internet Prices. No prescription drugs. Buy drugs online

    Buy:Lipitor.Lasix.Zetia.Buspar.SleepWell.Acomplia.Amoxicillin.Benicar.Advair.Female Pink Viagra.Ventolin.Nymphomax.Female Cialis.Lipothin.Wellbutrin SR.Seroquel.Zocor.Cozaar.Prozac.Aricept….

  3. HUBERT says:


    CheapTabletsOnline.Com. Canadian Health&Care.Special Internet Prices.Best quality drugs.No prescription online pharmacy. No prescription pills. Order pills online

    Buy:Buspar.Acomplia.Nymphomax.Female Cialis.Advair.Lipothin.Wellbutrin SR.Amoxicillin.Aricept.Benicar.Cozaar.Seroquel.Female Pink Viagra.Ventolin.Lasix.Lipitor.SleepWell.Zetia.Zocor.Prozac….

Leave a Reply