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

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

我已经试了将近三十分钟,试图找出如何从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文件的命令提示符的副本:

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>
ifmq2ha2

ifmq2ha21#

mysql> 提示您使用:

use books;
source bookorama.sql;

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

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会话之外使用适当的调用:

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

相关问题