MySQL: How do I import individual table dump files in to MySQL using shell script?

After I wrote the post: How do I dump all tables in a database into separate files? I got emails from couple people asking how to import the individual table files back in to MySQL. First way to import each sql file created by the post is to import each file individually by typing:mysql db_name < table1.sql This will work as long as you are only importing few files. But if you need to import all of the files in the directory, which could be in 100’s, this method does not scale well. To achieve this I wrote a shell script which does the work for me. Of course, there are other ways to do this and I am only showing you one way of doing it. This works for me so here it is:

if [ "$db" = "" ]; then
echo "Usage: $0 db_name"
exit 1
mkdir done
for sql_file in *.sql; do
echo "Importing $sql_file";
mysql $db< $sql_file;
mv $sql_file done;

Related posts:

DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.

9 thoughts on “MySQL: How do I import individual table dump files in to MySQL using shell script?

  1. CrazyToon Post author

    Initially, that is what I did. But somewhere along the line, my import failed. I had no idea where it failed and had to start importing from the beginning. Using the script gives me status and makes it easy for me continue where ever mysql import breaks/stops.

  2. Freelancenow

    Is there any way to dumping selected rows only from a table.
    I have a table with 70000 rows, and I want to dump about 6000 rows which meets certain criteria.

    it should be some thing like where in
    field = x1
    field = x3
    field = x4

    I just do not know whole syntax.


  3. Pingback: MySQL: How do you set up master/slave replication in MySQL? (CentOS, RHEL, Fedora) | Technology: Learn and Share

  4. Pingback: 12 Days of Jesus Junk – Day 3 – Avoid Wipeouts | Heal Your Church WebSite

  5. kalmesh


    Please provide the solution:

    I have 2 database in SQL server(VISH & KALM)

    VISH database is having 100 tables & KALM database is 10 tables.
    Now, i need to import all tables of KALM database to VISH database

    Note:VISH & KALM database is having simmilar tables.

Leave a Reply

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