【问题标题】:How Can I Execute an SQL File Correctly Within MySQL in Windows 10 Command Prompt?如何在 Windows 10 命令提示符下的 MySQL 中正确执行 SQL 文件?
【发布时间】:2018-12-26 05:15:20
【问题描述】:

我已经尝试了将近 30 分钟,试图弄清楚如何从 Win 10 命令提示符处执行一个 sql 文件,但我仍然不知道该怎么做。

我在 mysql> 目录中,我无法弄清楚文件路径之前的“源”是什么。我需要在 MySql 文件路径之前输入的“源”是什么?

这是我尝试执行的 bookorama.sql 文件的文件路径: C:\wamp64\bin\mysql\mysql5.7.23\bin

bookorama.sql 位于最后一个“bin”文件夹的文件路径中。

到目前为止,这是我试图找到的内容,但我继续尝试这些帖子中的许多答案建议,但我仍然无法正确解决。

Can't run MySQL from command prompt

Executing a SQL file

How to import an SQL file using the command line in MySQL?

execute .sql file using command line

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>

【问题讨论】:

    标签: mysql sql windows-10 command-prompt


    【解决方案1】:

    mysql&gt; 提示符下,您将使用:

    use books;
    source bookorama.sql;
    

    在您将使用的命令提示符处:

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

    【讨论】:

    • 对不起,是的,source。混合语言。 :)
    【解决方案2】:

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

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

    1. 使用参数调用mysql.exe,无需开启长时间会话。

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

    您的问题是上述两者的混合,您正在尝试使用参数 inside mysql.exe 的命令行调用 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
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 2019-11-07
      • 1970-01-01
      • 2020-06-13
      • 1970-01-01
      相关资源
      最近更新 更多