How to restore mysql database from command line

We need to import database on local machine when setting up a live website locally or when we are transferring hosting from one service provider to another. Let’s see how we can import MySQL database dump from command line.

Assuming that we have created the database say ‘mynewdb’ in which we have to import.

Run the following command

$ mysql -u username -ppassword mynewdb < dbbackupfile.sql

As a security measure you should avoid specifying the password with the command, just add the ‘-p’ option

$ mysql -u username -p mynewdb < dbbackupfile.sql

You will be prompted to enter the password when you run the above command.

If you have a compressed database backup file (.gz file) then you can import it as shown below:

$ gunzip < dbbackupfile.sql.gz | mysql -u username -p mynewdb

Leave a Comment

Back to top