如何使用 MySQL 命令行工具执行 SQL 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14348045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute a SQL file using MySQL command line tool?
提问by John Smith
I'm using symfony, putty and other SQL commands seem to work fine but when I try to run an .sql
file (of which there are many), the command line throws an error:
我正在使用 symfony、putty 和其他 SQL 命令似乎工作正常,但是当我尝试运行一个.sql
文件(其中有很多)时,命令行会引发错误:
You have an error in your SQL syntax; check the manual ...
This is the command I'm trying to execute:
这是我试图执行的命令:
mysql < data/MySQLFile.sql;
I already used the use
command on the proper database and tried using the full path, relative path, etc. Not sure what the problem is.
我已经use
在正确的数据库上使用了该命令,并尝试使用完整路径、相对路径等。不确定问题是什么。
Here's the statement:
这是声明:
ALTER TABLE tablename
ADD COLUMN `blahblah` varchar(10) NULL;
ALTER TABLE tablename
ADD COLUMN `ggggggg` varchar(50) NULL;
回答by NarasimhaTejaJ
if you want to execute through command line
如果你想通过命令行执行
mysql -u user -p < file.sql
mysql -u user -p < file.sql
and if you want to execute once logged in to mysql database. use any of the below commands.
如果你想在登录到 mysql 数据库后执行。使用以下任何命令。
source file.sql
or
或者
\. file.sql
\. file.sql
or
或者
mysql db_name <file.sql
回答by Abraham
Ok if you are in the mysql interface already type in:
好的,如果你在mysql界面已经输入:
\. /full path/to/file.sql
What I meant with full path is that you need to specify according to the current OS file system the location of the file. Just by putting data/ is not enough. For example if you are using MAC OS it would be: From the terminal and assuming is in Documents:
我对完整路径的意思是您需要根据当前操作系统文件系统指定文件的位置。仅仅放置 data/ 是不够的。例如,如果您使用的是 MAC OS,它将是:从终端并假设在文档中:
mysql < /Users/<yourusername>/Documents/file.sql
If you are already logged in mysql then type in:
如果您已经登录 mysql,请输入:
\. /Users/<yourusername>/Documents/file.sql
If my tips don't help you, I will recommend you to go to this page:
如果我的提示对您没有帮助,我会建议您转到此页面:
http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.htmlTo Find out more about this.
http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.html了解更多信息。
回答by datavoredan
I think you just need to type 'source' before the path to the sql file. So you would have
我认为您只需要在 sql 文件的路径之前键入“源”。所以你会有
mysql < source [fullpath]/MySQLFile.sql;