我想在MySQL中执行一个包含SQL查询的文本文件。
我试着运行source /Desktop/test。SQL和收到的错误:
mysql >。回家\ \ sivakumar桌面\ \测试。sql ERROR:无法打开文件 ' \ \ sivakumar \电脑\测试。Sql ',错误:2
你知道我哪里做错了吗?
我想在MySQL中执行一个包含SQL查询的文本文件。
我试着运行source /Desktop/test。SQL和收到的错误:
mysql >。回家\ \ sivakumar桌面\ \测试。sql ERROR:无法打开文件 ' \ \ sivakumar \电脑\测试。Sql ',错误:2
你知道我哪里做错了吗?
当前回答
如果你在MySQL命令行MySQL >,你必须声明SQL文件作为源文件。
mysql> source \home\user\Desktop\test.sql;
其他回答
你可以使用下面的命令来执行已经写入文本文件的mysql语句:
mysql -u yourusername -p yourpassword yourdatabase < text_file
如果你的数据库还没有创建,首先登录到你的mysql使用:
mysql -u yourusername -p yourpassword
然后:
mysql>CREATE DATABASE a_new_database_name
然后:
mysql -u yourusername -p yourpassword a_new_database_name < text_file
这样就行了!
更多信息请访问:http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.html
如果你正在尝试这个命令:
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
该命令将执行source /path/to/script。SQL一旦连接到服务器,将执行您的脚本。
作为将来的参考,我发现这与前面提到的方法相比,在Windows下的msql控制台:
mysql > >源c: / / path_to_file / / path_to_file / / file_name.sql;
如果您的根驱动器不叫“c”,那么只需与您的驱动器交换。先试试反斜杠,如果不行,再试试正斜杠。如果它们也不起作用,请确保您有完整的文件路径,文件名上的.sql扩展名,如果您的版本坚持使用分号,请确保它存在并重试。
我使用Bash的Here字符串进行即时SQL执行:
mysql -uroot -p <<<"select date(now())"
https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Strings
我来这里也是为了寻找这个答案,下面是我发现最适合我的答案:注意,我使用的是Ubuntu 16.x.x
访问mysql使用:
Mysql -u <your_user> - p
在mysql提示符下,输入:
源file_name.sql
希望这能有所帮助。