'How to do a silent install of MySQL in Inno Setup?

I would create a setup program for my Java application. It uses a MySQL database. So the installation of my program must include the installation of MySQL server 5.5, the configuration of server and loading of my database. I use Inno Setup to do this, but I found some problems. I found this code but it is a little old.

[Files]
Filename: msiexec; \
    Parameters: "/i mysql-5.5.11-win32.msi /qn INSTALLDIR=""C:\mysql"""; \
    WorkingDir: C:\Users\Gabriele\Desktop\setup; \
    StatusMsg: Sto installando Mysql 5.5.11;  Flags: runhidden

Filename: C:\mysql\bin\mysqld-nt.exe; \
    Parameters: --install; WorkingDir: C:\mysql\bin; \
    StatusMsg: Sto installando il Servizio MySQL; \
    Description: Installing MySQL Service; Flags: runhidden

Filename: net.exe; Parameters: start mysql; \
    StatusMsg: Sto Avviando il Servizio MySQL; \
    Description: Avvio Servizio MySQL; Flags: runhidden

Filename: C:\mysql\bin\mysql.exe; \
    Parameters: "-e ""insert into mysql.user(host,user,password) values ('localhost','root', PASSWORD('emmaus');"" -u root"; \
    WorkingDir: {tmp}; StatusMsg: Configurazione del Server della Base di Dati; \
    Flags: runhidden

Filename: C:\mysql\bin\mysql.exe;
    Parameters: "-u root -h localhost -e ""create database ata";

Filename: C:\mysql\bin\mysql.exe; \
    Parameters: "-e ""grant all privileges on ata.* to ata;"" -u root"; \
    WorkingDir: {tmp}; StatusMsg: Configurazione Server Base di Dati; \
    Flags: runhidden

Filename: C:\mysql\bin\mysql.exe; \
    Parameters: "-e ""flush privileges;"" -u root"; \
    WorkingDir: {tmp}; StatusMsg: Configurazione Server Base di Dati; \
    Flags: runhidden

Filename: C:\mysql\bin\mysql.exe; \
    Parameters: "-u root -h localhost -e ""use ata; source ata.sql;"; \
    WorkingDir: {tmp}; StatusMsg: Caricamento base di dati; \
    Flags: runhidden  

When I debug, it generates an error after the first statement. Can not find the specified program in the second instruction. I tried to use mysqld instead of mysqld-nt but nothing changes.

Can someone help me?



Solution 1:[1]

[Files]
Source: "J:\mysql-5.5.11-win32.msi"; DestDir: "{tmp}"; Flags: nocompression dontcopy

[Run]
Filename: "{reg:HKLM\SOFTWARE\MySQL AB\MySQL Server 5.5,Location}\bin\mysqld.exe"; 
  Parameters: "--install"; WorkingDir: "{reg:HKLM\SOFTWARE\MySQL AB\MySQL Server 5.5,Location}\bin"; 
  StatusMsg: "Sto installando il Servizio MySQL"; 
  Description: "Installing MySQL Service"; 
  Flags: runhidden; Check: MySQL_Is
;//and the rest of commands
[Code]
function MySQL_Is(): Boolean;
var
iResultCode: Integer;
begin
  Result := true;
  if (not RegKeyExists(HKLM, 'SOFTWARE\MySQL AB\MySQL Server 5.5')) or 
   (not FileExists(ExpandConstant('{reg:HKLM\SOFTWARE\MySQL AB\MySQL Server 5.5,Location}\bin\mysql.exe'))) 
  then begin
     ExtractTemporaryFile('mysql-5.5.11-win32.msi');
     Exec('msiexec.exe', '/i mysql-5.5.11-win32.msi /qn INSTALLDIR="C:\mysql"', 
      ExpandConstant('{tmp}'), SW_HIDE, ewWaitUntilTerminated, iResultCode);
         if not FileExists(ExpandConstant('{reg:HKLM\SOFTWARE\MySQL AB\MySQL Server 5.5,Location}\bin\mysql.exe')) then begin
            MsgBox('Something went wrong! Installation should be terminated', 
              mbInformation, MB_OK);
            Result := false;
         end;
  end;
end;

Solution 2:[2]

I leave here my mysql installation version using inno setup in which you can customize the port and service name for version 5.6 with a customPage I didn't try it with other versions. This solution has been found thanks to other contributions

[Registry]
Root: HKLM; Subkey: "SOFTWARE\MySoftware\G2Database"; ValueType: string; ValueName: Port; ValueData: {code:GetPort}; Flags: createvalueifdoesntexist uninsdeletekeyifempty uninsdeletevalue
Root: HKLM; Subkey: "SOFTWARE\MySoftware\G2Database"; ValueType: string; ValueName: ServiceName; ValueData: {code:GetServiceName}; Flags: createvalueifdoesntexist uninsdeletekeyifempty uninsdeletevalue

We create 2 registers to save the data of Port and service name

[Files]
Source: "{#InstallersDir}MysqlServer\server_5631_win32.msi"; DestDir: "{tmp}"; Flags: ignoreversion nocompression; Tasks: Mysql
Source: "{#InstallersDir}MysqlServer\script.txt"; DestDir: "{app}\mysql\bin"; Flags: ignoreversion nocompression ; Tasks: Mysql
Source: "{#InstallersDir}MysqlServer\users.bat"; DestDir: "{app}\mysql\bin"; Flags: ignoreversion nocompression ; Tasks: Mysql
[Code]

var
  lblPort: TLabel;
  lblServiceName: TLabel;
  ePort: TEdit;
  eServiceName: TEdit;

function frmDBSettingsReg_CreatePage(PreviousPageId: Integer): Integer;
var
  Page: TWizardPage;
begin
  Page := CreateCustomPage(
    PreviousPageId,
    ExpandConstant('{cm:AdvancedSettings}'),
    ExpandConstant('{cm:AdvancedDescription}')
  );

  { lblPort }
  lblPort := TLabel.Create(Page);
  with lblPort do
  begin
    Parent := Page.Surface;
    Left := ScaleX(24);
    Top := ScaleY(30);
    Width := ScaleX(35);
    Height := ScaleY(13);
    Caption := ExpandConstant('{cm:Port}');
  end;

  { lblServiceName }
  lblServiceName := TLabel.Create(Page);
  with lblServiceName do
  begin
    Parent := Page.Surface;
    Left := ScaleX(24);
    Top := ScaleY(60);
    Width := ScaleX(52);
    Height := ScaleY(13);
    Caption := ExpandConstant('{cm:ServiceName}') ;
  end;

  { ePort }
  ePort := TEdit.Create(Page);
  with ePort do
  begin
    Parent := Page.Surface;
    Left := ScaleX(130);
    Top := ScaleY(27);
    Width := ScaleX(185);
    Height := ScaleY(21);
    Text := '3306';
    TabOrder := 0;
  end;

  { eServiceName }
  eServiceName := TEdit.Create(Page);
  with eServiceName do
  begin
    Parent := Page.Surface;
    Left := ScaleX(130);
    Top := ScaleY(56);
    Width := ScaleX(185);
    Height := ScaleY(21);
    Text := 'G2Database';
    TabOrder := 1;
  end;

  Result := Page.ID;
end;

function GetPort(param: String): String;
begin
  Result := Trim(ePort.Text);
end;
function GetServiceName(param: String): String;
begin
  Result := Trim(eServiceName.Text);
end;

We create a new CustomPage to let user set Port and Servicename

{cm:X} means CustomMessage just has to put your own text and remove ExpandConstant

In my case I wanted to skip the MySQL configuration in case you don't mark the task

function ShouldSkipPage(curPageId:Integer):Boolean;
begin
  if curPageID <> 100 then //Need to check if in your case is same ID
    Result := false
  else if ((curPageID = 100) and not WizardIsTaskSelected('Mysql')) then
    Result := true
end;
[Run]
;Install MySQL
Filename: msiexec.exe; Parameters:"/i""{tmp}\server_5631_win32.msi"" /qn INSTALLDIR=""YOUR RUTE"" DATADIR=""YOUR RUTE"" PORT=""{code:GetPort}"" "; WorkingDir:{app}; StatusMsg:"{cm:waitDatabase}"; Flags: runhidden; Tasks: Mysql
;Install Service
Filename: YOUR RUTE\mysqld.exe; Parameters:"--install {code:GetServiceName} --port=""{code:GetPort}"""; WorkingDir:{app}; Flags: runhidden; Tasks: Mysql
;Start Service
Filename: net.exe;  Parameters: start {code:GetServiceName}; WorkingDir:{app}; StatusMsg:{cm:startDatabase}; Flags: runhidden; Tasks: Mysql
;Open Firewall Port
Filename: netsh; Parameters: firewall add portopening TCP {code:GetPort} {code:GetServiceName};  Flags: runhidden; StatusMsg:{cm:configureDatabase}; Tasks: Mysql
;Create Custom users
 Filename: {app}\mysql\bin\users.bat; Parameters:" {code:MySQLPath} {code:FormatRute} {code:GetPort}"; StatusMsg:{cm:configureDatabase};Flags: runhidden;Tasks: Mysql

Here comes Script.txt and Users.bat and Aux Functions for them

use mysql;
update user set password=PASSWORD("root") where user='root';
CREATE USER 'myUser'@'%' IDENTIFIED BY 'myUser';
GRANT ALL PRIVILEGES ON * . * TO 'myUser'@'%';
CREATE USER 'myUser'@'localhost' IDENTIFIED BY 'myUser';
GRANT ALL PRIVILEGES ON * . * TO 'myUser'@'localhost';
FLUSH PRIVILEGES;
:: Move to mysql dir
cd %1

:: Start session with user root -Port and execute script
call "mysql.exe" -u root -P %3 < %2/script.txt
//All this functions will go at CODE section
function MySQLPath(Param:String):String;
var
  Path: string; 
begin
  Path := ExpandConstant('{pf}\MySQL\MySQLServer5.6\bin');
  Result :=  FileSearch ('mysqld.exe',Path );
  StringChangeEx(Result, 'mysqld.exe', '', True);
  Result :=  '"' + Result + '"';
end;

function FormatRute(Param:String):String;
begin
  Result := ExpandConstant('{app}\mysql\bin');
  Result :=  '"' + Result + '"'
end; 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Prikryl
Solution 2 Martin Prikryl