如何在MySQL中运行SQL脚本?
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
如果您使用 MySQL 命令行
mysql>
,则必须将 SQL 文件声明为source
。If you’re at the MySQL command line
mysql>
you have to declare the SQL file assource
.您有很多选择:
path/to/test.sql
You have quite a lot of options:
mysql -h hostname -u user database < path/to/test.sql
您可以使用以下命令执行已写入文本文件中的 mysql 语句:
如果您的数据库尚未创建,请首先使用以下命令登录您的 mysql:
then:
then:
应该可以了!
更多信息在这里: 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:
if your database has not been created yet, log into your mysql first using:
then:
then:
that should do it!
More info here: http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.html
我最喜欢的选择是:
我以这种方式使用它,因为当您用“”将其串起来时,您可以避免错误的路径和空格错误,并且可能会避免更多我没有遇到过的字符问题。
对于 @elcuco 评论,我建议在之前使用此命令与 [space] ,这样它就会告诉 bash 忽略将其保存在历史记录中,这在大多数 bash 中都是开箱即用的。
如果它仍在历史记录中保存您的命令,请查看以下解决方案:
执行命令而不将其保留在历史记录中
额外的安全编辑
以防万一您想要更加安全,您可以使用以下命令并在命令行输入中输入密码:
My favorite option to do that will be:
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:
所有热门答案都很好。但万一有人想从远程服务器上的文本文件运行查询并将结果保存到文件中(而不是显示在控制台上),您可以这样做:
希望这对某人有帮助。
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:
Hope this helps someone.
我来这里也是为了寻找这个答案,这是我发现最适合我的: 注意我正在使用 Ubuntu 16.xx
mysql -u; -p
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
mysql -u <your_user> - p
source file_name.sql
Hope this helps.
将 .sql 文件的路径指定为:
Give the path of .sql file as:
我会执行以下操作而不是重定向
这将执行文件 path-to-sql-file
instead of redirection I would do the following
This will execute the file path-to-sql-file
不要指定单引号。
如果上述命令不起作用,请将文件复制到 c: 驱动器,然后重试。
如下图所示,
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 -u 你的用户名数据库名<文件.sql -p
mysql -u your-username database-name < file.sql -p
直接从命令行传递密码参数永远不是一个好习惯,它保存在
~/.bash_history
文件中,并且可以从其他应用程序访问。使用这个代替:
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:
有很多方法可以做到这一点。
如果您从命令行收到错误,请确保您之前已经运行过。
这必须从 mysqld.exe 目录(即 CD)中运行。
希望这对您有所帮助,而不仅仅是多余。
So many ways to do it.
If you get errors from the command line, make sure you have previously run
This must be run from within the mysqld.exe directory, hence the CD.
Hope this is helpful and not just redundant.
从linux 14.04到MySql 5.7,使用
cat
命令通过管道连接mysql登录:您可以使用此方法直接从Shell执行许多MySQL命令。例如:
确保用分号(';')分隔命令。
我在上面的答案中没有看到这种方法,并认为这是一个很好的贡献。
From linux 14.04 to MySql 5.7, using
cat
command piped with mysql login:You can use this method for many MySQL commands to execute directly from Shell. Eg:
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.
很可能,您只需要更改斜杠/黑斜杠:
从
到
所以命令是:
Very likely, you just need to change the slash/blackslash:
from
to
So the command would be:
使用 mysql 命令提示符中的以下内容 -
不使用引号。即使路径包含空格(' '),也完全不使用引号。
use the following from mysql command prompt-
Use no quotation. Even if the path contains space(' ') use no quotation at all.
如果你正在尝试这个命令:
你可能会得到这样的错误:如果你有特殊字符,主要是“`”
所以我建议使用这样的命令:
这个命令将执行
source /path/to/script.sql 一旦连接到服务器,就会执行您的脚本。
If you are trying this command :
You may get an error like this : if you have special characters, mainly '`'
So I would suggest to use a command like this :
This command will execute
source /path/to/script.sql
once connected to the server, which execute your script.由于 mysql -u yourusername -p yourpassword yourdatabase
text_file
在远程服务器(亚马逊的 EC2)上不起作用...确保首先创建数据库。
然后:
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:
为了将来的参考,我发现这比上述方法更有效,在 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.
在powershell中,当使用>时C:\backup\backup.sql 我们不断得到
因此,如果其他人在 Windows 机器上使用 cmd 或 powershell 对此进行攻击,这会为我们完成。
In powershell, when using > C:\backup\backup.sql we kept getting
So if someone else is thrashing on this on a windows box and using cmd or powershell, this did it for us.
在您的终端中按照以下步骤操作。请注意,将
my_user
、my_database
和file_name
替换为您的详细信息。In your terminal follow the steps below. Note replace
my_user
,my_database
andfile_name
with your details.如果您在这里寻找 DRUPAL 环境,
您可以在项目目录中使用 drush 命令运行
If you are here LOOKING FOR A DRUPAL ENVIRONMENT
You can run with drush command on your project directory
我遇到了这个错误,并尝试了所有我能得到的建议都无济于事。
最后,问题是我的文件夹的文件夹名称中有一个空格,在文件夹路径中显示为正斜杠,一旦我找到并删除它,它就可以正常工作。
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.
我使用 Bash 的 Here Strings 来即时执行 SQL:
https ://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Strings
I use Bash's Here Strings for an instant SQL execution:
https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Strings