【问题标题】:Restoring mysql backup through windows batch file, which calls the .sql file通过调用.sql文件的windows批处理文件恢复mysql备份
【发布时间】:2020-07-14 17:14:56
【问题描述】:

我正在尝试从备份中恢复 mysql 数据库。我可以直接在命令提示符下使用

mysql> -u username -ppassword < E:\replication\DestinationTest\restore.sql.

这很好用。

我正在尝试将同一行与其他 mysql 语句一起放在 .txt 文件中并调用它。来自mysql命令提示符的txt文件。我有几个删除语句,然后是.txt文件中的恢复语句。 它看起来像这样:

enter code here
Delete from Db.tbl1;
Delete from Db.tbl2;
Delete from Db.tbl3;
Delete from Db.tbl4;
Delete from Db.tbl5;

-h Server -D DB -u username -ppassword < E:\replication\DestinationTest\Db.sql;

当我从 mysql 提示符调用上面的 .txt 文件时,它会执行删除语句,但在恢复部分会出错。它说未知数据库'ereplicationdestinationtestdb.sql'。 请告诉我,正确的方法是什么。

【问题讨论】:

  • mysql&gt; 是提示符。此时输入-u 毫无意义。同样,您需要使用mysql ... &lt; restore.sql 才能正确恢复,不能只运行两个这样的命令。
  • "C:\MySQL\MySQL Server 5.7\bin\mysql.exe" -uroot database_name -pmypassword

标签: mysql mysql-backup


【解决方案1】:

您的删除语句正在执行,因为每个表前面都有如下架构名称。

从 Db.tbl1 中删除; 从 Db.tbl2 中删除;

但是请确认您是否遗漏了需要恢复表的模式名称?根据您提到的错误,您的 restore.sql 似乎不知道需要将表导入哪个架构。

【讨论】:

  • 恢复语句中的-D DB,-D后的DB是我指定的数据库。
【解决方案2】:

这是一个“提示,而不是命令:

mysql>

所以这是无效的:

mysql> -u username -ppassword < E:\replication\DestinationTest\restore.sql.

另外,最后的句号可能是一个错字。

这可能是批处理文件中的有效行:

mysql -u username -ppassword < E:\replication\DestinationTest\restore.sql

如果要在批处理文件中做一些其他的事情,那么先用SQL语句构建一个文本文件,比如

Delete from Db.tbl1;
Delete from Db.tbl2;
Delete from Db.tbl3;
Delete from Db.tbl4;
Delete from Db.tbl5;

在该文件中不要放任何其他内容。我们将文件命名为deletes.sql。现在,我们将两者放在一起——两行:

mysql -u username -ppassword < E:\replication\DestinationTest\restore.sql
mysql -u username -ppassword < deletes.sql

PS:如果您只想删除所有行,TRUNCATE TABLE Db.tbl4; 的运行速度比 DELETE 快。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多