How to Export Database in MySQL From the Command Line

Preet Sanghavi Feb 02, 2024
How to Export Database in MySQL From the Command Line

This tutorial explores the concept of exporting a database using the command line in MySQL.

Export Database in MySQL From the Command Line

Many-a-times, it becomes important for businesses and organizations to export a database when they migrate, copy, store or share a database. MySQL helps us get this done using the command line.

In this tutorial, we will learn how to route our command line to the bin directory in MySQL and how to export a database.

We will divide this tutorial into three steps.

  1. Open the command prompt.
  2. Go to the bin directory.
  3. Write the command to export the database.

We begin by opening the command prompt using the Windows+R key. This key would trigger Windows to open run, as shown in the following picture.

open cmd

Now, we type cmd in our run box to open the command prompt.

command prompt

Once the command prompt opens, we need to direct ourselves into the root directory of MySQL; it would give access to our MySQL server.

It can be done with the command below (make sure to change the below command with your MySQL’s bin directory).

cd C:\wamp\bin\mysql\mysql5.1.36\bin

Within our server, we need to export our database. It can be done using the following command.

mysqldump -u user_name -p database_name > wantedsqlfile.sql

In this case, we use the following query (assuming that root is the username, the password is empty, and the database name is boatdb).

mysqldump -u root -p boatdb > file_Name.sql

export database commands

The image shows that the file is generated when we put the password. It can be visualized with the help of the following image.

exported sql file

Thus, with the help of mysqldump and set path commands, we can easily export any database using the command line.

Preet Sanghavi avatar Preet Sanghavi avatar

Preet writes his thoughts about programming in a simplified manner to help others learn better. With thorough research, his articles offer descriptive and easy to understand solutions.

LinkedIn GitHub

Related Article - MySQL Export