Smith Smith - 1 year ago 183
MySQL Question

mysql install from batch file

I am tring to use this to install mysql from the command line

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 /

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


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

Answer Source

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 /

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.


Use this batch code:

@echo off

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download