Connecting RadStudio to MySQL with FireDAC

A customer called our office recently saying that they could not make Rad Studio connect to a MySQL database, and that an internet search on how to do this had yielded no results. I’ve been using MySQL with Delphi for years, and so this is a situation I set out to remedy….

Lets start by taking a look at the issue. Start up Rad Studio and create a new application (VCL or FMX/Firemonkey), and drop a TFDConnection component on the main form…

Double click on the connection and select “MySQL” from the combo list titled “Driver ID”

Now fill in the fields for “Database”, “Username”, “Password”, and “Server” for your MySQL database, and click the “Test” button. You’ll first be presented with another connection dialog (click “OK”) and then the following error message…

The error message presented is a warning that the Rad Studio IDE can’t find “libmysql.dll” which is the driver library required to connect to your MySQL database. So far as I am aware, there is no single package for downloading this driver by it’s self, if anyone knows of one, and it’s both legal and conforming to the license agreement, please let me know and I’ll update this post. For now however, you must download an entire MySQL installation package and extract the required DLL file(s). The driver file is the same for MySQL Community server as for the corresponding commercial server of the same version.

Visit MySQL Developer Page and download the ZIP Archive files for BOTH 64-bit and 32-bit installations of MySQL server.

Extract both packages to separate locations.

Create a new directory structure to look something like this…

The dll name for 64-bits is precisely the same as that for 32-bits, and so we’ve created separate 32 & 64 bit directories to keep them separated. Now within both extracted directories “mysql–win32″ and “mysql–winx64″ navigate down into the “lib” directory and take a copy of “libmysql.dll” – Place each into their respective 32 or 64 bit directory within the “MySQL Client Libraries” directory that you created earlier.

You may now trash all of the MySQL server install files (zips and extracted directories), just keep your “MySQL Client Libraries” directory with the client libraries inside.

Now take a copy of the 32-bit “libMySQL.dll” file and place it somewhere which appears on your path when the IDE is running.
I’m dropping mine into “:\windows\system32”.

Edit: Do not place the DLL into “:\program files (x86)\Embarcadero\Studio\<version>\bin” as I’d previously suggested in this blog post. It was unknown to me at the time of writing, that placing anything in the Rad Studio “bin” directory will really upset your IDE installation!

This will enable the IDE to see the required driver library file at design time.

Note: The RadStudio IDE is a 32-bit application, regardless of the bitedness (is that a word?) of your version of windows. Hence you must use the 32-bit driver to enable the IDE to connect to MySQL databases.

Repeat the steps in the beginning of this post (under, “The Problem”) to create a MySQL connection at design-time. You should no longer get the error message but instead…

That’s great for design time, but what about your application at runtime? Well this is where you have some choices to make.

For your application to see the driver while running inside the IDE, you could copy the 32-bit version of the library into your Debug binary directory for 32-bits, and the 64-bit version into your 64-bit debug directory. To do this you’ll first have to build the application for both targets so that the relevant debug directories are created, and then copy the library dll files.

One alternative would be to place a copy of the MySQL driver library somewhere on the system path (such as “..\windows\system32″ for example) but this will only work for one target (32 or 64-bit).

When the time comes for you to deploy your application, you’ll also need to ship the dll’s for the relevant target platform. If you ship the 32-bit and 64-bit versions of your application in a single package, you likely already separate the executables into different directories, so simply ship the relevant dll version inside those directories.

You now have the power of the FireDAC framework to use against the excellent MySQL database! Go forth and make database applications! For additional information, see the official documentation for RadStudio here.

Thanks for reading!