The mysqldump
command in MySQL is used to create backups of MySQL databases. It provides several options to customize the backup process. One such option is --tables
, which allows you to specify particular tables to include in the backup, overriding the default behavior of dumping the entire database specified with --databases
.
When using --tables
, you list the names of the tables you want to dump after the option. Here are some examples to illustrate its usage:
Example 1: Dump a single table named users
from the database mydatabase
:
mysqldump --tables mydatabase users
This command generates SQL statements that can recreate the users
table from the mydatabase
database.
Example 2: Dump multiple tables orders
and customers
from the database sales
:
mysqldump --tables sales orders customers
This command creates a backup containing the structure and data of both the orders
and customers
tables from the sales
database.
Example 3: Dump tables from different databases, users
from db1
and products
from db2
:
mysqldump --tables db1 users db2 products
This command backs up the specified tables from their respective databases, db1
and db2
.
Example 4: Dump all tables from a database testdb
excluding a table named logs
:
mysqldump --tables testdb --ignore-table=testdb.logs
Here, the --ignore-table
option is used in conjunction with --tables
to exclude the logs
table from the backup of testdb
.
Example 5: Dump a table with a non-standard name, such as customer_data
:
mysqldump --tables sales customer_data
This command ensures that the customer_data
table from the sales
database is included in the backup.
Example 6: Dump all tables from a database archive
:
mysqldump --tables archive
This straightforward command creates a backup containing all tables from the archive
database.
Example 7: Dump tables with a wildcard pattern, such as all tables starting with data_
from the database db1
:
mysqldump --tables db1 data_%
Here, data_%
is used as a wildcard pattern to include all tables from db1
whose names start with data_
.
Example 8: Dump a table with special characters in its name, like user_info
:
mysqldump --tables sales user_info
This command ensures that the table user_info
from the sales
database is backed up correctly, handling any special characters in its name.
Example 9: Dump tables from a database archive
while specifying the path to store the dump file:
mysqldump --tables archive > /path/to/backup.sql
This command redirects the output of mysqldump
to a specific file location, /path/to/backup.sql
, instead of printing it to the console.
Example 10: Dump tables using a configuration file myconfig.cnf
that specifies database connection details:
mysqldump --defaults-file=myconfig.cnf --tables mydatabase users
This command uses a configuration file, myconfig.cnf
, to provide MySQL connection parameters and then dumps the users
table from the mydatabase
database.
Also check similar articles.
Creating Tab-Separated Output Files with mysqldump
Handling Failed SSL Session Data Reuse in mysqldump
Setting SSL Session Data File in mysqldump
Setting TLS 1.3 Cipher in mysqldump
Configuring SSL FIPS Mode in mysqldump (OpenSSL Only)
Discussion about this post