Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import multiple database at once through SSH?

Tags:

sql

mysql

ssh

I am attempting to restore my mysql database to my website, and all of the tables in my database get dropped into individual files, so I am trying to figure out how can I restore all of the database .sql files through SSH with a single (or easy command) instead of restoring all 100 tables individually.

like image 846
zeropsi Avatar asked May 07 '26 18:05

zeropsi


2 Answers

cat *.sql > data.sql
mysql -u <username> -p < data.sql
like image 82
scibuff Avatar answered May 10 '26 09:05

scibuff


It depends on how you created the individual files - if they have all the instructions for recreating the tables (i.e., "Drop if exists...", "Create ...", and "Insert into ..."), then you can either concatenate them into mysql:

cat *.sql | mysql -u xxx -pxxx dbname

or write a script to do it

#!/bin/sh
mysql -u xxx -pxxx dbname < file001.sql
mysql -u xxx -pxxx dbname < file002.sql

The second choice lets you more easily control the order of files processed.

Finally, you might want to create your backups in a more convenient way - check out mysqldump for how to dump a database (or several!) into one file (basically, "mysqldump -u xxx -pxxx dbname > dbname.sql", but there are some helpful flags you might want to add).

like image 42
D Mac Avatar answered May 10 '26 08:05

D Mac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!