The mysqldump
command in MySQL is used to create backups of databases. One of its options, -F
or --flush-logs
, allows you to flush the MySQL server logs before starting the dump operation. This can be particularly useful when you want to ensure that the backup captures all recent changes up to the point of the dump.
Here are several examples demonstrating how to use mysqldump
with the -F
option:
Example 1: Flush logs and dump a specific database named ‘example’:
$ mysqldump -F -u username -p example > example_backup.sql
This command flushes the logs before dumping the ‘example’ database into a file named ‘example_backup.sql’. To verify the command’s execution, you can check if the file ‘example_backup.sql’ exists and contains data from the ‘example’ database.
Example 2: Flush logs and dump all databases:
$ mysqldump -F -u username -p --all-databases > all_databases_backup.sql
This command flushes the logs and dumps all databases into a file named ‘all_databases_backup.sql’. To verify, check if the file ‘all_databases_backup.sql’ exists and contains data from all databases on the server.
Example 3: Flush logs and dump a specific table from a database:
$ mysqldump -F -u username -p example table_name > table_backup.sql
This command flushes the logs and dumps only the ‘table_name’ from the ‘example’ database into a file named ‘table_backup.sql’. Verify by examining ‘table_backup.sql’ for the desired table data.
Example 4: Flush logs and compress the dump output:
$ mysqldump -F -u username -p example | gzip > example_backup.sql.gz
This command flushes the logs and dumps the ‘example’ database while compressing the output into ‘example_backup.sql.gz’. Check if ‘example_backup.sql.gz’ exists and is a valid gzip file.
Example 5: Flush logs and dump using a socket connection:
$ mysqldump -F -u username -p --socket=/path/to/mysql.sock example > example_backup.sql
This command flushes the logs and dumps the ‘example’ database using a specified socket connection. Verify by ensuring ‘example_backup.sql’ contains the expected data.
Example 6: Flush logs and dump, specifying a host:
$ mysqldump -F -u username -p -h hostname example > example_backup.sql
This command flushes the logs and dumps the ‘example’ database from a specific host (‘hostname’). Verify by checking ‘example_backup.sql’ for the database dump from the specified host.
Example 7: Flush logs and dump with specific character encoding:
$ mysqldump -F -u username -p --default-character-set=utf8 example > example_backup.sql
This command flushes the logs, dumps the ‘example’ database with UTF-8 character encoding, and stores it in ‘example_backup.sql’. Verify by examining ‘example_backup.sql’ for UTF-8 encoded data.
Example 8: Flush logs and dump, ignoring tables:
$ mysqldump -F -u username -p --ignore-table=example.table_name example > example_backup.sql
This command flushes the logs and dumps the ‘example’ database while ignoring the specified table (‘table_name’). Verify by checking ‘example_backup.sql’ to ensure ‘table_name’ data is excluded.
Example 9: Flush logs and dump, including triggers:
$ mysqldump -F -u username -p --triggers example > example_backup.sql
This command flushes the logs and dumps the ‘example’ database, including triggers. Verify by reviewing ‘example_backup.sql’ for the presence of triggers from the ‘example’ database.
Example 10: Flush logs and dump, including routines:
$ mysqldump -F -u username -p --routines example > example_backup.sql
This command flushes the logs and dumps the ‘example’ database, including stored routines. Verify by examining ‘example_backup.sql’ for the dumped routines along with other database content.
Also check similar articles.
Escaping Fields in mysqldump Output
Optionally Enclosing Fields in mysqldump Output
Enclosing Fields in mysqldump Output
Setting Field Terminator in mysqldump Output
Using Extended INSERT Syntax in mysqldump
Discussion about this post