Recent

Author Topic: [Solved] mySQL: impossible to open a stable connection with mysql_real_connect  (Read 1044 times)

devEric69

  • Hero Member
  • *****
  • Posts: 648
Hello,

For some reason, which my reason doesn't know, my WAMP-mySQL server continues to accept connections from the Lazarus IDE, and from PhpMyAdmin, but now refuses connections from a binary compiled with Lazarus (even if I close Lazarus before running Project1.exe; Cf. attached screenshot) :( . It's all the more annoying, as PhpMyAdmin continues to work via its Ajax-based-dll-PDO client.

So, i'm trying to understand if it's mysql_init, mysql_options or mysql_real_connect, which is the culprit. I've searched on the web: I found this problem many times. But nothing that pinpoints precisely where the problem lies (I've no firewall, only one libmysql.dll on my PC, etc).   
Now, i'm looking at the file mariadb_lib.c on https://github.com/mariadb-corporation/mariadb-connector-c/tree/3.1/libmariadb. This function takes a pointer to a structure as parameter: if this pointer is NULL in C++ language i.e. nil in Pascal language, then a malloc is made and the function simply returns the validly assigned pointer from the address of the structure loaded in the heap. That's all

I load the library like this..:
Code: Pascal  [Select][+][-]
  1. var
  2.  hFLib: TLibHandle;
  3. begin
  4.  hFLib:= Dynlibs.SafeLoadLibrary ('C:\Windows\System32\libmysql.dll');
  5. end;

...and it works. I see (Cf. the screenshot) that the library is loaded i.e. a copy of the code of this library is linked to Project1.exe.

Then, I'm trying to call mysql_init like this...:

Code: Pascal  [Select][+][-]
  1. uses
  2.   mysql57dyn;
  3. ...\...
  4. var
  5.   HMySQL: PMySQL;
  6. begin
  7.   HMySQL:= nil;
  8.   HMySQL:=  mysql57dyn.mysql_init(nil);
  9.   if not Assigned(HMySQL) then
  10.     ShowMessage('HMySQL is not assigned')
  11.   else
  12.     ShowMessage('HMySQL is assigned');


...and i've an access violation. In fact, looking at mysql.inc, i guess that var ...\... mysql_init: function (mysql:PMYSQL):PMYSQL;extdecl;
is a kind of way to declare a pointer to a library routine (I don't know this syntax). My problem seems to be that the way i call it or  the moment i call it, it doesn't point at anything at all.

In fact, i would like to call this routine via its external+ name conventional (for me) declaration i.e. to call:

Code: Pascal  [Select][+][-]
  1. function mysql_init(mysql:PMYSQL):PMYSQL;extdecl;external mysqllib name 'mysql_init';

However, all external declarations, already present in mysql.inc, are conditioned with  {$IFNDEF LinkDynamically}. But if I add uses mysql57dyn; to my project, I implicitly add {$DEFINE LinkDynamically}.



==> Does anyone know how to:
- use the structures imported from the *.h files in mysql.inc
- and use the external external+ name function declarations  :-\ ?
« Last Edit: June 21, 2020, 09:24:40 am by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

devEric69

  • Hero Member
  • *****
  • Posts: 648
I've just found and read the explanation of the framework for writing a unit to use an external library: https://wiki.freepascal.org/Dynamically_loading_headers.

So i've replaced ...:

Code: Pascal  [Select][+][-]
  1. Dynlibs.SafeLoadLibrary ('C:\Windows\System32\libmysql.dll');

...with...:

Code: Pascal  [Select][+][-]
  1. mysql57dyn.InitialiseMysql;

...and the mysql_init call works. I'll look at a step further.
« Last Edit: June 21, 2020, 09:25:03 am by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

devEric69

  • Hero Member
  • *****
  • Posts: 648
For information: fixed (firewall problem, launching only briefly, but briefly anyway, before a script asking to disable it launches itself; it seemed enough to let the Windows service launch the mysqld_z server in an unstable listening state sometimes: Lazarus as client often created the other half-connection, but never my compiled binary; no other explanation :-\ ).

In my case, i may have applications as NetStat or WYSIWYG software (like TCPView), etc, telling me that the server is indeed started and listening on the port number 3306, i come to the conclusion, that as long as a Telnet cmd (on localhost:3306, or on 127.0.0.1:3306) does not respond i.e. does not communicate, then the socket may have been created but really in a "unstable" way, therefore unusable.

By the way, there is an interesting dbtests.pp unit, which can be used for inspiration on how to write personal code around APi-mysql + @TMYSQL or a PMYSQL variable's value.
« Last Edit: June 22, 2020, 11:32:58 am by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

 

TinyPortal © 2005-2018