在Windows 10命令提示符中如何正确执行MySQL的SQL文件?

3
我已经尝试了将近30分钟来执行一个SQL文件,但我仍然无法从Win 10命令提示符中找到执行方法。我在mysql>目录下,但我不知道在文件路径前面要输入什么“源”。请问在MySql文件路径前需要输入什么“源”?
这是我要执行的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文件的副本:
C:\wamp64\bin\mysql\mysql5.7.23\bin>mysql -hlocalhost -uroot -p
Enter password: *********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input 

mysql> use books
Database changed
mysql> -u bookorama -d books < bookoroma.sql
    -> ;
ERROR 1064 (42000): 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 '-u bookorama -d books < bookoroma.sql' at line 1
mysql> mysql
    -> ;
ERROR 1064 (42000): 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 'mysql' at line 1
mysql> mysql -h localhost - u bookorama -p < bookorama.sql
    -> ;
ERROR 1064 (42000): 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 'mysql -h localhost - u bookorama -p < bookorama.sql' at line 1
mysql> cd..
    -> ;
ERROR 1064 (42000): 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 'cd..' at line 1
mysql> mysql -h localhost -u bookorama -D books -p < bookorama.sql
    ->
    -> CREATE TABLE Customers
    -> ;
ERROR 1064 (42000): 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 'mysql -h localhost -u bookorama -D books -p < bookorama.sql

CREATE TABLE Custom' at line 1
mysql> -u bookorama -D books -P < bookorama.sql
    -> ;
ERROR 1064 (42000): 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 '-u bookorama -D books -P < bookorama.sql' at line 1mysql> -u root
    -> -p Oscar545* -D bookorama < bookorama.sql
    -> ;
ERROR 1064 (42000): 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 '-u root
-p Oscar545* -D bookorama < bookorama.sql' at line 1
mysql> mysql -h localhose u-root -D bookorama < 
C:\wamp64\bin\mysql\mysql5.7.23\bin.bookorama.sql
Show warnings disabled.
ERROR:
Unknown command '\b'.
ERROR:
Unknown command '\m'.
ERROR:
Unknown command '\m'.
ERROR:
Unknown command '\b'.
    -> ;
ERROR 1064 (42000): 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 'mysql -h localhose u-root -D bookorama < 
C:amp64\bin\mysql\mysql5.7.23\bin.booko' at line 1
mysql> mysql -h localhose u-root -D bookorama < bookorama.sql
    -> ;
ERROR 1064 (42000): 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 'mysql -h localhose u-root -D bookorama < bookorama.sql' at line 1
mysql> mysql -u root -p books < bookorama.sql
    -> ;
ERROR 1064 (42000): 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 'mysql -u root -p books < bookorama.sql' at line 1
mysql> use books
Database changed
mysql> mysql -u root -p books < bookorama.sql
    -> ;
ERROR 1064 (42000): 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 'mysql -u root -p books < bookorama.sql' at line 1
mysql> C:\wamp64\bin\mysql\mysql5.7.23\bin bookorama.sql
Show warnings disabled.ERROR:
Unknown command '\b'.
ERROR:
Unknown command '\m'.
ERROR:
Unknown command '\m'.
ERROR:
Unknown command '\b'.
    -> ;
ERROR 1064 (42000): 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 'C:amp64\bin\mysql\mysql5.7.23\bin bookorama.sql' at line 1
mysql> source C:\wamp64\bin\mysql\mysql5.7.23\bin;
Show warnings disabled.
ERROR:
Unknown command '\b'.
ERROR:
Unknown command '\m'.
ERROR:
Unknown command '\m'.
ERROR:
Unknown command '\b'.
ERROR:
Failed to open file 'C:amp64\bin\mysql\mysql5.7.23\bin', error: 2
mysql> C:\wamp64\bin\mysql\mysql5.7.23\bin\bookorama.sql
Show warnings disabled.
ERROR:
Unknown command '\b'.
ERROR:
Unknown command '\m'.
ERROR:
Unknown command '\m'.
ERROR:
Unknown command '\b'.
ERROR:
Unknown command '\b'.
    -> ;
ERROR 1064 (42000): 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 'C:amp64\bin\mysql\mysql5.7.23\bin\bookorama.sql' at line 1
mysql> mysql -h localhost -u root
    -> ;
ERROR 1064 (42000): 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 'mysql -h localhost -u root' at line 1
mysql> -h localhost -root -pOscar545* books < bookorama.sql
    -> ;
ERROR 1064 (42000): 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 '-h localhost -root -pOscar545* books < bookorama.sql' at line 1
mysql> mysql -u root -p books < bookorama.sql
    -> ;
ERROR 1064 (42000): 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 'mysql -u root -p books < bookorama.sql' at line 1
mysql>
2个回答

3
mysql>提示符下,您需要使用以下命令:
use books;
source bookorama.sql;

在命令提示符下,您会使用以下命令:

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

抱歉,是的,source。混合语言。 :) - shawn

0

MySQL CLI(命令行界面)像其他CLI或shell程序一样,包括数据库(Postgres的psql,SQLite的sqlite3,SQL Server的sqlcmd,Oracle的sqlplus),语言(python.exe,perl.exe,php.exe,r.exe)和其他可执行文件(.exe),不像大多数软件.exes那样启动GUI,而是在终端窗口内调用会话。

诸如Windows的PowerShell和Cmd.exe或Unix的(MacOS / Linux)Bash终端之类的命令行解释器用于启动此类CLI会话。基本上,有两种运行MySQL命令的方法:

  1. 使用参数调用mysql.exe而不打开长时间会话。

  2. 启动mysql会话并运行单独的调用。

你的问题在于将上述两个混淆了,你试图在 mysql.exe 命令行中使用参数 内部 调用或在会话中运行其顶级参数。在 CLI 中,解析器仅解释特殊的 MySQL 命令,如 source 和直接 SQL 查询,而不是可执行的 mysql、调用或连接参数(-h,-u),没有其 shell 命令调用者:\!

只需在 CLI 会话外使用适当的调用即可:

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

或者在CLI会话中:

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

话虽如此,您也可以做相反的事情。

  • 在外部运行命令查询:

    mysql -h localhost -u bookorama -p -e "source bookorama.sql"
    
  • 在内部调用终端调用:

    mysql> \! mysql -h localhost -u bookorama -pXXXXX < bookorama.sql
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接