DELPHI - find mysql server

how do I know if mysql server is installed on a computer and if there is a specific database?
danz67Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

carsRSTCommented:
Couple of options..

1.  Check the services on the computer and see if it's listed/running
     (just look for "MySQL" in the services box)
2.  Search for "mysql.exe" on the computer - run it from the command line and see if it prompts you for a login

Now if you know it's installed and have a username/password, then use the following in the command line to see what databases it has:

mysql> show databases;

Any databases on the server will show.
0
danz67Author Commented:
I understand, can you give me an example, I do not know where to start, thanks
0
carsRSTCommented:
is this a windows computer?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

danz67Author Commented:
it's windows and use Delphi for development
0
systanCommented:
Make a program to Scan port 3306(mysql), if there is, mysql is open.
Make a program to Scan port 1433(mSsql), if there is, mSsql is open.

And other database Server ports, you would like.

heres my code: (just put listbox1 and a Timer1

unit NsMain;
{$R-}

interface

uses
Winsock,
TlHelp32,
ShellAPI,
Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;

const
  ANI_SIZE = 1;

  type
  TMainForm = class(TForm)
    Timer: TTimer;
    ListBox1: TListBox;
     procedure FormCreate(Sender: TObject);
     procedure TimerTimer(Sender: TObject);
 private
    function GetProcessNameByPId(PId: DWORD): string;
    procedure GetConnectionsTable(ReNew: Boolean);
    function GetPort(dwPort: DWORD; const Proto: string): string;
  protected
  public
  end;

  PMibTcpRow = ^TMibTcpRow;
  TMibTcpRow = packed record
    dwState: DWORD;
    dwLocalAddr: DWORD;
    dwLocalPort: DWORD;
    dwRemoteAddr: DWORD;
    dwRemotePort: DWORD;
  end;
  PMibTcpTable = ^TMibTcpTable;
  TMibTcpTable = packed record
    dwNumEntries: DWORD;
    table: array[0..ANI_SIZE - 1] of TMibTcpRow;
  end;


  PMibTcpExRow = ^TMibTcpExRow;
  TMibTcpExRow = packed record
    dwState: DWORD;
    dwLocalAddr: DWORD;
    dwLocalPort: DWORD;
    dwRemoteAddr: DWORD;
    dwRemotePort: DWORD;
    dwProcessId: DWORD;
  end;
  PMibTcpExTable = ^TMibTcpExTable;
  TMibTcpExTable = packed record
    dwNumEntries: DWORD;
    table: array[0..ANI_SIZE - 1] of TMibTcpExRow;
  end;
  TAllocateAndGetTcpExTableFromStack = function(pTcpExTable: PMibTcpExTable;
  bOrder: BOOL; heap: THandle; dwZero, dwFlags: DWORD): DWORD; stdcall;

  PMibUdpExRow = ^TMibUdpExRow;
  TMibUdpExRow = packed record
    dwLocalAddr: DWORD;
    dwLocalPort: DWORD;
    dwProcessId: DWORD;
  end;
  PMibUdpExTable = ^TMibUdpExTable;
  TMibUdpExTable = packed record
    dwNumEntries: DWORD;
    table: array[0..ANI_SIZE - 1] of TMibUdpExRow;
  end;
  TAllocateAndGetUdpExTableFromStack = function(pUdpExTable: PMibUdpExTable;
   bOrder: BOOL; heap: THandle; dwZero, dwFlags: DWORD): DWORD; stdcall;


function GetTcpTable(pTcpTable: PMibTcpTable; pdwSize: PDWORD; bOrder: BOOL): DWORD; stdcall;
const
  PID_SIZE = SizeOf(TMibTcpExRow) - SizeOf(TMibTcpRow);
var
  MainForm: TMainForm;
  GetTcpExTable: TAllocateAndGetTcpExTableFromStack = nil;
  GetUdpExTable: TAllocateAndGetUdpExTableFromStack = nil;
  FoundExApi: Boolean = False;
  TaskMessage: UINT;

implementation

const
iphelper = 'iphlpapi.dll';
function GetTcpTable; external iphelper  name 'GetTcpTable';

{$R *.dfm}

procedure InitExApi;
var
  hLib: HMODULE;
begin
  hLib := GetModuleHandle(iphelper);
  @GetTcpExTable := GetProcAddress(hLib, 'AllocateAndGetTcpExTableFromStack');
  @GetUdpExTable := GetProcAddress(hLib, 'AllocateAndGetUdpExTableFromStack');
  if Assigned(GetTcpExTable) and Assigned(GetUdpExTable) then
    FoundExApi := True;
end;

procedure Check(ResultCode: DWORD);
begin
  if ResultCode <> NO_ERROR then
    raise Exception.Create(SysErrorMessage(ResultCode));
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  InitExApi;
end;

function TMainForm.GetProcessNameByPId(PId: DWORD): string;
var
  hSnapProcess, hSnapModule: THandle;
  ProcessEntry: TProcessEntry32;
  ModuleEntry: TModuleEntry32;
begin
  Result := '?';
  hSnapProcess := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      if hSnapProcess <> INVALID_HANDLE_VALUE then
  try
    ProcessEntry.dwSize := SizeOf(TProcessEntry32);
    if Process32First(hSnapProcess, ProcessEntry) then
    repeat
      if ProcessEntry.th32ProcessID = PId then
      begin
        Result := ProcessEntry.szExeFile;
        hSnapModule := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, PId);
        if hSnapModule <> INVALID_HANDLE_VALUE then
        try
          ModuleEntry.dwSize := SizeOf(TModuleEntry32);
          if Module32First(hSnapModule, ModuleEntry) then
          repeat
            if AnsiLowerCase(Result) = AnsiLowerCase(ModuleEntry.szModule) then
            begin
              Result := ModuleEntry.szExePath;
              Break;
            end;
          until not Module32Next(hSnapModule, ModuleEntry);
        finally
          CloseHandle(hSnapModule);
        end;
        Break;
      end;
    until not Process32Next(hSnapProcess, ProcessEntry);
  finally
    CloseHandle(hSnapProcess);
  end;
end;

function TMainForm.GetPort(dwPort: DWORD; const Proto: string): string;
var
  ServEnt: PServEnt;
begin
  Result := IntToStr(htons(dwPort));
  ServEnt := getservbyport(dwPort, PChar(Proto));
  if Assigned(ServEnt) then Result := ServEnt^.s_name;
end;

procedure TMainForm.GetConnectionsTable(ReNew: Boolean);
var
  ConnTable: Pointer;
  xTcpRow: PMibTcpRow;
  dwSize: DWORD;
  i, j: Integer;
  xport:string[5];
  xpath:string;
begin
    Check(GetTcpExTable(@ConnTable, True, GetProcessHeap, 0, 2));
try
    if not FoundExApi then
    begin
    Check(GetTcpTable(ConnTable, @dwSize, True));
    end;
    with PMibTcpTable(ConnTable)^ do
    begin
      for i := 0 to dwNumEntries - 1 do
      begin
        if FoundExApi then
          xTcpRow := PMibTcpRow(Longint(@table[i]) + (i * PID_SIZE))
        else
          xTcpRow := @table[i];
          begin
            if FoundExApi then
            begin
xport:=(Format('%s', [GetPort(xTcpRow^.dwLocalPort, 'TCP')]));
if xport='3306' then
begin
//Timer.Enabled := false;
listbox1.Items.add('mysql is open');
end;
if xport='1433' then
begin
//Timer.Enabled := false;
listbox1.Items.add('mSsql is open');
end;
            end;
          end;
      end;
    end;
  finally
    if FoundExApi then
      HeapFree(GetProcessHeap, 0, ConnTable)
    else
      FreeMem(ConnTable);
 end;
end;

procedure TMainForm.TimerTimer(Sender: TObject);
begin
  GetConnectionsTable(true);
end;
end.
0
carsRSTCommented:
Make it easy...

If it's windows, it should be running as a service.

1.  Go to Control Panel
2.  Then Administrative tools
3.  Then double click services.
4.  Optional but sort by name and look for "MySQL"
5.  If you see it, see if what the "status" is - is it running or not?
6.  If it's not running, then start it, so you can log in.

Now, if you see it's running and you have a UN/PW, then you just need to run the MYSQL.exe to log in (just do a search on the computer/server for that exe).

Once you're in, then just type the following to see what databases it has:

mysql> show databases;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
danz67Author Commented:
systan:

I thank you for the attached draft but I do not understand how to use functions to obtain information
0
systanCommented:
if your goal is to detect if any database server is running, then your asking about a delphi code to find mysql server then that draft is good.  Just add 1 Listbox and a Timer to your Delphi form, then paste the code to your unit1.pas, build it, or run it, then you will find what you are looking for.
0
systanCommented:
Oh, that was a Delphi solution!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.