How to take complete backup of mysql database using mysqldump command line utility
Solution 1:
If you want to take a full backup i.e., all databases, procedures, routines, and events without interrupting any connections:
mysqldump -u [username] -p -A -R -E --triggers --single-transaction > full_backup.sql
-
-A
For all databases (you can also use--all-databases
) -
-R
For all routines (stored procedures & triggers) -
-E
For all events -
--single-transaction
Without locking the tables i.e., without interrupting any connection (R/W).
If you want to take a backup of only specified database(s):
mysqldump -u [username] -p [database_name] [other_database_name] -R -e --triggers --single-transaction > database_backup.sql
If you want to take a backup of only a specific table in a database:
mysqldump -u [username] -p [database_name] [table_name] > table_backup.sql
If you want to take a backup of the database structure only just add --no-data
to the previous commands:
mysqldump -u [username] –p[password] –-no-data [database_name] > dump_file.sql
mysqldump
has many more options, which are all documented in the mysqldump
documentation or by running man mysqldump
at the command line.
Solution 2:
It depends a bit on your version. Before 5.0.13 this is not possible with mysqldump.
From the mysqldump man page (v 5.1.30)
--routines, -R
Dump stored routines (functions and procedures) from the dumped
databases. Use of this option requires the SELECT privilege for the
mysql.proc table. The output generated by using --routines contains
CREATE PROCEDURE and CREATE FUNCTION statements to re-create the
routines. However, these statements do not include attributes such
as the routine creation and modification timestamps. This means that
when the routines are reloaded, they will be created with the
timestamps equal to the reload time.
...
This option was added in MySQL 5.0.13. Before that, stored routines
are not dumped. Routine DEFINER values are not dumped until MySQL
5.0.20. This means that before 5.0.20, when routines are reloaded,
they will be created with the definer set to the reloading user. If
you require routines to be re-created with their original definer,
dump and load the contents of the mysql.proc table directly as
described earlier.
Solution 3:
Use these commands :-
mysqldump <other mysqldump options> --routines > outputfile.sql
If we want to backup ONLY the stored procedures and triggers and not the mysql tables and data then we should run something like:
mysqldump --routines --no-create-info --no-data --no-create-db --skip-opt <database> > outputfile.sql
If you need to import them to another db/server you will have to run something like:
mysql <database> < outputfile.sql