Site icon TestingDocs.com

MySQL mysqldump Export

Overview

In this tutorial, we will learn about MySQL mysqldump Export utility. The mysqldump utility dumps structure and contents of MySQL databases and tables.

We can use this utility in different ways:

 

mysqldump Utility

The mysqldump utility only dumps the SQL statements. In order to use the dumped statements, we need to connect to a target MySQL server using with a client, and execute the dumped SQL statements.

We can use mysqldump from the command shell. To view the options open command shell/prompt and issue the mysqldump command.

>mysqldump
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] –databases [OPTIONS] DB1 [DB2 DB3…]
OR mysqldump [OPTIONS] –all-databases [OPTIONS]
For more options, use mysqldump –help

Examples

To dump a database to a text (.txt) file or (.sql) file using the mysqldump utility, use the redirect operator
to indicate the filename and the location.

In this example,we will export all the database tables and the data for a database. To dump the full world database to ‘world.sql’ file, we can issue the following command:

> mysqldump world -u root -p > world.sql

 

The -p option tells MySQL to prompt for a password. The –u option sets the database username. For example, root user.

We can also dump a database to a text file using the mysqldump utility. To dump the world
database to ‘world.txt’ file:

> mysqldump world -u root -p > world.txt

The data created in the dump file assumes that no table exists on the destination database server.
The –add-drop-table option adds code to drop a table if it exists in the new target database
when the file is imported.

DROP TABLE IF EXISTS `city`;

For example, a statement like above before creating the city table.

We can use the MySQL Workbench tool to open the dump file when dumped in .sql file. On Windows, we can right click on the world.sql file and choose open with MySQL Workbench utility.

 

To dump some tables in the database, we can specify the tables as arguments after the database:

> mysqldump world Country City

This command will dump only the tables specified in the command.

If we do not specify any table names following the database name or if we use the –databases
or –all-databases option, the entire databases are dumped.

Dump multiple databases

To dump multiple databases, we can use the following commands:

> mysqldump—all-databases

> mysqldump—databases world sakila

MySQL Tutorials

MySQL Tutorials on this website:

https://www.testingdocs.com/mysql-tutorials-for-beginners/

For more information on MySQL Database:

https://www.mysql.com/

Exit mobile version