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

我试着运行source /Desktop/test。SQL和收到的错误:

mysql >。回家\ \ sivakumar桌面\ \测试。sql ERROR:无法打开文件 ' \ \ sivakumar \电脑\测试。Sql ',错误:2

你知道我哪里做错了吗?


当前回答

我使用Bash的Here字符串进行即时SQL执行:

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

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

其他回答

你可以使用下面的命令来执行已经写入文本文件的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命令行客户端:MySQL -h hostname -u user database < path/to/test.sql 安装MySQL GUI工具并打开SQL文件,然后执行它 如果数据库可以通过web服务器访问,请使用phpmysql

因为mysql -u你的用户名-p你的密码你的数据库< text_file不能在远程服务器上工作(亚马逊的EC2)…

确保首先创建了数据库。

然后:

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

很可能,你只需要改变斜杠/黑斜杠: 从

 \home\sivakumar\Desktop\test.sql

to

 /home/sivakumar/Desktop/test.sql

因此,命令将是:

source /home/sivakumar/Desktop/test.sql

使用下面的mysql命令提示符-

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

不要使用报价。即使路径包含空格(' '),也不要使用引号。