MySQL database backup, recovery, Import and Export

Source: Internet
Author: User
6.4.1. Dumping data in SQL format Mysqldump

This section describes how to useMysqldumpTo create SQL-format dump files. For information about reloading such dump files, see Section 6.4.2, "reloading SQL-format backups ".

By default,MysqldumpWrites information as SQL statements to the standard output. You can save the output in a file:

shell> mysqldump [arguments] > file_name

To dump all databases, invokeMysqldumpWith--all-databasesOption:

shell> mysqldump --all-databases > dump.sql

To dump only specific databases, name them on the command line and use--databasesOption:

shell> mysqldump --databases db1 db2 db3 > dump.sql

The--databasesOption causes all names on the command line to be treated as database names. Without this option,MysqldumpTreats the first name as a database name and those following as table names.

With--all-databasesOr--databases,MysqldumpWritesCREATE DATABASEAndUSEStatements prior to the dump output for each database. this ensures that when the dump file is reloaded, it creates each database if it does not exist and makes it the default database so database contents are loaded into the same database from which they came. if you want to cause the dump file to force a drop of each database before recreating it, use--add-drop-databaseOption as well. In this case,MysqldumpWritesDROP DATABASEStatement preceding eachCREATE DATABASEStatement.

To dump a single database, name it on the command line:

shell> mysqldump --databases test > dump.sql

In the single-database case, it is permissible to omit--databasesOption:

shell> mysqldump test > dump.sql

The difference between the two preceding commands is that--databases, The dump output contains noCREATE DATABASEOrUSEStatements. This has several implications:

  • When you reload the dump file, you must specify a default database name so that the server knows which database to reload.

  • For reloading, you can specify a database name different from the original name, which enables you to reload the data into a different database.

  • If the database to be reloaded does not exist, you must create it first.

  • Because the output will contain noCREATE DATABASEStatement,--add-drop-databaseOption has no effect. If you use it, it produces noDROP DATABASEStatement.

To dump only specific tables from a database, name them on the command line following the Database Name:

shell> mysqldump test t1 t3 t7 > dump.sql
http://dev.mysql.com/doc/refman/5.1/en/mysqldump-sql-format.html
 
6.4.2. Reloading SQL-format backups

To reload a dump file writtenMysqldumpThat consists of SQL statements, use it as input toMySQLClient. If the dump file was createdMysqldumpWith--all-databasesOr--databasesOption, it containsCREATE DATABASEAndUSEStatements and it is not necessary to specify a default database into which to load the data:

shell> mysql < dump.sql

Alternatively, fromMySQL, UsesourceCommand:

mysql> source dump.sql

If the file is a single-database dump not containingCREATE DATABASEAndUSEStatements, create the database first (if necessary ):

shell> mysqladmin create db1

Then specify the database name when you load the dump file:

shell> mysql db1 < dump.sql

Alternatively, fromMySQL, Create the database, select it as the default database, and load the dump file:

mysql> CREATE DATABASE IF NOT EXISTS db1;mysql> USE db1;mysql> source dump.sqlhttp://dev.mysql.com/doc/refman/5.1/en/reloading-sql-format-dumps.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.