Saturday, 16 February 2019

command line - restoring a mysql db via powershell


Say I have the following command:


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

It works fine in cmd but in PowerShell I get the following:


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

If I remove the double quotes the command gives me less errors in PowerShell but doesn't work at all in cmd, giving me this error:


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

In PowerShell I get this:


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

So how do I feed backup.sql into mysql?


In Linux I think pipping would work. eg.


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

But that gives me this error:


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

If I re-add the double quotes it doesn't work either.


Any ideas?



Answer



Use Get-Content to read the file and pipe | it to your command. Use & to run the command.


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


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...