MySQL Backup Utility - Mysqldump
MySQL Backup Utility - Mysqldump
● 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:
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
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: [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:
For example, the following command makes a backup of the classicmodels and world databases:
To make a backup of all databases in a MySQL Server, you use the -A option:
The following statement makes a backup of all databases in the current MySQL server:
To make a backup of one or more tables from a database, you use the following command:
For example, to make a backup of the employees table from the classicmodels database, you use the following command:
To make a backup of the database structure only, you use the -d option:
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:
To make a backup of the database data only, you use the -t option:
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:
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
mysql -u root -p
USE hr;
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
USE hr;
- Finally, retrieve data from the employees table to verify the restoration:
-Syntax:
- To back up some tables, you specify a list of table names after the database name in the mysqldump command:
< 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.