restoring a mysql db via powershell

5,439

Solution 1

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

Solution 2

Try:

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

You're essentially running in cmd in this case, but it's my preferred way of doing it as using < in PowerShell itself will not work (until it does one-day :P).

Also, I recommend putting the path to your mysql executable into your PATH environment variable so you can simply use mysql rather than the whole path to mysql just to run a command.

Share:
5,439

Related videos on Youtube

neubert
Author by

neubert

Apparently, this user prefers to keep an air of mystery about them.

Updated on September 18, 2022

Comments

  • neubert
    neubert over 1 year

    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?