libmysql.dll version mismatch on windows 11
Hi,
I am trying to use the library on windows 11 with ruby 3.4. I install the gem through bundle install reading my gemfile. I am not specifying the version, so I think its the latest.
I am using webrick with rack. When I do a "rackup config.ru" I get a stack trace saying that the library was compiled for versio 10.x but I am using 3.x.
I have both Maria 10.11 and MySQL 8 installed. I also read somewhere that when on version mismatches, one should get the libmysql.dll from the database server and place it under the gem distribution in the ruby directory tree. If that is so, I don't knwo where to put it. I also did not find the file installed when the gem was installed. The docs say that the dll is fetched from the internet and placed somewhere.
I then dug a little more, and saw that on windows, to have a different version of the shared library that the gem uses, one has to compile some source files with mingw in order to generate a .a or .so. I saw that in a readme fille where a .so is placed.
I would really like to get this going, from the doc file, I like the simplicity of using it. So, in all, what are my options ?
Thanks, Regards
The libmysql.dll should be in your MySQL server installation directory under lib folder. If your MySQL Server is installed in C:/Program Files/MySQL Server 8.0, it will be in C:/MySQL Server 8.0/lib/libmysql.dll. Copy it and paste it in bin directory of where ruby is.
I looked at the generated extconf.rb for mysql2, and found an option that takes this a little further gem install mysql2 -- --with-mysql-dir="C:\Program Files\MariaDB 11.8"
the native extension compiles, but it does not link. I renamed a copy of the mariadb client library to make it findable by the build process
I get a lot of undefined symbols and the warning : Warning: corrupt .drectve at end of def file which from what I saw on the net is due to mixing gcc code with MSVC code of mariadb
Microsoft co-pilot says don't mix code. Recompile all with the same platform. But I can't do that, I need to use the mysql client library. Now what ?
I found this stack overflow post : ruby on rails - Installing MySQL2 Gem on Windows - Stack Overflow which explains what needs to be done to the mysql connector dll in order to link with the mysql2 shared library. I ran the dlltool on it and placed the output of it and the dll in the ruby executable directory. It is now working. I have a simple Sinatra application that serves a URL that connects to a database and does a select on a table.
TL;DR: There is a fix already merged, but to this date unreleased, so just add the GitHub repo to the Gemfile and it should install and work fine.
gem "mysql2", github: "brianmario/mysql2"
I just managed working installs of the mysql2 gem on a couple of servers running Windows Server 2025, so I thought I might document my process here, in case anyone else finds it useful.
These servers use the Plesk web hosting panel, and current versions of Plesk only offer one option of MySQL-family database: MariaDB 11.4. That didn't work with some of my legacy non-Ruby apps, so I installed MySQL 8.3 (those apps don't work with MySQL 8.4 either), but I had to set it up to run alongside MariaDB instead of replacing it, lest it cause issues with Plesk. To install MySQL 8.3 I didn't do anything fancy: downloaded the MSI installer and installed it with the default options. After the setup was done, MySQL Configurator started. It detected the MariaDB installation and offered to “update” it, but I was careful to choose to install MySQL 8.3 alongside the MariaDB installation. The only other thing I needed to be careful about was choosing a port other than 3306, since MariaDB was using that one.
Once MySQL 8.3 was installed and configured, I tweaked a few settings in my.ini (buffer sizes, stuff like that), then started it and imported a dump of the database from the previous server.
The first Ruby app that I needed to set up in this new server was running on Ruby 3.3, so I opted to try 3.4. I installed it in C:\Ruby34 using RubyInstaller, and in the MSYS2 setup prompt I selected 1,2,3 (install MSYS2, install MSYS2 updates, install development toolchain—the default options are 1,3, skipping the MSYS2 updates). After that, I made sure to put C:\Ruby34\bin in the PATH env var.
First attempt
I ran bundle in the application directory, and it didn't work, reporting a gem that was not included with Ruby anymore. Following the instructions in the error message, I added it to the Gemfile and ran bundle again. The same thing happened with another gem, and another, about half a dozen times in total, then, in one of the servers, the bundle was complete.
Unfortunately, the app itself crashed at require "mysql2", reporting:
Incorrect MySQL client library version! This gem was compiled for 10.8.8 but the client library is 3.4.5.
In the other server, I don't really know why, Bundler failed to install mysql2 because it didn't find libmysqlclient-related files (I'm not sure now if it was the headers or the libraries). A quick web search suggested using --with-mysql-dir pointing to msys64/mingw64 within the Ruby directory. In my case, that amounted to:
gem install mysql2 --platform=ruby -- "--with-mysql-dir=C:/Ruby34/msys64/mingw64"
That allowed Bundler to finish, but produced the same library version error as above.
Second attempt
Remembering how I often do it on Linux, I decided to try pointing to the MySQL server directory:
gem install mysql2 --platform=ruby -- "--with-mysql-dir=C:/Program Files/MySQL/MySQL 8.3"
This failed during linking, with a looooot of undefined references. This, right at the end of the configuration step, right before it starts compiling, either shows the cause for those undefined references, or that the build would still have failed (or the gem wouldn't have worked) even without the undefined references:
-----
Setting rpath to /Program
-----
Notice how it removed the drive letter and stopped at the first space. I tried quoting just the path (starting next to the equal sign) but it didn't change anything. I considered copying the MySQL directory to a directory without spaces in the name, but the undefined references gave me the impression that it wouldn't be the last issue to fix, so I decided to do a bit more research first.
Success!
Between web searches and Stack Overflow threads and GitHub issues, eventually I was directed towards this:
https://github.com/brianmario/mysql2/pull/1406
It's a fix for this specific problem, and it has already been merged into the mysql2 repo. It's just that a new version of the gem hasn't been released yet, so the latest version available on RubyGems and installed by default by Bundler doesn't include this fix.
With Bundler, the solution is easy enough: add the GitHub repo tag in the Gemfile, and Bundler will install the main branch of the repo instead of the version available on RubyGems.
gem "mysql2", github: "brianmario/mysql2"
Then bundle update mysql2, and voilà. I didn't need “--with-mysql-dir”. But one thing I did trip on is the fact that it requires Bundler to be loaded, so when I tested just invoking irb and in it require "mysql2" it gave the same error, because the version I had previously installed (the one available to RubyGems) is the broken one. When I tried bundle exec irb and then require "mysql2", it just returned true, with no error.