如何在Windows10命令提示符下在mysql中正确执行sql文件?

lpwwtiir  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(423)

我已经试了将近三十分钟,试图找出如何从win10命令提示符执行sql文件,但我仍然不知道如何做到这一点。
我在mysql>目录中,在文件路径之前,我不知道“source”是什么。在mysql文件的路径之前,我需要输入什么“source”?
下面是我试图执行的bookorama.sql文件的路径:c:\wamp64\bin\mysql\mysql5.7.23\bin
bookorama.sql位于最后一个“bin”文件夹的文件路径中。
到目前为止,我一直在努力寻找答案,但在这些帖子中,我一直在尝试很多关于答案的建议,但我仍然没有找到正确的答案。
无法从命令提示符运行mysql
执行sql文件
如何在mysql中使用命令行导入sql文件?
使用命令行执行.sql文件
https://tecadmin.net/run-sql-text-file-on-mysql-command-prompt/
下面是试图执行sql文件的命令提示符的副本:

  1. C:\wamp64\bin\mysql\mysql5.7.23\bin>mysql -hlocalhost -uroot -p
  2. Enter password:*********
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 22
  5. Server version: 5.7.23 MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input
  11. mysql> use books
  12. Database changed
  13. mysql> -u bookorama -d books < bookoroma.sql
  14. -> ;
  15. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  16. that corresponds to your MySQL server version for the right syntax to use
  17. near '-u bookorama -d books < bookoroma.sql' at line 1
  18. mysql> mysql
  19. -> ;
  20. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  21. that corresponds to your MySQL server version for the right syntax to use
  22. near 'mysql' at line 1
  23. mysql> mysql -h localhost - u bookorama -p < bookorama.sql
  24. -> ;
  25. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  26. that corresponds to your MySQL server version for the right syntax to use
  27. near 'mysql -h localhost - u bookorama -p < bookorama.sql' at line 1
  28. mysql> cd..
  29. -> ;
  30. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  31. that corresponds to your MySQL server version for the right syntax to use
  32. near 'cd..' at line 1
  33. mysql> mysql -h localhost -u bookorama -D books -p < bookorama.sql
  34. ->
  35. -> CREATE TABLE Customers
  36. -> ;
  37. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  38. that corresponds to your MySQL server version for the right syntax to use
  39. near 'mysql -h localhost -u bookorama -D books -p < bookorama.sql
  40. CREATE TABLE Custom' at line 1
  41. mysql> -u bookorama -D books -P < bookorama.sql
  42. -> ;
  43. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  44. that corresponds to your MySQL server version for the right syntax to use
  45. near '-u bookorama -D books -P < bookorama.sql' at line 1mysql> -u root
  46. -> -p Oscar545* -D bookorama < bookorama.sql
  47. -> ;
  48. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  49. that corresponds to your MySQL server version for the right syntax to use
  50. near '-u root
  51. -p Oscar545* -D bookorama < bookorama.sql' at line 1
  52. mysql> mysql -h localhose u-root -D bookorama <
  53. C:\wamp64\bin\mysql\mysql5.7.23\bin.bookorama.sql
  54. Show warnings disabled.
  55. ERROR:
  56. Unknown command '\b'.
  57. ERROR:
  58. Unknown command '\m'.
  59. ERROR:
  60. Unknown command '\m'.
  61. ERROR:
  62. Unknown command '\b'.
  63. -> ;
  64. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  65. that corresponds to your MySQL server version for the right syntax to use
  66. near 'mysql -h localhose u-root -D bookorama <
  67. C:amp64\bin\mysql\mysql5.7.23\bin.booko' at line 1
  68. mysql> mysql -h localhose u-root -D bookorama < bookorama.sql
  69. -> ;
  70. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  71. that corresponds to your MySQL server version for the right syntax to use
  72. near 'mysql -h localhose u-root -D bookorama < bookorama.sql' at line 1
  73. mysql> mysql -u root -p books < bookorama.sql
  74. -> ;
  75. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  76. that corresponds to your MySQL server version for the right syntax to use
  77. near 'mysql -u root -p books < bookorama.sql' at line 1
  78. mysql> use books
  79. Database changed
  80. mysql> mysql -u root -p books < bookorama.sql
  81. -> ;
  82. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  83. that corresponds to your MySQL server version for the right syntax to use
  84. near 'mysql -u root -p books < bookorama.sql' at line 1
  85. mysql> C:\wamp64\bin\mysql\mysql5.7.23\bin bookorama.sql
  86. Show warnings disabled.ERROR:
  87. Unknown command '\b'.
  88. ERROR:
  89. Unknown command '\m'.
  90. ERROR:
  91. Unknown command '\m'.
  92. ERROR:
  93. Unknown command '\b'.
  94. -> ;
  95. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  96. that corresponds to your MySQL server version for the right syntax to use
  97. near 'C:amp64\bin\mysql\mysql5.7.23\bin bookorama.sql' at line 1
  98. mysql> source C:\wamp64\bin\mysql\mysql5.7.23\bin;
  99. Show warnings disabled.
  100. ERROR:
  101. Unknown command '\b'.
  102. ERROR:
  103. Unknown command '\m'.
  104. ERROR:
  105. Unknown command '\m'.
  106. ERROR:
  107. Unknown command '\b'.
  108. ERROR:
  109. Failed to open file 'C:amp64\bin\mysql\mysql5.7.23\bin', error: 2
  110. mysql> C:\wamp64\bin\mysql\mysql5.7.23\bin\bookorama.sql
  111. Show warnings disabled.
  112. ERROR:
  113. Unknown command '\b'.
  114. ERROR:
  115. Unknown command '\m'.
  116. ERROR:
  117. Unknown command '\m'.
  118. ERROR:
  119. Unknown command '\b'.
  120. ERROR:
  121. Unknown command '\b'.
  122. -> ;
  123. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  124. that corresponds to your MySQL server version for the right syntax to use
  125. near 'C:amp64\bin\mysql\mysql5.7.23\bin\bookorama.sql' at line 1
  126. mysql> mysql -h localhost -u root
  127. -> ;
  128. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  129. that corresponds to your MySQL server version for the right syntax to use
  130. near 'mysql -h localhost -u root' at line 1
  131. mysql> -h localhost -root -pOscar545* books < bookorama.sql
  132. -> ;
  133. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  134. that corresponds to your MySQL server version for the right syntax to use
  135. near '-h localhost -root -pOscar545* books < bookorama.sql' at line 1
  136. mysql> mysql -u root -p books < bookorama.sql
  137. -> ;
  138. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
  139. that corresponds to your MySQL server version for the right syntax to use
  140. near 'mysql -u root -p books < bookorama.sql' at line 1
  141. mysql>
ifmq2ha2

ifmq2ha21#

mysql> 提示您使用:

  1. use books;
  2. source bookorama.sql;

在命令提示下,您将使用:

  1. mysql.exe -h localhost -root -pOscar545* books < bookorama.sql
gopyfrb3

gopyfrb32#

mysql cli(命令行界面)与其他cli或shell程序一样,包括数据库(postgres的psql、sqlite的sqlite3、sql server的sqlcmd、oracle的sqlplus)、langauges(python.exe、perl.exe、php.exe、r.exe),还有一些是可执行文件(.exe),它们不像大多数软件.exe那样使用gui启动,而是在终端窗口中调用会话。
命令行解释器,如windows的powershell和cmd.exe,或unix的(macos/linux)bash终端,用于启动这样的cli会话。实际上,有两种方法可以运行mysql命令:
呼叫 mysql.exe 有争论而不开庭。
启动mysql会话并运行单独的调用。
您的问题是上述两种情况的合并,您正在尝试对 mysql.exe 里面有参数 mysql.exe 或者在会话中运行其顶级参数。在cli中,解析器只解释特殊的mysql命令,比如 source 以及不可执行的直接sql查询, mysql ,调用或连接参数( -h, -u )没有shell命令调用程序: \! .
只需在cli会话之外使用适当的调用:

  1. cd C:\amp64\bin\mysql\mysql5.7.23\bin
  2. mysql -h localhost -u bookorama -p < bookorama.sql

或在cli会话中:

  1. cd C:\amp64\bin\mysql\mysql5.7.23\bin
  2. mysql -h localhost -u bookorama -p
  3. mysql> source bookorama.sql

话虽如此,你却可以做相反的事。
在外部运行命令查询:

  1. mysql -h localhost -u bookorama -p -e "source bookorama.sql"

呼叫终端呼叫内部:

  1. mysql> \! mysql -h localhost -u bookorama -pXXXXX < bookorama.sql
展开查看全部

相关问题