How to Export MySQL Database using Command Line?
Last Updated :
12 Jun, 2024
Exporting MySQL databases using the command line utility in Windows is a knowledge every Database Administrator and developer should possess. The mysqldump utility is an easy-to-use program that can back up databases, replicate or transfer data from one server to another and migrate databases.
In this article, We will outline how to back up one database, multiple databases and particular tables in a database using the Windows Command Prompt in detail.
Prerequisites:
- MySQL is installed and running on your Windows system.
- Necessary credentials (username and password) for the MySQL database.
- Access to the Command Prompt.
Advantages of mysqldump
- Ease of Use: Simple commands for various export needs.
- Versatility: Supports exporting single, multiple, or all databases.
- Compatibility: Works across different MySQL versions.
- Portability: SQL files generated can be easily transferred and imported.
Export MySQL Database Using Command Line
Steps we are using in this article to export MySQL database using the command line:
- Open Command Prompt
- Navigate to the MySQL Bin Directory
- Use the mysqldump Command
- Enter Your Password
- Verify the Export
1. Open Command Prompt
Open the Command Prompt by pressing Win + R or typing cmd, and hitting Enter.
2. Navigate to the MySQL Bin Directory
If mysql and mysqldump commands are not in our system's PATH, navigate to the MySQL bin directory. This is usually located at:
cd C:\Program Files\MySQL\MySQL Server X.Y\bin
Replace X.Y with our MySQL version.
Example:
We can see in the below image that my version is 5.7. So the syntax becomes:
cd C:\Program Files\MySQL\MySQL Server 5.7\bin
MySQL Bin DirectoryExporting a Single Database
Now first lets see the list of databases which we have and select which database we want to export:
show databases
Output:
Show database command3. Use the mysqldump Command
The basic command to export a single database is:
mysqldump -u [username] -p [database_name] > [output_file.sql]
Replace [username], [database_name], and [output_file.sql] with your MySQL username, the name of the database, and the desired output file name, respectively.
Example:
To export a database named bookstoredb to a file named bookstoredb_dump.sql:
mysqldump -u root -p bookstoredb > D:\Dumps\bookstoredb_dump.sql
4. Enter Your Password
After entering the command, we'll be prompted to enter your MySQL password. Type your password and press Enter.
Enter password5. Verify the Export
- After this command is executed, we can check where the export has been successful by visiting the file location.
- This file can be opened with any text editor to view the SQL statements that define the layout of the database and its contents.
Exporting Multiple Databases
The same steps for exporting multiple databases also:
Syntax:
To export multiple databases, use the --databases option followed by the database names:
mysqldump -u [username] -p --databases [db1] [db2] > [output_file.sql]
Replace [username], [db1], [db2], and [output_file.sql] with your MySQL username, the names of the databases, and the desired output file name, respectively.
Example:
Select the databases which we want to export using the below command:
show databases
Now lets export databases named bookshop, namdb and salesdb to a file named multiple_backup.sql:
mysqldump -u root -p --databases bookshop namdb salesdb > D:\Dumps\multiple_backup.sql
Enter your MySQL password when prompted.
Exporting Specific Tables from a Database
Same steps for exporting multiple databases also:
Syntax:
To export specific tables, list the table names after the database name:
mysqldump -u [username] -p [database_name] [table1] [table2] > [output_file.sql]
Replace [username], [database_name], [table1], [table2], and [output_file.sql] with your MySQL username, the database name, the table names, and the desired output file name, respectively.
Example:
Lets select the customer and users tables from the bookstoredb database:
show tables
Output:
Show tables commandTo export table customer and users from the bookstoredb database to a file named tables_backup.sql:
mysqldump -u root -p bookstoredb users customer > D:\Dumps\bookstoredb_customer_users.sql
Enter your MySQL password when prompted.
Conclusion
The mysqldump utility in the Windows Command Prompt is a reliable way of backing up and exporting MySQL databases. Regardless of whether you require exporting a single database, multiple databases or certain tables, the directions provided herein make the exercise easy and convenient. Addition options make it possible to have different exports to meet as many needs as possible. It is also advisable to regularly check your backup files to confirm that your data is safe and secure so that the database functions effectively.
Similar Reads
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read