Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to easily import multiple sql files into a MySQL database?

Tags:

import

mysql

I have several sql files and I want to import all of them at once into a MySQL database.

I go to PHPMyAdmin, access the database, click import, select a file and import it. When I have more than a couple of files it takes a long time.

I would like to know if there is a better way to import multiple files, something like one file which will import the other files or similar.

I'm using WAMP and I would like a solution that does not require installing additional programs on my computer.

like image 217
Federico J. Avatar asked Jan 25 '12 09:01

Federico J.


People also ask

How do I import multiple SQL files?

Import Multiple SQL files to Database by using Data Import/Restore in MySQL Workbench is great feature when you need to import multiple SQL files into MySQL Server. If you have created multiple files by using Data Export feature in MySQL Server, SQL Dump file project can be imported by using MySQL Import/Restore.

How do I import SQL file into another SQL file?

Open SQL Server Management Studio. Connect to an instance of the SQL Server Database Engine or localhost. Expand Databases, right-click a database (test in the example below), point to Tasks, and click Import Flat File above Import Data.


2 Answers

In Windows, open a terminal, go to the content folder and write:

copy /b *.sql all_files.sql 

This concate all files in only one, making it really quick to import with PhpMyAdmin.

In Linux and macOS, as @BlackCharly pointed out, this will do the trick:

cat *.sql  > .all_files.sql 

Important Note: Doing it directly should go well, but it could end up with you stuck in a loop with a massive output file getting bigger and bigger due to the system adding the file to itself. To avoid it, two possible solutions.

A) Put the result in a separate directory to be safe (Thanks @mosh):

mkdir concatSql cat *.sql  > ./concatSql/all_files.sql 

B) Concat them in a file with a different extension and then change it the name. (Thanks @William Turrell)

cat *.sql  > all_files.sql1 mv all_files.sql1 all_files.sql 
like image 161
Federico J. Avatar answered Sep 29 '22 01:09

Federico J.


This is the easiest way that I have found.

In Windows (powershell):

cat *.sql | C:\wamp64\bin\mysql\mysql5.7.21\bin\mysql.exe -u user -p database

You will need to insert the path to your WAMP - MySQL above, I have used my systems path.

In Linux (Bash):

cat *.sql | mysql -u user -p database

like image 21
StanleyD Avatar answered Sep 29 '22 01:09

StanleyD