Import/Restore MySQL Files From the Command Line

A note from A Beautiful Site’s founder: We developed Surreal to be easy for you and your clients. If you’re a web designer, you should take a look at the simple, hosted CMS that’s changing the way content is managed. Surreal integrates in moments and is trusted by over 18,000 websites. Try it out for free and let us know what you think! Visit Website »

I don’t use this very often, but when I do I always spend a decent amount of time trying to figure it out online. Here are the steps to import a large SQL file from the command line.

  1. Upload the SQL file to the server that your MySQL database is on
  2. Execute the following command from the terminal (command line):
    mysql -u [username] -p [database name] < /path/to/file/data.sql
  3. Enter the appropriate MySQL password at the prompt

Since anything over 2MB won’t paste gracefully into a textarea, this works really well with large SQL files generated by phpMyAdmin. Remember to truncate data and/or drop tables where necessary before importing, otherwise the restore may not work as intended. (For example, CREATE TABLE will not work if the table already exists and INSERT statements will fail if a primary key already exists.)

If you enjoyed this article, please share it with a friend!

One Response to Import/Restore MySQL Files From the Command Line

  1. great solution for import many records into database
    thxs 4 u’r solution jeh …

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>