社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

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

fsulover • 5 年前 • 1522 次点击  

我已经尝试了将近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>
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/48918
 
1522 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Parfait
Reply   •   1 楼
Parfait    6 年前

MySQL CLI(命令行界面)与其他CLI s或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命令:

  1. 呼叫 mysql.exe 没有开长会的争论。

  2. 启动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
    
shawn
Reply   •   2 楼
shawn    6 年前

mysql> 提示您使用:

use books;
source bookorama.sql;

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

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