Restore a MySQL backup file (.sql) from the terminal

June 15, 2013 | 718 views | Comments

Efficiently import MySql backup database records from an .sql file using the command mysql from the terminal.

For example.

mysql -uNAME -pPASSWORD DATABASE < database_backup.sql

Where:

  1. NAME = the user who has access to the database.
  2. PASSWORD = the password of the user.
  3. DATABASE = the name of the database where the backup file will be imported.
  4. database_backup.sql = the sql file to be restored.

Notes:

  • Because the path where the .sql file to be restored was not specified, mysql will assume that the .sql file (database_backup.sql) is on the same location where the command is executed.
  • The same command can also be executed on windows command line.

 

Other Examples

Do not include the password on the command, and enter only when promted.

mysqldump -uNAME -p DATABASE < database_backup.sql

 

Specifically import from linux home directory.

mysqldump -uNAME -pPASSWORD DATABASE < /home/database_backup.sql

 

Specifically import from windows drive D.

mysqldump -uNAME -pPASSWORD DATABASE < D:\database_backup.sql