我想在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 -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一旦连接到服务器,将执行您的脚本。
其他回答
使用下面的mysql命令提示符-
source \\home\\user\\Desktop\\test.sql;
不要使用报价。即使路径包含空格(' '),也不要使用引号。
有很多种方法。
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。
希望这是有用的,而不是多余的。
很可能,你只需要改变斜杠/黑斜杠: 从
\home\sivakumar\Desktop\test.sql
to
/home/sivakumar/Desktop/test.sql
因此,命令将是:
source /home/sivakumar/Desktop/test.sql
从命令行直接传递password参数永远都不是一个好习惯,它保存在~/。Bash_history文件,可以从其他应用程序访问。
用这个代替:
mysql -u user --host host --port 9999 database_name < /scripts/script.sql -p
Enter password:
如果您正在寻找DRUPAL环境
您可以在项目目录上使用drush命令运行
drush sqlc