简体   繁体   English

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

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

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.我 go 到PHPMyAdmin ,访问数据库,点击import , select 一个文件并导入。 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.我正在使用WAMP ,我想要一个不需要在我的计算机上安装其他程序的解决方案。

In Windows , open a terminal, go to the content folder and write: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 and macOS , as @BlackCharly pointed out, this will do the trick:LinuxmacOS 中,正如@BlackCharly 所指出的,这可以解决问题:

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): A) 将结果放在一个单独的目录中以确保安全(谢谢@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. B)将它们连接到具有不同扩展名的文件中,然后更改名称。 (Thanks @William Turrell) (感谢@William Turrell)

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

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

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

  1. Goto cmd转到 cmd

  2. Type in command prompt C:\\users\\Usersname>cd [.sql tables folder path ]键入命令提示符 C:\\users\\Usersname>cd [.sql 表文件夹路径]
    Press Enter按 Enter
    Ex: C:\\users\\Usersname>cd E:\\project\\database例如: C:\\users\\Usersname>cd E:\\project\\database

  3. Type command prompt键入命令提示符
    C:\\users\\Usersname>[.sql folder's drive (directory)name] C:\\users\\Usersname>[.sql 文件夹的驱动器(目录)名称]
    Press Enter按 Enter
    Ex: C:\\users\\Usersname>E:例如: C:\\users\\Usersname>E:

  4. Type command prompt for marge all .sql file(table) in a single file在单个文件中为所有 .sql 文件(表)输入命令提示符
    copy /b *.sql newdatabase.sql复制 /b *.sql newdatabase.sql
    Press Enter按 Enter
    EX: E:\\project\\database>copy /b *.sql newdatabase.sql例如: 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您可以在目录文件夹中看到 Merge Multiple .sql(file) tables Files Into A Single File
    Ex: E:\\project\\database例如: E:\\project\\database

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.这是 Windows 中的一个命令行,它将从一个文件夹中导入多个 SQL 文件。 You run this from the command line while in the directory where mysql.exe is located.您可以在 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)。

just type:只需输入:

cat *.sql |mysql -uroot -p

and mysql will import all the sql file in sequence并且mysql会依次导入所有的sql文件

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;源文件1.sql; source file2;源文件2; source file3;源文件3;

You could also a for loop to do so:您也可以使用 for 循环来执行此操作:

#!/bin/bash
for i in *.sql
do
    echo "Importing: $i"
    mysql your_db_name < $i
    wait
done 

Source 来源

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) 与数据库名称。

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 文件管理一些导入标记)。

只需在命令提示符下键入以下命令,它将所有 sql 文件绑定到单个 sql 文件中,

c:/xampp/mysql/bin/sql/ type *.sql > OneFile.sql;

在 windows 中打开 windows powershell 并转到 sql 文件所在的文件夹,然后运行此命令

cat *.sql |  C:\xampp\mysql\bin\mysql.exe -u username -p databasename

Import From multiple SQL files into one Database.<\/strong>从多个 SQL 文件导入到一个数据库。<\/strong><\/h1>

Step 1<\/strong> : Goto to the folder and create file 'import-script.sh' with execute permission (give Permission to file is chmod u+x import-script.sh<\/code> )第 1 步<\/strong>:转到文件夹并创建具有执行权限的文件“import-script.sh”(授予文件权限为chmod u+x import-script.sh<\/code> )

 #!\/bin\/bash for i in *.sql do echo "Importing: $i" mysql -u USERNAME -pPASSWORD DBNAME < $i wait done<\/code><\/pre>

The main thing is -p and PASSWORD didn't add any space.<\/strong><\/em>最主要的是 -p 和 PASSWORD 没有添加任何空格。<\/strong><\/em>

Step 2:<\/strong> then in your terminal run this command .\/import-script.sh<\/code>第 2 步:<\/strong>然后在您的终端中运行此命令.\/import-script.sh<\/code>

"

for windows users,对于windows用户,

You can select the database in the phpMyadmin interface on the left, drag and drop all your files from your windows folder onto the web UI of phpMyadmin . You can select the database in the phpMyadmin interface on the left, drag and drop all your files from your windows folder onto the web UI of phpMyadmin .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM