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.

12 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.

  6. balenciaga sneakers

    My husband and i ended up being now glad that Louis managed to round up his web research from the ideas he got while using the site. It is now and again perplexing just to choose to be giving away solutions which often people today could have been making money from. And now we see we have got the website owner to appreciate for that. The main explanations you’ve made, the straightforward site menu, the friendships you aid to foster – it is all fantastic, and it’s helping our son in addition to us imagine that the content is cool, which is incredibly important. Many thanks for the whole thing!

  7. links of london

    My husband and i were really satisfied when Albert managed to round up his investigation via the ideas he received out of the web page. It is now and again perplexing to simply be offering tips and hints some people have been making money from. And we all fully grasp we’ve got the blog owner to give thanks to for that. Those illustrations you made, the straightforward site menu, the relationships your site aid to create – it’s most terrific, and it’s leading our son in addition to our family feel that this subject matter is interesting, which is certainly unbelievably indispensable. Many thanks for the whole thing!

  8. totosite

    I have been looking for articles on these topics for a long time. totosite I don’t know how grateful you are for posting on this topic. Thank you for the numerous articles on this site, I will subscribe to those links in my bookmarks and visit them often. Have a nice day

Leave a Reply

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