If you want to keep your database secure and safe, then you have to back it up from time to time. And it is also true that there should always be a backup of the data. If ever there is an attack on the server. Or if the data is lost by mistake, then you can use your old backup.
In this tutorial, you will learn how to take backup and restore mysql database using command line or command prompt in windows.
How to Backup and Restore MySQL Database Command Line Windows
- Backup MySQL Database from Command Line Windows
- Step 1: Open Command Prompt
- Step 2: Navigate to MySQL Bin Directory
- Step 3: Run the mysqldump command
- Restore MySQL Database from Command Line Windows
- Step 1: Open Command Prompt
- Step 2: Navigate to MySQL Bin Directory
- Step 3: Create the Database
- Step 4: Restore the Database
Backup MySQL Database from Command Line Windows
By using these steps, you can take backup MySQL database from command line or command prompt in windows:
Step 1: Open Command Prompt
To start the backup mysql database process, So, press the “Windows key” + “R” and type “cmd” in the Run dialog box, then press Enter to open the Command Prompt:
Step 2: Navigate to MySQL Bin Directory
Now, execute the following command on command prompt to navigate to the MySQL bin directory:
cd C:\Program Files\MySQL\MySQL Server 8.0\bin
Note that:- Replace the path with the location where you have installed MySQL into your server.
Step 3: Run the mysqldump command
Execute the following mysqldump command on command prompt to backup the MySQL database:
mysqldump -u username -p database_name > C:\backup\database_name.sql
Before execute the above given command, please replace to followings:
- “username” with your MySQL username,
- “database_name” with the name of the database you want to backup
- “C:\backup\database_name.sql” with the path where you want to save the backup file
Once you run this command, the MySQL database backup will be saved in the same directory as the MySQL bin folder.
Restore MySQL Database from Command Line Windows
By using these steps, you can take restore MySQL database from command line or command prompt in windows:
Step 1: Open Command Prompt
To start the restore mysql database process, So, press the “Windows key” + “R” and type “cmd” in the Run dialog box, then press Enter to open the Command Prompt:
Step 2: Navigate to MySQL Bin Directory
Now, execute the following command on command prompt to navigate to the MySQL bin directory:
cd C:\Program Files\MySQL\MySQL Server 8.0\bin
Note that:- Replace the path with the location where you have installed MySQL into your server.
Step 3: Create the Database
If you do not have a database. Then execute the following command on command prompt to create a database:
mysql -u username -p -e "CREATE DATABASE database_name;"
Step 4: Restore the Database
To restore a MySQL database, execute the following command on command prompt to restore the MySQL database:
mysql -u username -p database_name < backup_file.sql
Before execute the above given command, please replace to followings:
- “username” with your MySQL username
- “database_name” with the name of the database you want to restore
- “backup_file.sql” with the name of the backup file
Once you run this command, the MySQL database will be restored from the backup file.
Conclusion
Backing up and restoring MySQL databases from the command line in Windows is a simple and effective way to protect your data and ensure business continuity. By following the steps outlined in this tutorial, you can create backups and restore MySQL databases with ease. Remember to schedule regular backups to ensure that you always have a recent and reliable copy of your data.