通过 powershell 恢复 mysql db

通过 powershell 恢复 mysql db

假设我有以下命令:

"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql" -u user --password=pass dbname < backup.sql

它工作正常cmd但在 PowerShell 中我得到以下信息:

At line:1 char:53
+ "C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql" -u user --password=pass dbna ...
+                                                     ~~
Unexpected token '-u' in expression or statement.
At line:1 char:56
+ "C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql" -u user --password=pass dbna ...
+                                                        ~~~~
Unexpected token 'user' in expression or statement.
At line:1 char:84
+ ... rd=pass dbname < backup.sql
+                    ~
The '<' operator is reserved for future use.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

如果我删除双引号,该命令在 PowerShell 中会出现较少的错误,但在中根本不起作用cmd,并出现此错误:

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

在 PowerShell 中我得到这个:

At line:1 char:82
+ ... rd=pass dbname < backup.sql
+                    ~
The '<' operator is reserved for future use.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : RedirectionNotSupported

那么我如何将 backup.sql 输入mysql

我认为在 Linux 中 piping 会起作用。例如。

C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql -u user --password=pass dbname | cat backup.sql

但这给了我这个错误:

C:\Program : The term 'C:\Program' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql -u user --password=pass dbname ...
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Program:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

如果我重新添加双引号,它也不起作用。

有任何想法吗?

答案1

用于Get-Content读取文件并将|其传送到命令。用于&运行命令。

get-content 'c:\folder\backup.sql' | &"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe" -u user --password=pass dbnamedbname

答案2

尝试:

&cmd /c "mysql -u user --password=pass dbname < backup.sql"

在这种情况下,您基本上是在运行cmd,但这是我首选的方式,因为在 PowerShell 本身中使用<是行不通的(直到有一天它能做到:P)。

此外,我建议将可执行文件的路径mysql放入环境变量中PATH,这样您就可以简单地使用mysql而不是整个路径来mysql运行命令。

相关内容