如何在MySQL中运行SQL脚本?

发布于 2024-12-28 07:27:00 字数 250 浏览 3 评论 0原文

我想在 MySQL 中执行一个包含 SQL 查询的文本文件。

我尝试运行 source /Desktop/test.sql 并收到错误:

mysql> 。 \home\sivakumar\Desktop\test.sql 错误:无法打开文件 '\home\sivakumar\Desktop\test.sql',错误:2

你知道我做错了什么吗?

I want to execute a text file containing SQL queries, in MySQL.

I tried to run source /Desktop/test.sql and received the error:

mysql> . \home\sivakumar\Desktop\test.sql ERROR: Failed to open file
'\home\sivakumar\Desktop\test.sql', error: 2

Any idea on what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(23

明媚殇 2025-01-04 07:27:00

如果您使用 MySQL 命令行 mysql>,则必须将 SQL 文件声明为 source

mysql> source \home\user\Desktop\test.sql;

If you’re at the MySQL command line mysql> you have to declare the SQL file as source.

mysql> source \home\user\Desktop\test.sql;
半边脸i 2025-01-04 07:27:00

您有很多选择:

  • 使用 MySQL 命令行客户端: mysql -h 主机名 -u 用户数据库 path/to/test.sql
  • 安装 MySQL GUI 工具并打开 SQL 文件,然后执行它
  • 如果数据库可通过您的网络服务器获得,请使用 phpmysql

You have quite a lot of options:

  • use the MySQL command line client: mysql -h hostname -u user database < path/to/test.sql
  • Install the MySQL GUI tools and open your SQL file, then execute it
  • Use phpmysql if the database is available via your webserver
泛滥成性 2025-01-04 07:27:00

您可以使用以下命令执行已写入文本文件中的 mysql 语句:

mysql -u yourusername -p yourpassword yourdatabase < text_file

如果您的数据库尚未创建,请首先使用以下命令登录您的 mysql:

mysql -u yourusername -p yourpassword

then:

mysql>CREATE DATABASE a_new_database_name

then:

mysql -u yourusername -p yourpassword a_new_database_name < text_file

应该可以了!

更多信息在这里: http://dev.mysql.com /doc/refman/5.0/en/mysql-batch-commands.html

you can execute mysql statements that have been written in a text file using the following command:

mysql -u yourusername -p yourpassword yourdatabase < text_file

if your database has not been created yet, log into your mysql first using:

mysql -u yourusername -p yourpassword

then:

mysql>CREATE DATABASE a_new_database_name

then:

mysql -u yourusername -p yourpassword a_new_database_name < text_file

that should do it!

More info here: http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.html

撩动你心 2025-01-04 07:27:00

我最喜欢的选择是:

 mysql --user="username" --database="databasename" --password="yourpassword" < "filepath"

我以这种方式使用它,因为当您用“”将其串起来时,您可以避免错误的路径和空格错误,并且可能会避免更多我没有遇到过的字符问题。


对于 @elcuco 评论,我建议在之前使用此命令与 [space] ,这样它就会告诉 bash 忽略将其保存在历史记录中,这在大多数 bash 中都是开箱即用的。

如果它仍在历史记录中保存您的命令,请查看以下解决方案:

执行命令而不将其保留在历史记录中


额外的安全编辑

以防万一您想要更加安全,您可以使用以下命令并在命令行输入中输入密码:

mysql --user="username" --database="databasename" -p < "filepath"

My favorite option to do that will be:

 mysql --user="username" --database="databasename" --password="yourpassword" < "filepath"

I use it this way because when you string it with "" you avoiding wrong path and mistakes with spaces and - and probably more problems with chars that I did not encounter with.


With @elcuco comment I suggest using this command with [space] before so it tell bash to ignore saving it in history, this will work out of the box in most bash.

in case it still saving your command in history please view the following solutions:

Execute command without keeping it in history


extra security edit

Just in case you want to be extra safe you can use the following command and enter the password in the command line input:

mysql --user="username" --database="databasename" -p < "filepath"
我一向站在原地 2025-01-04 07:27:00

所有热门答案都很好。但万一有人想从远程服务器上的文本文件运行查询将结果保存到文件中(而不是显示在控制台上),您可以这样做:

mysql -u yourusername -p yourpassword yourdatabase < query_file > results_file

希望这对某人有帮助。

All the top answers are good. But just in case someone wants to run the query from a text file on a remote server AND save results to a file (instead of showing on console), you can do this:

mysql -u yourusername -p yourpassword yourdatabase < query_file > results_file

Hope this helps someone.

晨光如昨 2025-01-04 07:27:00

我来这里也是为了寻找这个答案,这是我发现最适合我的: 注意我正在使用 Ubuntu 16.xx

  1. 使用以下命令访问 mysql:

mysql -u; -p

  1. 在 mysql 提示符下,输入:

source file_name.sql

希望这会有所帮助。

I came here searching for this answer as well, and here is what I found works the best for me: Note I am using Ubuntu 16.x.x

  1. Access mysql using:

mysql -u <your_user> - p

  1. At the mysql prompt, enter:

source file_name.sql

Hope this helps.

染火枫林 2025-01-04 07:27:00

将 .sql 文件的路径指定为:

source c:/dump/SQL/file_name.sql;

See In The Image:

Give the path of .sql file as:

source c:/dump/SQL/file_name.sql;

See In The Image:

浅笑依然 2025-01-04 07:27:00

我会执行以下操作而不是重定向

mysql -h <hostname> -u <username> --password=<password> -D <database> -e 'source <path-to-sql-file>'

这将执行文件 path-to-sql-file

instead of redirection I would do the following

mysql -h <hostname> -u <username> --password=<password> -D <database> -e 'source <path-to-sql-file>'

This will execute the file path-to-sql-file

旧时光的容颜 2025-01-04 07:27:00
mysql> source C:\Users\admin\Desktop\fn_Split.sql

不要指定单引号。

如果上述命令不起作用,请将文件复制到 c: 驱动器,然后重试。
如下图所示,

mysql> source C:\fn_Split.sql
mysql> source C:\Users\admin\Desktop\fn_Split.sql

Do not specify single quotes.

If the above command is not working, copy the file to c: drive and try again.
as shown below,

mysql> source C:\fn_Split.sql
梦屿孤独相伴 2025-01-04 07:27:00

mysql -u 你的用户名数据库名<文件.sql -p

mysql -u your-username database-name < file.sql -p

沐歌 2025-01-04 07:27:00

直接从命令行传递密码参数永远不是一个好习惯,它保存在 ~/.bash_history 文件中,并且可以从其他应用程序访问。

使用这个代替:

mysql -u user --host host --port 9999 database_name < /scripts/script.sql -p
Enter password:

Never is a good practice to pass the password argument directly from the command line, it is saved in the ~/.bash_history file and can be accessible from other applications.

Use this instead:

mysql -u user --host host --port 9999 database_name < /scripts/script.sql -p
Enter password:
烟─花易冷 2025-01-04 07:27:00

有很多方法可以做到这一点。

From Workbench: File > Run SQL Script -- then follow prompts

From Windows Command Line:
   Option 1: mysql -u usr -p
             mysql> source file_path.sql
   Option 2: mysql -u usr -p '-e source file_path.sql'
   Option 3: mysql -u usr -p < file_path.sql
   Option 4: put multiple 'source' statements inside of file_path.sql (I do this to drop and recreate schemas/databases which requires multiple files to be run)
             mysql -u usr -p < file_path.sql

如果您从命令行收到错误,请确保您之前已经运行过。

cd {!!>>mysqld.exe home directory here<<!!}
mysqld.exe --initialize 

这必须从 mysqld.exe 目录(即 CD)中运行。

希望这对您有所帮助,而不仅仅是多余。

So many ways to do it.

From Workbench: File > Run SQL Script -- then follow prompts

From Windows Command Line:
   Option 1: mysql -u usr -p
             mysql> source file_path.sql
   Option 2: mysql -u usr -p '-e source file_path.sql'
   Option 3: mysql -u usr -p < file_path.sql
   Option 4: put multiple 'source' statements inside of file_path.sql (I do this to drop and recreate schemas/databases which requires multiple files to be run)
             mysql -u usr -p < file_path.sql

If you get errors from the command line, make sure you have previously run

cd {!!>>mysqld.exe home directory here<<!!}
mysqld.exe --initialize 

This must be run from within the mysqld.exe directory, hence the CD.

Hope this is helpful and not just redundant.

氛圍 2025-01-04 07:27:00

从linux 14.04到MySql 5.7,使用cat命令通过管道连接mysql登录:

cat /Desktop/test.sql | sudo mysql -uroot -p 

您可以使用此方法直接从Shell执行许多MySQL命令。例如:

echo "USE my_db; SHOW tables;" | sudo mysql -uroot -p 

确保用分号(';')分隔命令。

我在上面的答案中没有看到这种方法,并认为这是一个很好的贡献。

From linux 14.04 to MySql 5.7, using cat command piped with mysql login:

cat /Desktop/test.sql | sudo mysql -uroot -p 

You can use this method for many MySQL commands to execute directly from Shell. Eg:

echo "USE my_db; SHOW tables;" | sudo mysql -uroot -p 

Make sure you separate your commands with semicolon (';').

I didn't see this approach in the answers above and thought it is a good contribution.

只是一片海 2025-01-04 07:27:00

很可能,您只需要更改斜杠/黑斜杠:

 \home\sivakumar\Desktop\test.sql

 /home/sivakumar/Desktop/test.sql

所以命令是:

source /home/sivakumar/Desktop/test.sql

Very likely, you just need to change the slash/blackslash:
from

 \home\sivakumar\Desktop\test.sql

to

 /home/sivakumar/Desktop/test.sql

So the command would be:

source /home/sivakumar/Desktop/test.sql
和我恋爱吧 2025-01-04 07:27:00

使用 mysql 命令提示符中的以下内容 -

source \\home\\user\\Desktop\\test.sql;

不使用引号。即使路径包含空格(' '),也完全不使用引号

use the following from mysql command prompt-

source \\home\\user\\Desktop\\test.sql;

Use no quotation. Even if the path contains space(' ') use no quotation at all.

只为一人 2025-01-04 07:27:00

如果你正在尝试这个命令:

mysql -u root -proot -D database < /path/to/script.sql

你可能会得到这样的错误:如果你有特殊字符,主要是“`”

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/path/to/script.sql' at line 1

所以我建议使用这样的命令:

echo "source /path/to/script.sql" | mysql -u root -proot -D database
# OR
mysql -u root -proot -D database -e "source /path/to/script.sql"

这个命令将执行source /path/to/script.sql 一旦连接到服务器,就会执行您的脚本。

If you are trying this command :

mysql -u root -proot -D database < /path/to/script.sql

You may get an error like this : if you have special characters, mainly '`'

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/path/to/script.sql' at line 1

So I would suggest to use a command like this :

echo "source /path/to/script.sql" | mysql -u root -proot -D database
# OR
mysql -u root -proot -D database -e "source /path/to/script.sql"

This command will execute source /path/to/script.sql once connected to the server, which execute your script.

别闹i 2025-01-04 07:27:00

由于 mysql -u yourusername -p yourpassword yourdatabase text_file 在远程服务器(亚马逊的 EC2)上不起作用...

确保首先创建数据库。

然后:

mysql --host=localhost --user=your_username --password=your_password your_database_name < pathTofilename.sql

Since mysql -u yourusername -p yourpassword yourdatabase < text_file did not work on a remote server (Amazon's EC2)...

Make sure that the Database is created first.

Then:

mysql --host=localhost --user=your_username --password=your_password your_database_name < pathTofilename.sql
末が日狂欢 2025-01-04 07:27:00

为了将来的参考,我发现这比上述方法更有效,在 Windows 下的 msql 控制台中:

mysql>source c://path_to_file//path_to_file//file_name.sql;

If您的根驱动器不称为“c”,那么只需与您的驱动器名称互换即可。首先尝试反斜杠,如果不起作用,请尝试正斜杠。如果它们也不起作用,请确保您有完整的文件路径、文件名的 .sql 扩展名,如果您的版本坚持使用分号,请确保它在那里并重试。

For future reference, I've found this to work vs the aforementioned methods, under Windows in your msql console:

mysql>>source c://path_to_file//path_to_file//file_name.sql;

If your root drive isn't called "c" then just interchange with what your drive is called. First try backslashes, if they dont work, try the forward slash. If they also don't work, ensure you have your full file path, the .sql extension on the file name, and if your version insists on semi-colons, ensure it's there and try again.

满天都是小星星 2025-01-04 07:27:00

在powershell中,当使用>时C:\backup\backup.sql 我们不断得到

The '<' operator is reserved for future use.

因此,如果其他人在 Windows 机器上使用 cmd 或 powershell 对此进行攻击,这会为我们完成。

.\mysql.exe -u root -p -D db_prod -e "source C:\\backup\\yesterday.sql" 

In powershell, when using > C:\backup\backup.sql we kept getting

The '<' operator is reserved for future use.

So if someone else is thrashing on this on a windows box and using cmd or powershell, this did it for us.

.\mysql.exe -u root -p -D db_prod -e "source C:\\backup\\yesterday.sql" 
左秋 2025-01-04 07:27:00

在您的终端中按照以下步骤操作。请注意,将 my_usermy_databasefile_name 替换为您的详细信息。

mysql -u my_user -p

use my_database;

source file_name.sql

In your terminal follow the steps below. Note replace my_user, my_database and file_name with your details.

mysql -u my_user -p

use my_database;

source file_name.sql
木落 2025-01-04 07:27:00

如果您在这里寻找 DRUPAL 环境

您可以在项目目录中使用 drush 命令运行

drush sqlc

If you are here LOOKING FOR A DRUPAL ENVIRONMENT

You can run with drush command on your project directory

drush sqlc
っ〆星空下的拥抱 2025-01-04 07:27:00

我遇到了这个错误,并尝试了所有我能得到的建议都无济于事。

最后,问题是我的文件夹的文件夹名称中有一个空格,在文件夹路径中显示为正斜杠,一旦我找到并删除它,它就可以正常工作。

I had this error, and tried all the advice i could get to no avail.

Finally, the problem was that my folder had a space in the folder name which appearing as a forward-slash in the folder path, once i found and removed it, it worked fine.

深居我梦 2025-01-04 07:27:00

我使用 Bash 的 Here Strings 来即时执行 SQL:

mysql -uroot -p <<<"select date(now())"

https ://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Strings

I use Bash's Here Strings for an instant SQL execution:

mysql -uroot -p <<<"select date(now())"

https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Strings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文