Backup mysql database command line windows

Skip to content

Backup mysql database command line windows

For most database work I use phpMyAdmin. However, on the rare occasion I encounter a strange error — looking at your foreign key constraints — I opt for the Windows command prompt. I do this because a) troubleshooting database issues is far outside of my skillset and b) I need to get stuff done regardless of that. Also, the errors in phpMyAdmin typically don’t exist when performing the same task in the Windows command prompt (for the commands I run).

Dumping and Restoring MySQL databases on the Windows command prompt is a simple process.

  1. Fire up your Windows command prompt. In Windows 7, click the start orb and type cmd in the Search Programs and Files input and hit enter.

    Backup mysql database command line windows
    Start Windows Command Prompt
  2. If MySQL has been added to your Windows path variable, continue to step 3. If this is the first time you have run a mysql command in the Windows command prompt, it will return the error captured below.

    Backup mysql database command line windows
    MySQL Not Recognized

    Fear not, your computer just needs to know the location of MySQL to run MySQL-related commands. You tell Windows this location by adding the MySQL directory to your path variable. I covered how to add PHP to your Windows path variable using the Windows GUI in a different post. But in the spirit of going commando, let’s add MySQL’s location to the Windows path variable using the command prompt.

    • In your Windows command prompt, execute the command: set path=C:\path\to\mysql\executable, where C:\path\to\mysql\executable corresponds to the location of the mysql.exe file on your machine. I am running MySQL version 5.6.12, which was bundled with WAMP at my time of my download. So, the MySQL executable is located in C:\wamp\bin\mysql\mysql\mysql5.6.12\bin.

    • After you have added MySQL’s location to your Windows path variable, you should test the mysql command. Below is a screenshot of MySQL being added to the Windows path variable and the mysql command tested on my machine.

      Backup mysql database command line windows
      Add MySQL to Windows Path

      Now that mysql is recognized in the Windows command prompt, you can run the MySQL commands listed below to dump and restore your databases.

  3. To dump/export a MySQL database, execute the following command in the Windows command prompt: mysqldump -u username -p dbname > filename.sql. After entering that command you will be prompted for your password. Once the password is entered your dump file will be available in the root directory for your Windows user – ie: C:\Users\username. An example is shown in the screenshot below.

    Backup mysql database command line windows
    MySQL Dump Command
  4. To restore/import a MySQL database, execute the following command in the Windows command prompt: mysql -u username -p dbname < filename.sql. After entering that command you will be prompted for your password. Once the password is entered your dump file that exists in the root directory for your Windows user will be restored. An example is shown in the screenshot below.

    Backup mysql database command line windows
    MySQL Restore Command

That's it! We have successfully dumped and restored a MySQL database using the Windows command prompt.

How do I backup a MySQL database in Windows?

To back up an entire server, run this command:.
mysqldump --user root --password --all-databases > all-databases.sql..
“C:\Program Files\7-Zip\7z” a -p%password% archive.zip backup.sql..
mysql --user root --password mysql < all-databases.sql..

How do I backup MySQL command

To create a backup of all MySQL server databases, run the following command:.
mysqldump --user root --password --all-databases > all-databases.sql. ... .
mysql --user root --password mysql < all-databases.sql. ... .
mysql --user root --password [db_name] < [db_name].sql. ... .
select @@datadir;.

How do I backup my entire MySQL database?

Step 1: Create a MySQL Database Backup.
Open phpMyAdmin. On the directory tree on the left, click the database you want to back up. ... .
Click Export on the menu across the top of the display. You'll see a section called “Export Method.” Use Quick to save a copy of the whole database. ... .
Click Go..

How do you backup and restore MySQL database in Windows?

In Database Explorer, right-click the database and select Backup and Restore > Backup Database. In the Database Backup Wizard, select the database, specify a path to the backup file, and enter the name of the output file.