Unable to use while command in Windows Command Prompt - mysql

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

Related

sqldump somehow looks for a table instead of full backup?

I have a simple BAT file that I want to run at times to take backups of two local development MySQL databases.
The file name should be "testdb1-Fri 6/6/2018 194233.sql" for example. However, my BAT script is making mysqldump look for a table called 6/6/2018 somehow? Script below, and screenshot of error.
#ECHO OFF
set filename=%date:~0,4%%date:~5,2%%date:~8,2%%date:~10,4% %time:~0,2%%time:~3,2%%time:~6,2%
"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe" -u root -proot -hlocalhost cadsys > C:\temp\testdb1-%filename%.sql
"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe" -u root -proot -hlocalhost comm1 > C:\temp\testdb-%filename%.sql
pause
cls
exit
Any pointers?
Add quotes around the suggested filename, the spaces within the filename are interpreted by mysqldump as seperate commands
set filename="%date:~0,4%%date:~5,2%%date:~8,2%%date:~10,4% %time:~0,2%%time:~3,2%%time:~6,2%"

mysql install from batch file

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%?

Running mysql command from bat file with redirection

I have the following batch file:
#ECHO on
cd "C:\Program Files\MariaDB\mariadb\bin"
mysql -u root < "C:\database_setup.sql"
When I run the command directly in the command line, it works fine. When I run this batch file I get that it's trying to execute:
mysql -u root 0<"C:\database_setup.sql"
To solve this, I tried to escape the less than sign with:
mysql -u root ^< "C:\database_setup.sql"
It appears to be correct in the console but it's dumping the mysql options instead of inserting the contents of database_setup.sql.
I'm thinking that this is because the "<" is actually be referred to as a string since I'm escaping it and not as the redirection operator.
How does one accomplish running this command in a batch file (which works fine directly in the console)?
The following workaround could help you:
mysql -u root -e "SOURCE C:\database_setup.sql"
Also the following should work:
type C:\database_setup.sql | mysql -u root

using batch to detect whether database exists,if not ,create it

I am going to write a .bat file to realize this function:
to detect whether the database and the table existed,if not exist,create them.
I tried to write the bat file like this:
"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe" -h localhost -u root --password=
select * from martin.aaaperson
Then in cmd when I execute this, the bat file will not run the query until after I exit from mysql.
C:\000test>"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe" -h localhost
-u root --password=
(information of mysql)
mysql> exit
Bye
C:\000test>select * from martin.aaaperson
"select" is not knowby cmd
you need to do something like
mysql.exe -u root -p < your_sql_commands_in_this_file.sql
Put your sql into a file, and then redirect that file into mysql, so it's used as actual commands.
Batch files can only work at the command line. Once you run mysql.exe the .bat file is suspended until mysql exits, and then the batch resumes. That means by the time the batch fires back up again, you've exited mysql and are no longer doing sql operations - you're just back at a command prompt.

Why does script to run MySQLdump on each line of file gives error "command not found"?

Performing automated database backups for starters and testing commands. I've found for performing an action on each line of a text file via BASH CLI is something to the effect of:
# while read line; do
COMMAND $line
done
I've created a list of the database names file:
# mysql -uroot -e "show databases" > databases
Then tried the following against the file to see if it would work correctly.
# while read line; do
"mysqldump -uroot $line > /dbbackups/$line.sql"
done
Seemingly, this would be working correctly but am met with the following error(s):
[04:58:46] [root#theia database-backup-testing]# cat databases | while read line ; do "mysqldump -uroot $line > $line.sql" ; done
-bash: mysqldump -uroot Database > Database.sql: command not found
-bash: mysqldump -uroot information_schema > information_schema.sql: command not found
-bash: mysqldump -uroot cphulkd > cphulkd.sql: command not found
I am not sure why it is giving command not found, when obviously, the output of the commands seems to be correct. I have also tried using the absolute path of mysqldump (/usr/bin/mysqldump) but it gives the same error(s).
Can anyone fill me in on why this is happening?
EDIT: I found a fix:
The script works if the quotes are removed:
# cat databases |
while read line; do
mysqldump -uroot $line > $line.sql
done
Apparently, the quotes causes it to execute as a string and not a command.
why it is giving command not found
Your quotes are not correct, Try something like this:
while read line ; do mysqldump -uroot "$line" > "$line".sql ; done
Haven't used mysqldump so I cant help with the syntax of the specific command.
Here's a bash script that solves this:
https://github.com/jeevandongre/backup-restore