Solved

Mysql Pendrive data and service

Posted on 2013-07-01
9
725 Views
Last Modified: 2013-07-02
Hello,

I want to install Mysql and data, on a pendrive and the service must be started and stopped by application made in Delphi or automatically when I insert or disconnect the pendrive into the USB port.
I found a way to move the databases and modify the my.ini file but do not know how to start the service.
Seeking a solution, thanks
0
Comment
Question by:danz67
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 39292447
Assuming that you know how to start e service from delphi, you can create an autorun.inf that points to the startservice application on usb pen drive
So when you'll plug in the pen drive, the autorun.inf will execute the start service program

Another way is to create a service or application running in background to listen for the WMDeviceChange message. From lParam you can see if the device is a usb pen drive and  depending on the wParam you will know if the device was plugged in or out  and so start or stop the mysql service from there
0
 

Author Comment

by:danz67
ID: 39292654
Good, you have a sample for make?
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 39293160
Well, here's my little elaboration of what can be found easly googling around.

This code will detect a pen drive and the assigned drive, when inserted or removed.
You could just implement the code for start/stop the mysql service here

Create a new form and paste this code into the unit. No component is needed. Just double click on Form.oncreate and form.onclose to assign the events correctly. Finally run it and try plugging and unplagging a pen drive to see the effect

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    FWindowHandle: HWND;
    procedure WndProc(var Msg: TMessage);
    function USBRegister: Boolean;
    procedure WMDeviceChange(var Msg: TMessage); dynamic;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  UsbMsg: String;

implementation

{$R *.dfm}

type
  PDevBroadcastHdr = ^DEV_BROADCAST_HDR;

  DEV_BROADCAST_HDR = packed record
    dbch_size: DWORD;
    dbch_devicetype: DWORD;
    dbch_reserved: DWORD;
  end;

  PDevBroadcastDeviceInterface = ^DEV_BROADCAST_DEVICEINTERFACE;

  DEV_BROADCAST_DEVICEINTERFACE = record
    dbcc_size: DWORD;
    dbcc_devicetype: DWORD;
    dbcc_reserved: DWORD;
    dbcc_classguid: TGUID;
    dbcc_name: short;
  end;

  PDevBroadcastVolume = ^TDevBroadcastVolume;

  TDevBroadcastVolume = packed record
    dbcv_size: DWORD;
    dbcv_devicetype: DWORD;
    dbcv_reserved: DWORD;
    dbcv_unitmask: DWORD;
    dbcv_flags: Word;
  end;

const
  GUID_DEVINTERFACE_USB_DEVICE: TGUID = '{A5DCBF10-6530-11D2-901F-00C04FB951ED}';
  DBT_DEVICEARRIVAL = $8000; // system   detected   a   new   device
  DBT_DEVICEREMOVECOMPLETE = $8004; // device   is   gone
  DBT_DEVTYP_DEVICEINTERFACE = $00000005;
  DBTF_MEDIA = $0001;
  DBT_DEVTYP_VOLUME = $0002;

  // device   interface   class
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  DeallocateHWnd(FWindowHandle);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FWindowHandle := AllocateHWnd(WndProc);
  USBRegister;
end;

Procedure TForm1.WndProc(var Msg: TMessage);
begin
  if (Msg.Msg = WM_DEVICECHANGE) then
  begin
    try
      WMDeviceChange(Msg);
    except
      Application.HandleException(Self);
    end;
  end
  else
    Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
end;

Procedure TForm1.WMDeviceChange(var Msg: TMessage);
var
  devType: Integer;
  Datos: PDevBroadcastHdr;
  pData: PDevBroadcastDeviceInterface;
  Drive: string;
  function GetDrive(pDBVol: PDevBroadcastVolume): string;
  var
    i: Byte;
    Maske: DWORD;
  begin
    Maske := pDBVol^.dbcv_unitmask;
    for i := 0 to 25 do
    begin
      if (Maske and 1) = 1 then
        Result := Char(i + Ord('A'));
      Maske := Maske shr 1;
    end;
  end;

begin
  if (Msg.wParam = DBT_DEVICEARRIVAL) or (Msg.wParam = DBT_DEVICEREMOVECOMPLETE) then
  begin
    Datos := PDevBroadcastHdr(Msg.lParam);
    devType := Datos^.dbch_devicetype;
    case devType of
      DBT_DEVTYP_DEVICEINTERFACE:
        begin // USB   Device
          pData := PDevBroadcastDeviceInterface(Msg.lParam);
          if Msg.wParam = DBT_DEVICEARRIVAL then
            UsbMsg := 'USB Device inserted. Assigned to drive %s:'
            // add here your code to start MySql service
          else
            UsbMsg := 'USB Device removed. Drive %s: no more available'
            // add here your code to stop MySql service
        end;
      DBT_DEVTYP_VOLUME:
        begin
          Drive := GetDrive(PDevBroadcastVolume(Msg.lParam));
          ShowMessage(Format(UsbMsg, [Drive]));
        end;
    end;
  end;
end;

function TForm1.USBRegister: Boolean;
var
  dbi: DEV_BROADCAST_DEVICEINTERFACE;
  Size: Integer;
  r: Pointer;
begin
  Result := False;
  Size := SizeOf(DEV_BROADCAST_DEVICEINTERFACE);
  ZeroMemory(@dbi, Size);
  dbi.dbcc_size := Size;
  dbi.dbcc_devicetype := DBT_DEVTYP_DEVICEINTERFACE;
  dbi.dbcc_reserved := 0;
  dbi.dbcc_classguid := GUID_DEVINTERFACE_USB_DEVICE;
  dbi.dbcc_name := 0;
  r := RegisterDeviceNotification(FWindowHandle, @dbi, DEVICE_NOTIFY_WINDOW_HANDLE);
  if Assigned(r) then
    Result := True;
end;

end.

Open in new window

0
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 

Author Comment

by:danz67
ID: 39293506
Very good, for complete all i need code to start and stop MySql service.
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 39293601
That's another q.
One q. about detecting pen drive in/out
Another q. for start/stop a service

That's the ExEx policy
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 39293628
Anyway there's an easy way to start/stop mysql

winexec('net start mysql',0);

winexec('net stop mysql',0);

Just a gift for this time ;)
0
 

Author Comment

by:danz67
ID: 39293954
well, then you can explain to me what I have to write to the file autorun.inf to start the service mysql? I'll be happy to accept your answer, thanks
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 39294142
I must explain again better.
Autoran.inf is a way, but it should work just to start the mysql service, because when you'll unplug the pen drive nothing other could be execute from there and the device remove message couldn't be listened.

What I've suggested is to create an application that run on the machine to listen for the usb device plug/unplug and so start/stop the mysql service.
I guess that you are distributin an application that can work with mysql databases installed on a pen drive, so that application could listen itself for the usb device changes and then start/stop mysql
0
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 250 total points
ID: 39294177
Or do you need that all (Mysql, application and Database) stays just on the pen drive?

In this case there's a project that does exactly this.

Take a look here (it's an italian page, but you could translate what you need to understand)

http://silverpeacock.wordpress.com/2008/09/15/start-stop-mysql-delphi-pendrive/
0

Featured Post

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question