MySQL: Restore a DB From the Command Line

This is a pretty basic tip today, but I noticed someone going through awkward measures to re-import a database over an existing database the other day. I thought I'd just make a note of how you can restore a db in a couple commands.

Scenerio: You have a development environment setup and you want to restore a fresh copy of the production MySQL database.

- The database on the development system is named: "mydb"

- You are using the command line in a directory that has the sql file you want to install: "mydb.sql"

First, you can use the MySQL command with the -e flag to run 2 MySQL commands to drop and create the database. If there's any chance you may need to revert back to the current development copy of the db, make sure to backup before you run this command!

$ mysql -uroot -p -e "drop database mydb;create database mydb;"

Now you have a fresh database you can import to.

$ mysql -uroot -p mydb < mydb.sql