An updated solution for Windows users. I am running
Windows 10
MySql 8.0.30
Ruby 3.1.2
My Ruby version is the one from rubyinstaller.org with the MYSYS devkit included
I downloaded a MariaDB C connector library msi installer file v3.1.17, ran it and it installed the relevant files in a directory in Program Files. I copied that directory to another location, renamed it so that it didn't have any spaces and ran the command:
gem install mysql2 --platform=ruby -- --with-mysql-dir="c:/my-mariadb-connector-path-with-no-spaces"
This gave me a functioning gem.
This is a specific version of the library and not the most recent. Finding your way around the download options on the MariaDB website is a bit confusing, so here is the direct download link.
https://dlm.mariadb.com/2319542/Connectors/c/connector-c-3.1.17/mariadb-connector-c-3.1.17-win64.msi
Before I got to this point I'd tried several things which hadn't worked. I've included them below as background information, but if all you're interested in is a working solution then you can stop reading now.
A few sources suggest that if you have the MYSYS devkit installed you can build the gem with the following command:
ridk exec gem install mysql2 --platform=ruby -- --use-system-libraries
This did not compile for me.
You may also have seen a recommendation to download an archived version - 6.1 - of the MySQl connector library. I tried this, the gem compiled, but when I tried to start my app I got an error message: "Incorrect MySQL client library version! This gem was compiled for 6.1.11 but the client library is 10.5.5."
In MySQL V8 and above the C library is no longer an optional extra but is included in the main installation. I found the relevant files, copied them to a no spaces directory path and tried the usual command. Again the gem compiled but the app wouldn't start. This time the error message said that "This gem was compiled for 8.0.30 but the client library is 10.5.5"
I also tried the latest version of of the MariaDB library. With that the error message said that the gem was compiled for 10.6.8. You need the specific version described above, and yes, it really is a MariaDB library you need even if you're using "Original" MySql as distributed by the Oracle Corporation