且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何轻松地将多个sql文件导入MySQL数据库?

更新时间:2022-10-17 23:19:55

Windows 中,打开一个终端,转到内容文件夹并输入:

copy /b *.sql all_files.sql

这仅将所有文件连接在一起,因此可以非常快速地使用PhpMyAdmin导入.

正如@BlackCharly指出的,在 Linux macOS 中,这将达到目的:
cat *.sql  > .all_files.sql

重要提示:直接执行该操作应该会顺利进行,但由于系统将文件添加到自身中,因此最终可能会陷入一个循环,一个庞大的输出文件会变得越来越大.为了避免这种情况,有两种可能的解决方案.

A)将结果放在一个单独的目录中以确保安全(感谢@mosh):

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

B)将它们连接到具有不同扩展名的文件中,然后将其更改为名称. (感谢@William Turrell)

cat *.sql  > all_files.sql1
mv all_files.sql1 all_files.sql

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.

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