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

时间:2021-12-12 02:47:05

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

我有几个sql文件,我想一次将所有这些文件导入MySQL数据库。

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.

我转到PHPMyAdmin,访问数据库,单击导入,选择一个文件并导入它。当我有多个文件时,需要很长时间。

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.

我正在使用WAMP,我想要一个不需要在我的计算机上安装其他程序的解决方案。

7 个解决方案

#1


73  

Thanks BlackCharly, I found how to do it in Windows, open a terminal go to content folder and write:

谢谢BlackCharly,我发现如何在Windows中执行此操作,打开终端转到内容文件夹并写入:

copy /b *.sql all_files.sql

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

这样只会将所有文件连接起来,使用PhpMyAdmin快速导入它

In linux, as BlackCharly says:

在Linux中,正如BlackCharly所说:

cat *.sql  > all_files.sql

Last but not least, please, take into account the @mosh comment:

最后但同样重要的是,请考虑@mosh评论:

If all_files.sql is already present, this can result in recursive copy of the file on itself with no end! Put the result in a separate directory to be safe.

如果all_files.sql已经存在,这可能导致文件的递归副本本身没有结束!将结果放在单独的目录中是安全的。

#2


36  

This is the easiest way that I have found.

这是我找到的最简单的方法。

In Windows (powershell):

在Windows(powershell)中:

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

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.

您将需要插入WAMP的路径 - 上面的MySQL,我已经使用了我的系统路径。

In Linux (Bash):

在Linux(Bash)中:

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

cat * .sql | mysql -u user -p数据库

#3


9  

  1. Goto cmd

    转到cmd

  2. Type in command prompt C:\users\Usersname>cd [.sql tables folder path ]
    Press Enter
    Ex: C:\users\Usersname>cd E:\project\database

    键入命令提示符C:\ users \ Usersname> cd [.sql tables folder path]按Enter Ex:C:\ users \ Usersname> cd E:\ project \ database

  3. Type command prompt
    C:\users\Usersname>[.sql folder's drive (directory)name]
    Press Enter
    Ex: C:\users\Usersname>E:

    键入命令提示符C:\ users \ Usersname> [.sql文件夹的驱动器(目录)名称]按Enter Ex:C:\ users \ Usersname> E:

  4. Type command prompt for marge all .sql file(table) in a single file
    copy /b *.sql newdatabase.sql
    Press Enter
    EX: E:\project\database>copy /b *.sql newdatabase.sql

    在单个文件中输入marge all .sql文件(表)的命令提示符/ b * .sql newdatabase.sql按Enter键输入EX:E:\ project \ database> copy / b * .sql newdatabase.sql

  5. You can see Merge Multiple .sql(file) tables Files Into A Single File in your directory folder
    Ex: E:\project\database

    您可以在目录文件夹中看到合并多个.sql(文件)表文件到单个文件Ex:E:\ project \ database

#4


5  

I know it's been a little over two years... but I was looking for a way to do this, and wasn't overly happy with the solution posted (it works fine, but I wanted a little more information as the import happens). When combining all the SQL files in to one, you don't get any sort of progress updates.

我知道这已经有两年多了......但是我一直在寻找一种方法来做到这一点,并且对于发布的解决方案并不过分满意(它工作正常,但我想要更多信息,因为导入发生) 。将所有SQL文件合并为一个时,您不会获得任何类型的进度更新。

So I kept digging for an answer and thought this might be a good place to post what I found for future people looking for the same answer. Here's a command line in Windows that will import multiple SQL files from a folder. You run this from the command line while in the directory where mysql.exe is located.

所以我一直在寻找答案,并认为这可能是一个发布我为未来寻找相同答案的人发现的好地方。这是Windows中的一个命令行,它将从文件夹中导入多个SQL文件。您可以在mysql.exe所在的目录中从命令行运行此命令。

for /f %f in ('dir /b <dir>\<mask>') do mysql --user=<user> --password=<password> <dbname> < <dir>\%f

With some assumed values (as an example):

使用一些假设值(作为示例):

for /f %f in ('dir /b c:\sqlbackup\*.sql') do mysql --user=mylogin --password=mypass mydb < c:\sqlbackup\%f

If you had two sets of SQL backups in the folder, you could change the *.sql to something more specific (like mydb_*.sql).

如果文件夹中有两组SQL备份,则可以将* .sql更改为更具体的内容(如mydb _ * .sql)。

#5


4  

Enter the mysql shell like this.

像这样进入mysql shell。

mysql --host=localhost --user=username --password --database=db

mysql --host = localhost --user = username --password --database = db

Then use the source command and a semicolon to seperate the commands.

然后使用source命令和分号分隔命令。

source file1.sql; source file2; source file3;

source file1.sql;源文件2;源文件3;

#6


1  

Save this file as .bat and run it , change variables inside parenthesis ...

将此文件另存为.bat并运行它,更改括号内的变量...

@echo off
title Mysql Import Script
cd (Folder Name)
 for %%a in (*) do (
     echo Importing File  : %%a 
     mysql -u(username) -p(password)  %%~na < %%a
)
pause

if it's only one database modify (%%~na) with the database name .

如果它只有一个数据库修改(%% ~na)与数据库名称。

#7


0  

The easiest solution is to copy/paste every sql files in one.

最简单的解决方案是将每个sql文件复制/粘贴到一个文件中。

You can't add some sql markup for file importation (the imported files will be in your computer, not in the server, and I don't think MySQL manage some import markup for external sql files).

你不能为文件导入添加一些sql标记(导入的文件将在你的计算机中,而不是在服务器中,我不认为MySQL管理外部sql文件的一些导入标记)。

#1


73  

Thanks BlackCharly, I found how to do it in Windows, open a terminal go to content folder and write:

谢谢BlackCharly,我发现如何在Windows中执行此操作,打开终端转到内容文件夹并写入:

copy /b *.sql all_files.sql

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

这样只会将所有文件连接起来,使用PhpMyAdmin快速导入它

In linux, as BlackCharly says:

在Linux中,正如BlackCharly所说:

cat *.sql  > all_files.sql

Last but not least, please, take into account the @mosh comment:

最后但同样重要的是,请考虑@mosh评论:

If all_files.sql is already present, this can result in recursive copy of the file on itself with no end! Put the result in a separate directory to be safe.

如果all_files.sql已经存在,这可能导致文件的递归副本本身没有结束!将结果放在单独的目录中是安全的。

#2


36  

This is the easiest way that I have found.

这是我找到的最简单的方法。

In Windows (powershell):

在Windows(powershell)中:

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

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.

您将需要插入WAMP的路径 - 上面的MySQL,我已经使用了我的系统路径。

In Linux (Bash):

在Linux(Bash)中:

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

cat * .sql | mysql -u user -p数据库

#3


9  

  1. Goto cmd

    转到cmd

  2. Type in command prompt C:\users\Usersname>cd [.sql tables folder path ]
    Press Enter
    Ex: C:\users\Usersname>cd E:\project\database

    键入命令提示符C:\ users \ Usersname> cd [.sql tables folder path]按Enter Ex:C:\ users \ Usersname> cd E:\ project \ database

  3. Type command prompt
    C:\users\Usersname>[.sql folder's drive (directory)name]
    Press Enter
    Ex: C:\users\Usersname>E:

    键入命令提示符C:\ users \ Usersname> [.sql文件夹的驱动器(目录)名称]按Enter Ex:C:\ users \ Usersname> E:

  4. Type command prompt for marge all .sql file(table) in a single file
    copy /b *.sql newdatabase.sql
    Press Enter
    EX: E:\project\database>copy /b *.sql newdatabase.sql

    在单个文件中输入marge all .sql文件(表)的命令提示符/ b * .sql newdatabase.sql按Enter键输入EX:E:\ project \ database> copy / b * .sql newdatabase.sql

  5. You can see Merge Multiple .sql(file) tables Files Into A Single File in your directory folder
    Ex: E:\project\database

    您可以在目录文件夹中看到合并多个.sql(文件)表文件到单个文件Ex:E:\ project \ database

#4


5  

I know it's been a little over two years... but I was looking for a way to do this, and wasn't overly happy with the solution posted (it works fine, but I wanted a little more information as the import happens). When combining all the SQL files in to one, you don't get any sort of progress updates.

我知道这已经有两年多了......但是我一直在寻找一种方法来做到这一点,并且对于发布的解决方案并不过分满意(它工作正常,但我想要更多信息,因为导入发生) 。将所有SQL文件合并为一个时,您不会获得任何类型的进度更新。

So I kept digging for an answer and thought this might be a good place to post what I found for future people looking for the same answer. Here's a command line in Windows that will import multiple SQL files from a folder. You run this from the command line while in the directory where mysql.exe is located.

所以我一直在寻找答案,并认为这可能是一个发布我为未来寻找相同答案的人发现的好地方。这是Windows中的一个命令行,它将从文件夹中导入多个SQL文件。您可以在mysql.exe所在的目录中从命令行运行此命令。

for /f %f in ('dir /b <dir>\<mask>') do mysql --user=<user> --password=<password> <dbname> < <dir>\%f

With some assumed values (as an example):

使用一些假设值(作为示例):

for /f %f in ('dir /b c:\sqlbackup\*.sql') do mysql --user=mylogin --password=mypass mydb < c:\sqlbackup\%f

If you had two sets of SQL backups in the folder, you could change the *.sql to something more specific (like mydb_*.sql).

如果文件夹中有两组SQL备份,则可以将* .sql更改为更具体的内容(如mydb _ * .sql)。

#5


4  

Enter the mysql shell like this.

像这样进入mysql shell。

mysql --host=localhost --user=username --password --database=db

mysql --host = localhost --user = username --password --database = db

Then use the source command and a semicolon to seperate the commands.

然后使用source命令和分号分隔命令。

source file1.sql; source file2; source file3;

source file1.sql;源文件2;源文件3;

#6


1  

Save this file as .bat and run it , change variables inside parenthesis ...

将此文件另存为.bat并运行它,更改括号内的变量...

@echo off
title Mysql Import Script
cd (Folder Name)
 for %%a in (*) do (
     echo Importing File  : %%a 
     mysql -u(username) -p(password)  %%~na < %%a
)
pause

if it's only one database modify (%%~na) with the database name .

如果它只有一个数据库修改(%% ~na)与数据库名称。

#7


0  

The easiest solution is to copy/paste every sql files in one.

最简单的解决方案是将每个sql文件复制/粘贴到一个文件中。

You can't add some sql markup for file importation (the imported files will be in your computer, not in the server, and I don't think MySQL manage some import markup for external sql files).

你不能为文件导入添加一些sql标记(导入的文件将在你的计算机中,而不是在服务器中,我不认为MySQL管理外部sql文件的一些导入标记)。