So what I have is about 80-90 MySQL DBs to restore and using < gives the 'saved for future use' error when used directly through PowerShell. The next step I tried was using just straight & cmd /c which also prompted the same error.
I tried the code below, but I get either Access Denied or 'system can't find the file specified' and I'm trying to figure out where it's messing up. Any help is definitely appreciated.
PowerShell
$list= gci "C:\Scripts\Migration\mysql\" -name
$mysqlbinpath="C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql"
$mysqluser="admin"
$mysqlpass=getpass-mysql
pushd "C:\Program Files\MySQL\MySQL Server 5.6\bin"
foreach($file in $list){
$dbname=$file.Substring(0, $file.LastIndexOf('.'))
$location='C:\Scripts\Migration\mysql\'+$file
& $bt $mysqluser, $mysqlpass, $dbname, $location
write-host "$file restored"
}
The batch file called by $bt
Batch
"cmd /c "mysql" -u %1 -p%2 --binary-mode=1 %3 < %4"
getpass-mysql is a function I have that pulls the MySQL root pass (our root user is 'admin') as a string.
I hope this isn't a duplicate as I checked other posts first and tried those solutions (perhaps incorrectly applied) and they didn't seem to help.
I have updated the code block with the changes I have made. I am currently receiving the error about using the right syntax near ' ■-'.
Related
I gather from the context of the following page https://github.com/AtomBoy/double-metaphone that the following command has something to do with adding a custom function to the mysql server. Here is the command that I don't understand:
mysql yourDataBaseName -u root -p < metaphone.sql
I successfully ran the command, and installed the function, however I'm still unclear on the complete function of the less than sign. I understand that I am importing a .sql file. In this case the sql file is a function.
Where is this function saved?
What I'm trying to accomplish is dumping each of the databases I have into individual .sql files. Whenever I run my code, I get an error in relation to the 'while' and 'do' commands depending on which one I put first. I've since examples where the 'while' is first and where the 'do' is first. I have tried both and both result in the 'not recognized as an internal or external command.
I'm using the following in my cmd line
"C:\Program Files\MySQL\MySQL Server 5.7\bin\"mysql -u[user] -p[password] -e "show databases" | while read dbname do "C:\Program Files\MySQL\MySQL Server 5.7\bin\"mysqldump -u[user] -p[password] --complete-insert "$dbname" > "D:\Backup\$dbname"_6am_mon.sql; done
However, I keep getting the following error: 'while' is not recognized as an internal or external command, operable program or batch file.
Why am I getting an error for using the while and do commands? How do I resolve this error?
Thanks
The pipe | and the output redirection > are interpreted by cmd.exe first and not viewed as part of the script. Try to escape them with a caret ^| and ^>
Not knowing much about Mysql script processing the quoting of the redirection file name may be the reason $dbname is not resolved.
A workaround could be to put the script in a file and invoke it from the batch
#Echo off
Pushd "C:\Program Files\MySQL\MySQL Server 5.7\bin\"
mysql -u[user] -p[password] -e "Source X:\Path\Script.sql"
Popd
I am tring to use this to install mysql from the command line
cls
echo off
SET ProgFiles86Root="%ProgramFiles(x86)%"
IF NOT %ProgFiles86Root%=="" ( SET ProgFiles86Root=%ProgramFiles% )
echo Starting MySQL install
msiexec /i "mysql-5.5.11-win32.msi" /qn
echo MySQL installed successfully
echo Configurating MySQL Server...
"%ProgFiles86Root%\MySQL\MySQL Server 5.5\bin\mysqlinstanceconfig.exe" -i -q ServiceName=MySQL RootPassword=mysql ServerType=DEVELOPER
DatabaseType=MIXED Port=3306 Charset=utf8
echo MySQL has been installed successfully
setx PATH "%%ProgFiles86Root%\MySQL\MySQL Server 5.5\bin%;"
cd /
c:
mysql --user=root --password=mysql -e "CREATE USER 'myuser'#'localhost' IDENTIFIED BY '123456';"
mysql --user=root --password=mysql -e "GRANT ALL ON mydatabase.* TO 'myuser'#'192.168.0.%' IDENTIFIED BY '123abc' WITH GRANT OPTION; FLUSH
PRIVILEGES;
But i get the error
Starting MySQL install
MySQL installed successfully
Configurating MySQL Server...
The system cannot find the path specified.
MySQL has been installed successfully
SUCCESS: Specified value was saved.
'mysql' is not recognized as an internal or external command,
operable program or batch file.
'mysql' is not recognized as an internal or external command,
operable program or batch file.
The command line says Mysql was installed, but it wasnt, as i cannot fined it in the uninstall section in control panel
Mistake 1: Assigning program files folder path with and without double quotes to ProgFiles86Root
This line
SET ProgFiles86Root="%ProgramFiles(x86)%"
assigns the string "%ProgramFiles(x86)%" with the double quotes and all possibly existing trailing spaces and tabs to environment variable ProgFiles86Root.
The IF condition in line
IF NOT %ProgFiles86Root%=="" ( SET ProgFiles86Root=%ProgramFiles% )
works because the value of the environment variable ProgFiles86Root is with double quotes. The double quotes are also compared by command IF, not just the strings enclosed by the double quotes.
But if this IF condition is true because of batch file is running on a 32-bit Windows, the value of environment variable ProgramFiles is assigned to the environment variable ProgFiles86Root without double quotes.
For more details on how to assign a string correct to an environment variable see the answer on How to set environment variables with spaces? and also the answers linked there.
Let us next look on:
"%ProgFiles86Root%\MySQL\MySQL Server 5.5\bin\mysqlinstanceconfig.exe"
This is either expanded for example on 64-bit Windows to
""C:\Program Files (x86)"\MySQL\MySQL Server 5.5\bin\mysqlinstanceconfig.exe"
with double quotes inside a double quoted file name with path which is of course not good, or for example on 32-bit Windows to
"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqlinstanceconfig.exe"
which would be a correct specification of the executable.
Mistake 2: Invalid command line in batch file
The next mistake is on line:
DatabaseType=MIXED Port=3306 Charset=utf8
This line does not contain a command to execute for Windows command interpreter and therefore must result in an error message.
I'm quite sure that this line contains options for the mysqlinstanceconfig.exe command line. The same mistake is made on last line of batch code in question with the string PRIVILEGES; which should be at end of the command line above.
Mistake 3: Attempt to REPLACE user PATH with a custom folder path
The command setx is for REPLACING user (as done here) or system PATH (only with option /m which of course requires administrator privileges) and does not have any effect on local PATH of currently running command process interpreting the commands in the batch file.
It is of course a very bad idea to REPLACE the user PATH by the string you specified on the setx command line although there is by default no user PATH defined. This computer could start not working anymore as expected if this attempt to replace the user path would be successful.
It looks like you want to just append the path to directory bin of MySQL Server 5.5 to local PATH to run the executable mysql without path. But why not running mysql.exe with full path?
Mistake 4: Setting a different current directory is done wrong
cd /
c:
The first line does nothing. To set current directory to root of current drive it would be necessary to run cd \ as the backslash character is the directory separator on Windows and the forward slash is for options. The command CD interprets the slash as beginning of an option and therefore ignores it as there is no option specified after the forward slash. Run in a command prompt window cd /? for help on this command.
The second line makes the current directory on drive C: the current directory for the currently running command process. Which directory is the current directory on drive C: is not defined by this code.
Mistake 5: Attempt to run mysql.exe without path and file extension
The command setx hopefully for all users failed although this is very unlikely. But even if setx is successful, the current command process interpreting the batch file has already its local copy of all environment variables from parent process created on starting the batch file. Therefore running mysql without file extension and path must fail, except MySQL was already installed before and its directory bin was already added to system or user PATH before the batch file processing started.
Solution:
Use this batch code:
#echo off
cls
if "%ProgramFiles(x86)%" == "" (
set "MySQLServerPath=%ProgramFiles%\MySQL\MySQL Server 5.5\bin"
) else (
set "MySQLServerPath=%ProgramFiles(x86)%\MySQL\MySQL Server 5.5\bin"
)
echo Starting MySQL install ...
%SystemRoot%\System32\msiexec.exe /i "mysql-5.5.11-win32.msi" /qn
echo MySQL installed successfully.
echo Configurating MySQL Server ...
"%MySQLServerPath%\mysqlinstanceconfig.exe" -i -q ServiceName=MySQL RootPassword=mysql ServerType=DEVELOPER DatabaseType=MIXED Port=3306 Charset=utf8
echo MySQL has been configured successfully.
cd /D C:\
rem if not "%PATH:~-1%" == ";" set "PATH=%PATH%;"
rem set "PATH=%PATH%%MySQLServerPath%"
"MySQLServerPath\mysql.exe" --user=root --password=mysql -e "CREATE USER 'myuser'#'localhost' IDENTIFIED BY '123456';"
"MySQLServerPath\mysql.exe" --user=root --password=mysql -e "GRANT ALL ON mydatabase.* TO 'myuser'#'192.168.0.%' IDENTIFIED BY '123abc' WITH GRANT OPTION; FLUSH PRIVILEGES;"
The path to directory for MySQL binaries is not added to system PATH by this batch code if not done on installation or configuration of MySQL. If for some unknown reason the local PATH must be extended with path to MySQL binaries directory, uncomment the two lines near end of batch file above by removing the command REM from both lines.
But if you really want to add MySQL binaries directory to user or system PATH which I really don't recommend then read very carefully the comments and answers on
Why are other folder paths also added to system PATH with SetX and not only the specified folder path?
How can I use a .bat file to remove specific tokens from the PATH environment variable?
How to check if directory exists in %PATH%?
The scenario is that we have bunch of .sql dump files and new ones are created before every deployment.
If anything goes wrong with migration scripts someone has to manually drop/create schema and use command line to restore dump from the latest backup.
I am writing a PS script to automate this process.
find latest dump from given path
drop schema
create schema
restore dump.
I have accomplished first 3 steps but have wasted a lot of time on the 4th one:
Write-Host "Restoring: " $path
$command = '"mysql.exe -uUsername -pPassword ' + $dbname + ' < ' + $path + '"'
Write-Host $command
cmd /C $command
It says "The system cannot find the file specified."
If i use cmd $command without /C it starts cmd in powershell but doesn't execute $command.
I have tried different variations to execute the command in cmd but it doesn't seem to work, and the reason i will have to use cmd is because powershell doesn't play well with '<'.
I tried Invoke-Item and Invoke-Expression but can't guarantee i used correct syntax.
Any suggestions would be greatly appreciated.
Thanks
I'm running MySQL queries from within Windows command prompt by utilizing a batch file.
The file contains:
#echo off
cd "c:\Program Files\MySQL\MySQL Server 5.6\bin"
echo Please type the root password
set /p password=
mysql -uroot -p%password% < C:\Temp\GenerateDB.sql
echo Please wait while script is executing and when finished successfully
Pause
mysql -uroot -p%password% < C:\Temp\UpgradeSchema.sql
echo Please wait while script is executing and when finished successfully
Pause
Exit
My only problem is that I cannot know if the first query (GenerateDB.sql) is complete or not in order to proceed to the next one(UpgradeSchema.sql).
Is there a way to get a prompt when it is complete or even better track real time progress as when you run the script from MySQL Command Client?
Many thanks in advance