Accessing MySQL from Lazarus/Free Pascal on macOS.
|To follow this tutorial, it is supposed that you have Lazarus and MySQL installed, as well as the "world" sample database. If you have problems with the installation of Lazarus, my tutorial Installing Lazarus/Free Pascal on macOS may be helpful. And, concerning the installation of MySQL (including the "world" database), you may want to have a look at my Web/Database environment on macOS: MySQL database server setup tutorial.
|In my tutorial Connecting to a MySQL database using Lazarus/Free Pascal, I describe in detail how to connect to a MySQL 5.7 database from a Lazarus/Free Pascal application, using a TMySQL57Connection (with MySQL 8.0, use a TMySQL80Connection instead), or alternatively, a TODBCConection on MS Windows. This new tutorial shows how to access MySQL from a Lazarus/Free Pascal application on macOS. On most websites, that I visited when trying to figure out how to do, they recommend to use a TODBCConection connection. Personally, I find that setting up ODBC on macOS is rather complicated (at least if you compare with Windows), whereas using the TMySQL80Connection is straight forward (provided that you know how to resolve the dynamic library problem, described further down in the text). This tutorial will only cover the TMySQL80Connection (maybe I'll write another one using a TODBCConection sometimes in the future).
|The sample application, used in this tutorial has been tested on macOS 11 Big Sur, with Lazarus 2.2.0 and MySQL Community Server 8.0.31. It should apply to other (in particular newer) versions of macOS, Lazarus and MySQL, too. I suppose...
|I did not write a new application on my Mac, but tried to build the one that I use with the Connecting to a MySQL database using Lazarus/Free Pascal on Windows tutorial. Please, note that this application, written for MySQL 5.7, uses a TMySQL57Connection; that's why you have unit "mysql57conn" in the source code on the screenshot (this will not work; cf. further down in the text). You can see that the build was successful, and that the application started up as it should.
|However, when pushing the "Connect" button within the application, an EInOutError exception is raised; error message: Cannot load default MySQL library.
|It took me some searching the Internet, before I found the answers to the two questions that we have to ask here: What is the reason that this error occurs? What can we do to solve the issue? The mentioned library libmysqlclient.21.dylib corresponds to libmysql.dll on Windows and if you have a look at the Windows tutorial, you see that there, too, the error, that it could not be loaded, occurred. Not be loaded, because not be found, and we had to copy it to the Lazarus project output folder to make things work. Is this the solution on macOS, too? No! Even after copying libmysqlclient.21.dylib from /usr/local/mysql/lib to the project output folder, we still get the message that it can't be loaded!
|This is due to the fact that mysql.inc calls LoadLibrary with the library file name, but without specifying a path. That means that the system path will be searched. On macOS default library paths are /usr/lib and /usr/local/lib, /usr/local/mysql/lib is not on the default path, so the MySQL client library can't be loaded because it can't be found (and copying it to the project output folder doesn't change this).
There are several possibilities to solve the issue, as for example, adding /usr/local/mysql/lib to the library paths at runtime. A simpler solution consists in
copying the library file from /usr/local/mysql/lib to /usr/local/lib, or more elegantly, creating a symbolic link of
/usr/local/mysql/lib/libmysqlclient.21.dylib in /usr/local/lib. The general syntax for creating a symlink is as follows:
ln -s /path_to_original /path_to_link
in our case:
sudo ln -s /usr/local/mysql/lib/libmysqlclient.21.dylib /usr/local/lib/libmysqlclient.dylib
where prefixing the command with sudo gives us the super-user privileges that are needed to create a file in /usr/local/lib/.
|After having created the symlink, I reran the sample application, but just got another exception: TMySQL57Connection cannot work with the installed MySQL client version.
|This is a problem with MySQL that you don't, for example, have with MariaDB: MySQL clients are not backward-compatible. For Lazarus applications, that means that there must be a specific TMySQLxxConnection for each release of the client, i.e. specific for the MySQL client dynamic library used. What happened here is that my application used a TMySQL57Connection with a client 8.0 (as I said above, I build my macOS application using the code that I'd written for MySQL 5.7 on Windows), and that cannot work!
|No problem to make the application work: Just change the source, replacing the TMySQL57Connection by a TMySQL80Connection...
|To terminate the tutorial, a screenshot of the successful execution of the sample MySQL application:
If you find this text helpful, please, support me and this website by signing my guestbook.