How to restore a MySQL database?

To restore a MySQL database from a backup, you can use the mysql command-line tool. Here’s how to do it:

Restore Command

Use the following command to restore your database from a .sql backup file:

mysql -u username -p database_name < backup_file.sql

Explanation:

  • username: Your MySQL username.
  • database_name: The name of the database you want to restore. Make sure this database already exists; if not, create it first.
  • backup_file.sql: The path to your backup file.

Steps:

  1. Create the Database (if it doesn't exist):

    CREATE DATABASE database_name;
  2. Run the Restore Command:
    Execute the restore command in your terminal.

Example:

mysql -u root -p my_database < my_backup.sql

Important Notes:

  • Ensure you have the necessary permissions to restore the database.
  • Be cautious, as restoring will overwrite existing data in the database.

For further learning, consider exploring related labs on LabEx!

0 Comments

no data
Be the first to share your comment!