| by Arround The Web | No comments

How to Use Different Flags While Exporting MySQL Database?

MySQL databases store the data of the user in a structured way. Sometimes this data is crucial for any data loss, so the user should back up on regular or weekly bases. The mysqldump client utility aids in exporting MySQL databases to a SQL file. This guide will discuss different flags while exporting MySQL databases using the mysqldump client utility.

Export a Single Database to a SQL File

To export a single MySQL database in a SQL file, use this syntax:

mysqldump -u [username] -p [db-name] > [output-file-name].sql

In the syntax provide, your MySQL username, name of the database that you want to export and name of new SQL file that will be created. Let’s see an example; for this post, the username is “md”, the name of the database is “linuxhint” and the name for the SQL file is “exported_db.sql” so the command would become this:

mysqldump -u md -p linuxhint > exported_db.sql

To confirm if the file is created or not after the execution of the previous command, run:

where [output-file-name]

Note: This syntax will be used to verify the existence of newly created SQL files, throughout this post.

The output will display that the MySQL database is exported in an SQL file successfully:

Export Multiple Specified Databases to a SQL File

The mysqldump also provides the facility of exporting multiple databases in a single SQL file by using the “–databases” flag. The syntax for exporting multiple databases is given below:

mysqldump -u [username] -p --databases [db-name-1] [db-name-2] > [output-file-name].sql

If you want to export “2” or more databases in the file, provide their names with a space in between. For this post let’s export “linuxhint” and “newlinuxhint” databases in a file named “exported_db_databases.sql” by typing this command:

mysqldump -u md -p --databases linuxhint newlinuxhint > exported_db_databases.sql

The error-free output indicates the successful execution the process, use the “where” command to verify whether the file is created or not:

Your multiple databases are exported in a single MySQL file.

Export All Databases to a SQL File

Users might feel a need to export all databases available in MySQL server to a SQL file. The mysqldump allows you to do that by using the “–all-databases” flag. The syntax is given below:

mysqldump -u [username] -p --all-databases > [output-file-name].sql

Provide the username and name of the SQL file. For this post, the username is “md” and the name of the SQL file is “exported_db_all_databases.sql”, so the command would look like this:

mysqldump -u md -p --all-databases > exported_db_all_databases.sql

The output displays that file is created successfully:

Export Only Structure of a Database to a SQL File:

The “–no-data” flag of mysqldump aids the user to export only the structure of the database without exporting the data. Use the given below syntax:

mysqldump -u [username] -p --no-data [db-name] > [output-file-name].sql

For this post, Let’s export the “linuxhint” database with only structure in a SQL file “exported_db_structure.sql”, by running this command:

mysqldump -u md -p --no-data linuxhint > exported_db_structure.sql

To verify if the file is created or not:

The output shows that the SQL file is created successfully.

Export Only Data From a Specific Table to a SQL File

Sometimes, the user wants to export only data of a specific table without the information of the “CREATE” statements, to o so use the “–no-create-info” flag in the mysqldump, as shown in the syntax:

mysqldump -u [username] -p [db-name] [table-name] --no-create-info > [output-file-name].sql

If you want to export data of “supplier” in a SQL file named “exported_db_specific_table.sql” by running this command:

mysqldump -u md -p linuxhint supplier --no-create-info > exported_db_specific_table.sql

To verify if the file created successfully or not use the “where” command:

The output is displaying that SQL file is created successfully.

Export Multiple Specified Tables to a SQL File

The mysqldump can be used to export multiple specified tables by using the “–tables” flag in a SQL file using this syntax:

mysqldump -u [username] -p [db-name] --tables [table-name1] [table-name2] > [output-file-name].sql

For example, if the user wants to export “Supplier” and “items” table from the database “linuxhint” in a SQL file named “exported_db_specific_tables.sql”, run the given below command:

mysqldump -u md -p linuxhint --tables supplier items > exported_db_specific_table2.sql

The error-free output displays that the command is executed successfully, you can use the “where” command to confirm the creation of the SQL file:

Flags That can be Used With mysqldump

The rest of the flags will be discussed using the syntax only throughout this post. Replace “db-name” with the name of your database, “table-name” with the name of the table and “output-file-name” with the name for your SQL file that will be created on successful execution of the export command.

Export Multiple Rows of a Table Using a Single Insert Statement in a SQL File

While working with a database containing large tables, the “–extended-insert” flag can be used to export them efficiently as it uses multiple rows “INSERT” statements, instead of a single row “INSERT” command which is the default method while exporting tables. It speeds up the export time. Use this syntax for using the “–extended-insert” flag:

mysqldump -u [username] -p [db-name] [table-name] --extended-insert > [output-file-name].sql

Export Records From a Specific Table that matches a Condition

To export the records from a table where a certain condition is fulfilled, use the “–where” flag that defines a condition for filtering the records that will be exported. To do so, use this syntax:

mysqldump -u [username] -p [db-name] [table-name] --where="condition" > [output-file-name].sql

The condition can be anything, for example, “id < 30”.

Export Binary Data by Converting Into Hexadecimal Format

The “–hex-blob” flag aids in exporting binary data in hexadecimal format. By default, the binary data is formatted in binary format. Using this flag is beneficial if the data accuracy is concerned; otherwise, it consumes more time than the normal export. The syntax is given below:

mysqldump -u [username] -p [db-name] [table-name] --hex-blob > [output-file-name].sql

Export a Database in an XML Format

To export a database in the XML format, use the “–xml” flag in the mysqldump command as shown in the syntax:

mysqldump -u [username] -p --xml [db-name] > [output-file-name].xml

Export a Database With “DROP DATABASE” Statement in a SQL File

To create the export file by adding the “DROP DATABASE” statement before the “CREATE DATABASE” statement so that in case of import it drops the database if it already exists by using “–add-drop-database”. Use this syntax:

mysqldump -u [username] -p --add-drop-database [db-name] > [output-file-name].sql

Export a Database With “DROP TABLE” Statement in a SQL File

To create the export file by adding the “DROP TABLE” statement before the “CREATE TABLE” statement so that in case of import it drops the table if it already exists by using “–add-drop-table”. Use this syntax:

mysqldump -u [username] -p --add-drop-table [db-name] > [output-file-name].sql

Export a Database by Excluding a Certain Table in a SQL File

To export a database by excluding the specified table by using the “–ignore-table” flag in the mysqldump command using this syntax:

mysqldump -u [username] -p --ignore-table=[db-name].[table-name] [db-name] > [output-file-name].sql

Export a Database and Compress the SQL File

To save disk space, the user can use the gzip tool to compress the SQL file that is containing the exported database, using the “–compress” flag. The syntax for compressing the SQL file is:

mysqldump -u [username] -p --compress [db-name] | gzip > [output-file-name].sql.gz

You have learned about different flags of mysqldump.

Conclusion

The mysqldump client utility aids in generating the logical backup of the databases into a SQL file. It is used to export single and multiple databases with their data and structures even. The user can also format and compress the SQL files. This post demonstrated different flags of mysqldump while exporting the MySQL database.

Share Button

Source: linuxhint.com

Leave a Reply