您的位置:首页 > 数据库 > MySQL

Backup and Restore MySQL Database Using mysqldump

2010-01-07 10:52 561 查看
BackupandRestoreMySQLDatabaseUsingmysqldump

byRameshNatarajanonSeptember22,2008ShareThis

[mysqldump-MySQLBackup&Restore]mysqldumpisaneffectivetooltobackupMySQLdatabase.Itcreatesa*.sqlfilewithDROPtable,CREATEtableandINSERTintosql-statementsofthesourcedatabase.Torestorethedatabase,executethe*.sqlfileondestinationdatabase.ForMyISAM,usemysqlhotcopymethodthatweexplainedearlier,asitisfasterforMyISAMtables.

Usingmysqldump,youcanbackupalocaldatabaseandrestoreitonaremotedatabaseatthesametime,usingasinglecommand.Inthisarticle,letusreviewseveralpracticalexamplesonhowtousemysqldumptobackupandrestore.

Fortheimpatient,hereisthequicksnippetofhowbackupandrestoreMySQLdatabaseusingmysqldump:

backup:#mysqldump-uroot-p[root_password][database_name]>dumpfilename.sql

restore:#mysql-uroot-p[root_password][database_name]<dumpfilename.sql

HowToBackupMySQLdatabase

1.Backupasingledatabase:

Thisexampletakesabackupofsugarcrmdatabaseanddumpstheoutputtosugarcrm.sql

#mysqldump-uroot-ptmppasswordsugarcrm>sugarcrm.sql

#mysqldump-uroot-p[root_password][database_name]>dumpfilename.sql

Thesugarcrm.sqlwillcontaindroptable,createtableandinsertcommandforallthetablesinthesugarcrmdatabase.Followingisapartialoutputofsugarcrm.sql,showingthedumpinformationofaccounts_contactstable:

--

--Tablestructurefortable`accounts_contacts`

--

DROPTABLEIFEXISTS`accounts_contacts`;

SET@saved_cs_client=@@character_set_client;

SETcharacter_set_client=utf8;

CREATETABLE`accounts_contacts`(

`id`varchar(36)NOTNULL,

`contact_id`varchar(36)defaultNULL,

`account_id`varchar(36)defaultNULL,

`date_modified`datetimedefaultNULL,

`deleted`tinyint(1)NOTNULLdefault'0',

PRIMARYKEY(`id`),

KEY`idx_account_contact`(`account_id`,`contact_id`),

KEY`idx_contid_del_accid`(`contact_id`,`deleted`,`account_id`)

)ENGINE=MyISAMDEFAULTCHARSET=utf8;

SETcharacter_set_client=@saved_cs_client;

--

--Dumpingdatafortable`accounts_contacts`

--

LOCKTABLES`accounts_contacts`WRITE;

/*!40000ALTERTABLE`accounts_contacts`DISABLEKEYS*/;

INSERTINTO`accounts_contacts`VALUES('6ff90374-26d1-5fd8-b844-4873b2e42091',

'11ba0239-c7cf-e87e-e266-4873b218a3f9','503a06a8-0650-6fdd-22ae-4873b245ae53',

'2008-07-2305:24:30',1),

('83126e77-eeda-f335-dc1b-4873bc805541','7c525b1c-8a11-d803-94a5-4873bc4ff7d2',

'80a6add6-81ed-0266-6db5-4873bc54bfb5','2008-07-2305:24:30',1),

('4e800b97-c09f-7896-d3d7-48751d81d5ee','f241c222-b91a-d7a9-f355-48751d6bc0f9',

'27060688-1f44-9f10-bdc4-48751db40009','2008-07-2305:24:30',1),

('c94917ea-3664-8430-e003-487be0817f41','c564b7f3-2923-30b5-4861-487be0f70cb3',

'c71eff65-b76b-cbb0-d31a-487be06e4e0b','2008-07-2305:24:30',1),

('7dab11e1-64d3-ea6a-c62c-487ce17e4e41','79d6f6e5-50e5-9b2b-034b-487ce1dae5af',

'7b886f23-571b-595b-19dd-487ce1eee867','2008-07-2305:24:30',1);

/*!40000ALTERTABLE`accounts_contacts`ENABLEKEYS*/;

UNLOCKTABLES;

2.Backupmultipledatabases:

Ifyouwanttobackupmultipledatabases,firstidentifythedatabasesthatyouwanttobackupusingtheshowdatabasesasshownbelow:

#mysql-uroot-ptmppassword

mysql>showdatabases;

+--------------------+

|Database|

+--------------------+

|information_schema|

|bugs|

|mysql|

|sugarcr|

+--------------------+

4rowsinset(0.00sec)

Forexample,ifyouwanttotakebackupofbothsugarcrmandbugsdatabase,executethemysqldumpasshownbelow:

#mysqldump-uroot-ptmppassword--databasesbugssugarcrm>bugs_sugarcrm.sql

Verifythebugs_sugarcrm.sqldumpfilecontainsboththedatabasebackup.

#grep-i"Currentdatabase:"/tmp/bugs_sugarcrm.sql --CurrentDatabase:`mysql` --CurrentDatabase:`sugarcrm`

3.Backupallthedatabases:

ThefollowingexampletakesabackupofallthedatabaseoftheMySQLinstance.

#mysqldump-uroot-ptmppassword--all-databases>/tmp/all-database.sql

4.Backupaspecifictable:

Inthisexample,webackuponlytheaccounts_contactstablefromsugarcrmdatabase.

#mysqldump-uroot-ptmppasswordsugarcrmaccounts_contacts/

>/tmp/sugarcrm_accounts_contacts.sql

4.Differentmysqldumpgroupoptions:

*–optisagroupoption,whichissameas–add-drop-table,–add-locks,–create-options,–quick,–extended-insert,–lock-tables,–set-charset,and–disable-keys.optisenabledbydefault,disablewith–skip-opt.

*–compactisagroupoption,whichgiveslessverboseoutput(usefulfordebugging).Disablesstructurecommentsandheader/footerconstructs.Enablesoptions–skip-add-drop-table–no-set-names–skip-disable-keys–skip-add-locks

HowToRestoreMySQLdatabase

1.Restoreadatabase

Inthisexample,torestorethesugarcrmdatabase,executemysqlwith<asshownbelow.Whenyouarerestoringthedumpfilename.sqlonaremotedatabase,makesuretocreatethesugarcrmdatabasebeforeyoucanperformtherestore.

#mysql-uroot-ptmppassword

mysql>createdatabasesugarcrm;

QueryOK,1rowaffected(0.02sec)

#mysql-uroot-ptmppasswordsugarcrm</tmp/sugarcrm.sql

#mysql-uroot-p[root_password][database_name]<dumpfilename.sql

2.Backupalocaldatabaseandrestoretoremoteserverusingsinglecommand:

Thisisasleekoption,ifyouwanttokeeparead-onlydatabaseontheremote-server,whichisacopyofthemasterdatabaseonlocal-server.Theexamplebelowwillbackupthesugarcrmdatabaseonthelocal-serverandrestoreitassugarcrm1databaseontheremote-server.Pleasenotethatyoushouldfirstcreatethesugarcrm1databaseontheremote-serverbeforeexecutingthefollowingcommand.

[local-server]#mysqldump-uroot-ptmppasswordsugarcrm|mysql/

-uroot-ptmppassword--host=remote-server-Csugarcrm1

[Note:Therearetwo--(hyphen)infrontofhost]

Ifyoulikedthisarticle,pleasebookmarkitondel.icio.usandStumbleit.

#mysqldump-uroot-ptmppassword--databasesbugssugarcrm>bugs_sugarcrm.sql

Verifythebugs_sugarcrm.sqldumpfilecontainsboththedatabasebackup.

#grep-i"Currentdatabase:"/tmp/bugs_sugarcrm.sql

--CurrentDatabase:`mysql`

--CurrentDatabase:`sugarcrm`

3.Backupallthedatabases:

ThefollowingexampletakesabackupofallthedatabaseoftheMySQLinstance.

#mysqldump-uroot-ptmppassword--all-databases>/tmp/all-database.sql

4.Backupaspecifictable:

Inthisexample,webackuponlytheaccounts_contactstablefromsugarcrmdatabase.

#mysqldump-uroot-ptmppasswordsugarcrmaccounts_contacts/

>/tmp/sugarcrm_accounts_contacts.sql

4.Differentmysqldumpgroupoptions:

–optisagroupoption

,whichissameas
–add-drop-table,–add-locks,–create-options,–quick,–extended-insert,
–lock-tables,–set-charset,and–disable-keys.optisenabledby
default,disablewith–skip-opt.

–compactisagroupoption

,whichgives
lessverboseoutput(usefulfordebugging).Disablesstructurecomments
andheader/footerconstructs.Enablesoptions–skip-add-drop-table
–no-set-names–skip-disable-keys–skip-add-locks

HowToRestoreMySQLdatabase



1.Restoreadatabase

Inthisexample,torestorethesugarcrmdatabase,executemysql
with<asshownbelow.Whenyouarerestoringthedumpfilename.sql
onaremotedatabase,makesuretocreatethesugarcrmdatabasebefore
youcanperformtherestore.

#mysql-uroot-ptmppassword



mysql>createdatabasesugarcrm;

QueryOK,1rowaffected(0.02sec)

#mysql-uroot-ptmppasswordsugarcrm</tmp/sugarcrm.sql

#mysql-uroot-p[root_password][database_name]<dumpfilename.sql

2.Backupalocaldatabaseandrestoretoremoteserverusingsinglecommand:

Thisisasleekoption,ifyouwanttokeepa
read-onlydatabaseontheremote-server,whichisacopyofthemaster
databaseonlocal-server.Theexamplebelowwillbackupthesugarcrm
databaseonthelocal-serverandrestoreitassugarcrm1databaseon
theremote-server.Pleasenotethatyoushouldfirstcreatethe
sugarcrm1databaseontheremote-serverbeforeexecutingthefollowing
command.

[local-server]#mysqldump-uroot-ptmppasswordsugarcrm|mysql/

-uroot-ptmppassword--host=remote-server-Csugarcrm1


[Note:Therearetwo--(hyphen)infrontofhost]

Ifyoulikedthisarticle,pleasebookmarkitondel.icio.usandStumbleit.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: