Importing a SQL file directly from Command Line for MySQL

October 18th, 2011 by rvdavid Leave a reply »

I am writing this on the train, so I’ll be relatively quick.

It’s been a while since I wrote my import sql file via command line post and thought I’d post the updated version which I now use.

It is possible to execute an SQL file without logging into the mysql client first. Previously, I used login to mysql and use the source command to execute commands from an SQL text file.

Now, I execute it all straight from bash or windows shell without having to login to the MySQL client.

To import an SQL file by passing it as if it were an arguement, you need to use the < sign. So for example, say that I wanted to import an external database from production and wanted to import it into my development environment, I can do so by doing the following:

How to Import an SQL file directly from the Command Line:

Step 1: Download the source sql file.

Step 2: Extract the SQL file sif required – usually,  you’ll get the file in an archived form since an uncompressed SQL file is hideously large.

Step 3: Execute the following in the command line:

$ mysql -uusername -p -h databasehostname databasename < locally-extracted-sql-file.sql; 

Step 4: Enter your password at the prompt.

Step 5: Check that your changes have been applied and make adjustments as required.

You should now see that the sql commands in the external file has been executed.

if you enjoyed this post, make sure you subscribe to my RSS feed!
You can also follow me on Twitter here.

Related posts:

  1. Importing SQL dumps into MySQL through Command line
  2. MySQL Funny/Strange Characters replacing apostrophes after import
  3. Execute Linux commands in PHP by using backticks
  4. How to fix phpmyadmin error: “Connection for controluser as defined in your configuration failed”


  1. Sammuy says:

    This is like general knowledge.

  2. Devin Walker says:

    Thanks this was super quick and easy. I can’t remember the exact commands so this is a great reference. Sammuy is a troll, forget him.

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.