Cannot connect to mysql problem

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
gosta100
Posts: 9
Joined: Tue 29 Nov 2011 16:35

Cannot connect to mysql problem

Post by gosta100 » Tue 29 Nov 2011 18:24

Hello,

I am using MyDAC 7.1 and Lazarus 0.9.30-2 with FPC 2.4.4. Trying to run the following very simple project, I get the exception: "Can't connect to MySQL server on 'IP' (10061)."

Code: Select all


program project1; 

{$mode objfpc}{$H+} 

uses 
  {$IFDEF UNIX}{$IFDEF UseCThreads} 
  cthreads, 
  {$ENDIF}{$ENDIF} 
  Classes, SysUtils, CustApp, MyAccess; 

type 

  { TMyApplication } 

  TMyApplication = class(TCustomApplication) 
  protected 
    procedure DoRun; override; 
  public 
    constructor Create(TheOwner: TComponent); override; 
    destructor Destroy; override; 
    procedure WriteHelp; virtual; 
  end; 

{ TMyApplication } 

procedure TMyApplication.DoRun; 
var 
  ErrorMsg: String; 
  LConn: TMyConnection; 
begin 
  // quick check parameters 
  ErrorMsg:=CheckOptions('h','help'); 
  if ErrorMsg'' then begin 
    ShowException(Exception.Create(ErrorMsg)); 
    Terminate; 
    Exit; 
  end; 

  // parse parameters 
  if HasOption('h','help') then begin 
    WriteHelp; 
    Terminate; 
    Exit; 
  end; 

  { add your program here } 
  LConn := TMyConnection.Create(nil); 
  LConn.Server := 'IP'; 
  LConn.LoginPrompt:=false; 
  LConn.Database := 'database'; 
  LConn.Username:='user'; 
  LConn.Password:='pass'; 
  try 
    LConn.Connect; 
  finally 
    FreeAndNil(LConn); 
  end; 
  // stop program loop 
  Terminate; 
end; 

constructor TMyApplication.Create(TheOwner: TComponent); 
begin 
  inherited Create(TheOwner); 
  StopOnException:=True; 
end; 

destructor TMyApplication.Destroy; 
begin 
  inherited Destroy; 
end; 

procedure TMyApplication.WriteHelp; 
begin 
  { add your help code here } 
  writeln('Usage: ',ExeName,' -h'); 
end; 

var 
  Application: TMyApplication; 

{$R *.res} 

begin 
  Application:=TMyApplication.Create(nil); 
  Application.Run; 
  Application.Free; 
end.


Note that I can connect to the remote server using the mysql client.
Any ideas?

gosta100
Posts: 9
Joined: Tue 29 Nov 2011 16:35

Post by gosta100 » Wed 30 Nov 2011 20:46

Hi again,

could someone please try to reproduce the problem? I am really desperate and cannot see the solution.

Thanks again.

AndreyZ

Post by AndreyZ » Thu 01 Dec 2011 08:42

Hello,

MyDAC uses port 3306 by default. If your MySQL server uses another port, you should set it to the TMyConnection.Port property. Here is an example:

Code: Select all

LConn := TMyConnection.Create(nil);
LConn.Server := 'IP';
LConn.Port := 3307;
LConn.LoginPrompt:=false;
LConn.Database := 'database';
LConn.Username:='user';
LConn.Password:='pass';
try
  LConn.Connect;
finally
  FreeAndNil(LConn);
end;

gosta100
Posts: 9
Joined: Tue 29 Nov 2011 16:35

Post by gosta100 » Thu 01 Dec 2011 14:20

Hello AndreyZ,

my mysqlserver listens on the default port, so this is not the problem. I managed to make this work in the end. If you enter the IP with the octets reversed, it works as expected. So for example, if you enter '1.0.0.127' instead of '127.0.0.1' it works. Any idea what this is about? Is this a known bug?

AndreyZ

Post by AndreyZ » Fri 02 Dec 2011 09:03

Thank you for the information. We have reproduced this problem. We will fix this problem in the next MyDAC build.

Post Reply