user2834566
user2834566

Reputation: 805

Why do queries run through MyDAC TMyConnection sometimes fail because the connection protocol (IPv4 vs IPv6) changes

Scenario

My application obtains the public IP address of the user by running a small PHP script on the same server that hosts my database. I then programatically insert that IP address into the remote hosts table so that I can gain remote access to the DB to run queries etc on port 3306.

When I first get the IP address it may return as IPv4 or IPv6, depending on the whims of the server. I store the IP address I get, test the connection by setting TMyconnection to true - which works OK and disconnect again. Later, when I use TMyquery to run a query against the database I reconnect, or let the query do that, and run the query.

I do have the means to get my IP address as definitely IPv4 so I could standardise on IPv4 and always store that in the remote hosts.

The problem

Assume I have an IPv4 address stored in the remote hosts table (in the form 185.83.68.71) and the test connection passed. Sometimes, but not always, when the application later runs a query, it gets an error back from the database along the lines of " Connection Error :#28000Access denied for user 'myuser'@'2a0c:5c86:8220:1:8905:f958:d2f8:368d' (using password: YES) i.e. the TMyconnection is now trying to reach the database using my user's IPv6 IP address. This of course fails as that address is not in the remote hosts.

I have also had the opposite. The application stores an IPv6 address and tests the connection OK but TMyConnection later uses an IPv4 address, which fails as the IPv4 address is not in the remote hosts table.

Question

(1) Does TMyconnection itself get the user's IP address from somewhere every time it connects, which might be returned as a different protocol to the one the application obtained? If not how does it know which protocol, IPv4 or IPv6, to use for the connection?

(2) Is there a way to force TMyconnection to always use IPv4? Then my application can always obtain the user's IP address in IPv4 and always store an address that matches the one TMyConnection will use.

(I have posted a similar question on the Devart forum but that is not very active and there are more talented people here!)

Upvotes: 0

Views: 82

Answers (2)

Devart
Devart

Reputation: 122042

Please be informed that to indicate the IP Version in MyDAC there is a property "IPVersion". This property can take three values:

  • ivIPBoth - specifies that either Internet Protocol Version 6 (IPv6) or Version 4 (IPv4) will be used;
  • ivIPv4 (default) - specifies that Internet Protocol Version 4 (IPv4) will be used;
  • ivIPv6 - specifies that Internet Protocol Version 6 (IPv6) will be used.

Here is an example of the code to indicate the required protocol version:

...

uses

..., CRVio;

...

MyConnection.Options.IPVersion := ivIPv4;

...

More details about the property can be found in our documentation: https://docs.devart.com/mydac/devart.mydac.tmyconnectionoptions.ipversion.htm

Upvotes: 0

user2834566
user2834566

Reputation: 805

Found an answer to my question 2 so I'm putting it here in case others need it. The TMyConnection has a property Options and that has a property IPVersion. The IPversion can be set to the values ivIPv4, ivIPv6 or ivIPboth.

But the default is ivIPv4, so the TMyConnection used in my application should have only been using an IPv4 response when connecting. So I still don't understand why it sometimes used an IPv6 address and my question 1 remains unanswered.

Upvotes: 0

Related Questions