PowerShell, MySQL Command-Line, and mysqldump Restore Issues

Today’s fun involved trying to restore a mysql dump file. I’m integrating this into an existing process that happens to be a PowerShell script. First, I worked out the appropriate command via the MySQL Command Line Tool to import some data that I needed to work with. The result was that I tried to add something similar to the following into my PowerShell script:

mysql -u root -p[root_password] [database_name] < dumpfile.sql

Running this command in PowerShell, I was given this lovely error message.

The '<' operator is reserved for future use.

I’m not an expert PowerShell coder. I try my hand at it to get things done, but I don’t study it regularly. I pretty much know that if I need to do task A, there’s probably a command for it that I just need to get syntactically correct. I expect to put that command in a window that is supposed to run commands and I expect it to work. Apparently, the ‘<‘ operator is called a “redirection operator” and PowerShell doesn’t support them in this way. I had no concept of “redirection operator” before today. It’s still a fuzzy concept.

In the end, I found two solutions to my problem. One of them was good and one was not. First, here’s what you don’t want to do:

Get-Content dumpfile.sql | mysql -u root -p[root_password] [database_name]

This will cause your entire file to get loaded into an object that can be manipulated in local memory. Fine if you know your file is small, but bad if your file is large. My file is 300+ MB. Instead, try this:

&cmd /c "mysql -u root -p[root_password] [database_name] < dumpfile.sql"

This basically runs this command as though you typed it in the command line. It was much faster.

P.S – Thank you, Luke, for helping me figure this out.

About the author

Abby Sims

Copyright © 2016, Abby Sims