0% found this document useful (0 votes)
20 views19 pages

MySQL Backup Utility - Mysqldump

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views19 pages

MySQL Backup Utility - Mysqldump

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

MySQL Backup Utility: mysqldump

GV: Phạm Thị Quỳnh Trang


Introduction
- The mysqldump is a command-line utility in MySQL used for creating backups of
MySQL databases.
- The mysqldump tool allows you to dump the structure and/or data of one or more
databases into a file, which you can use to restore the databases later.
- In practice, you often use the mysqldump for backup and restore operations, database
migration, and transferring databases between servers.
Introduction
The mysqldump tool typically comes with the MySQL server installation by default. Its location depends on your operating system:

● Linux: on Linux systems, including distributions like Ubuntu, CentOS, and others, you can find mysqldump in the /usr/bin/
directory. You can use the which command to locate it: which mysqldump
● Windows: on Windows, you can find the mysqldump in the directory C:\Program Files\MySQL\MySQL Server X.Y\bin\, where
X.Y is the version number of the MySQL database server.
● macOS: Like Linux systems, you can find the mysqldump at /usr/bin/ directory on macOS.

It’s a good practice to include the directory that contains mysqldump in your system’s PATH environment variable so that you can
invoke the command from any location in the command prompt or terminal.
Basic syntaxes
There are three basic syntaxes for using the mysqldump tool:

1) Dump one or more tables


mysqldump -u root -p [options] db_name [tbl_name ...] > output_file

2) Dump one or more databases


mysqldump -u root -p [options] --databases db_name ... > output_file

3) Dump all databases


mysqldump -u root -p [options] --all-databases > output_file
The mysqldump options
The mysqldump tool provides you with two different ways to specify an option: long form and short
form.

For example, if you want to specify the long form for the user, you can use the --user=username
option:

--user=username

Alternatively, you can use the shorter and more concise option -u username:

-u username
The mysqldump options
The mysqldump tool examples
1) Creating a backup of a single database

The following command creates a backup of a single database:

mysqldump -u username -p -B db_name > path_to_backup_file


For example, the following command creates a backup of the database classicmodels to the file D:\backup\classicmodels.sql:

mysqldump -u root -p -B classicmodels > D:\backup\classicmodels.sql

It’ll prompt you to enter a password for the root account. After inputting a valid password for the root account, the tool will dump the
classicmodels database into the file D:\backup\classicmodels.sql

If you want to put the password in the command, you can use the --password long-form option:

mysqldump -u root --password=Abcd1234 -B classicmodels > D:\backup\classicmodels.sql

It’ll issue a warning:

mysqldump: [Warning] Using a password on the command line interface can be insecure.
The mysqldump tool examples
2) Creating a backup of multiple databases

To make a backup of multiple databases, you specify a list of the database names after the --database option:

mysqldump -u username -p -B <dbname1>[,<dbname2>, ...] > path_to_backup_file

For example, the following command makes a backup of the classicmodels and world databases:

mysqldump -u root -p --databases classicmodels world > D:\backup\databases.sql


The mysqldump tool examples
3) Creating a backup of all databases

To make a backup of all databases in a MySQL Server, you use the -A option:

mysqldump -u username -p -A > path_to_backup_file


Code language: plaintext (plaintext)

The following statement makes a backup of all databases in the current MySQL server:

mysqldump -u root -p -A > D:\backup\all_databases.sql


The mysqldump tool examples
4) Creating a backup of specific tables in a database

To make a backup of one or more tables from a database, you use the following command:

mysqldump -u username -p db_name tbl_name1, tbl_name2,... > path_to_backup_file

For example, to make a backup of the employees table from the classicmodels database, you use the following command:

mysqldump -u root -p classicmodels employees > d:\backup\employees.sql


The mysqldump tool examples
5) Creating a backup of the database structure only

To make a backup of the database structure only, you use the -d option:

mysqldump -u username -p -d db_name > path_to_backup_file


Code language: plaintext (plaintext)

The statement will generate a dump file that contains the SQL statement necessary to re-create the database structure. The dump file
does not contain INSERT statements.

For example, the following statement makes a backup of the database structure of the database classicmodels:

mysqldump -u root -p -d classicmodels > d:\backup\classicmodels.sql


The mysqldump tool examples
6) Creating a backup of data only

To make a backup of the database data only, you use the -t option:

mysqldump -u username -p -t db_name > path_to_backup_file

The statement will generate a dump file that contains the SQL statements necessary to lock tables and insert data into the tables. It has
no CREATE TABLE statements.

The following command makes a backup of data from the database classicmodels:

mysqldump -u root -p -t classicmodels > d:\backup\classicmodels.sql


Backing up a database
First, open a Command Prompt on Windows or Terminal program on Unix-like systems.

Second, execute the mysqldump program to back up the hr database:

mysqldump -h localhost -u root -p hr > D:\backup\hr.sql

After entering a valid password, the mysqldump program will create a backup of the hr database and store it in the hr.sql file located in
the D:\backup directory.
Backing up a database
Let’s break down each part of the command:

● mysqldump: This is the command-line utility for MySQL that allows you to dump the contents of a database into a file.
● -h localhost: This option specifies the hostname where the MySQL server is running. In this case, it’s set to “localhost,” which
means the MySQL server is on the same machine as the command is being executed. If you want to back up a database located
on a remote server, please specify the hostname of the server.
● -u root: This option specifies the MySQL user used for the connection. In this case, it’s set to “root,” which is a common default
superuser account in MySQL.
● -p: This option prompts the user for the MySQL password. After entering the command, you will be prompted to enter the
password for the specified user (in this case, the “root” user).
● hr: This is the name of the MySQL database that you want to back up. Replace “hr” with the actual name of the database you
want to back up.
● > D:\backup\hr.sql: This part of the command uses the output redirection symbol (>) to send the output of the mysqldump
command to the file "D:\backup\hr.sql".
Backing up a database

Accidentally deleting some rows from a table


First, connect to the MySQL:

mysql -u root -p

Second, switch the current database to the hr:

USE hr;

Third, delete the employee with id 3 from the employees table:

delete from employees where id = 3;

Suppose that this deletion is unintended and you want to restore the hr database to recover the data.
Restoring a database
- First, open the Command Prompt on Windows or Terminal on Unix-like systems.
- Second, use the following command to restore the hr database from the backup file created by the mysqldump utility:

mysql -u root -p hr < D:\backup\hr.sql

- Third, connect to the MySQL server:

mysql -u root -p

- Fourth, switch the current database to hr:

USE hr;

- Finally, retrieve data from the employees table to verify the restoration:

SELECT * FROM employees;


- The output indicates that the hr database has been fully restored.
Back Up and Restore All Databases in MySQL

-Syntax:

mysqldump -h localhost -u root -p --all-databases > D:\backup\all_databases.sql


> D:\backup\all_databases.sql: This part of the command uses the output redirection symbol (>) to send the output of the mysqldump
command to a file. In this case, the file is specified as D:\backup\all_databases.sql. The file will contain the SQL statements necessary to
recreate all databases and their data.

mysql -h localhost -u root -p < D:\backup\all_databases.sql


Backing Up MySQL Tables
- To make a backup of one table, you use mysqldump command with the following option:

mysqldump -h hostname -u username -p dbname tblname > table.sql

- To back up some tables, you specify a list of table names after the database name in the mysqldump command:

mysqldump -h hostname -u username -p dbname tblname1 tblname2 > table.sql


Restore MySQL Dump
- First, open the Command Prompt on Windows or Terminal program on Unix-like systems (macOS, Linux, Ubuntu, etc.).
- Second, execute the following command to restore databases from a dump file:

mysql -h hostname -P port -u username -p database_name < dump_file.sql

< dump_file.sql: This part of the command is used for input redirection. It takes the contents of the SQL dump file
(“dump_file.sql”) and uses them as input for the mysql command. This is how the data and structure from the dump file are
loaded into the specified MySQL database.

You might also like